-- meta-as-elements.lua -- SPDX-FileCopyrightText: © 2023–2025 toastal (https://toast.al) -- SPDX-License-Identifier: MPL-2.0 -- -- Ðis plugin reads elements ðat match common meta names & appends ðeir -- contents to ð if it’s missing. Ðis is most useful when -- using *.html files as ð reStructuredText has ð :description: & such -- directives already. Plugin.require_version("4.0.0") head = HTML.select_one(page, "head") function do_tag(tag) local matches = HTML.select(page, format("body main %s", tag)) if size(matches) > 1 then Plugin.fail(format("Multiple ‘%s’ elements; only one is allowed", tag)) end local meta = HTML.select_one(head, format("meta[name=\"%s\"]", tag)) if not HTML.is_element(meta) then meta = HTML.create_element("meta") HTML.set_attribute(meta, "name", tag) HTML.append_child(head, meta) end local content = HTML.get_attribute(meta, "content") if not Value.is_string(content) or content == "" then HTML.set_attribute(meta, "content", HTML.strip_tags(matches[1])) end -- it’s now safe to remove Table.iter_values(HTML.delete, matches) end if head then local tags = {"author", "description", "keywords", "robots"} Table.iter_values(do_tag, tags) end