added meta viewport to dhtml output

This commit is contained in:
Michael Murtaugh 2021-07-10 14:06:19 +02:00
parent 7599c70be5
commit 32dacb00a1
2 changed files with 11 additions and 2 deletions

View File

@ -43,7 +43,8 @@ def pluralize (x):
else: else:
return (x,) return (x,)
def html5tidy (doc, charset="utf-8", title=None, scripts=None, links=None, indent=False):
def html5tidy (doc, charset="utf-8", title=None, scripts=None, links=None, indent=False, viewport_meta=None):
if scripts: if scripts:
script_srcs = [x.attrib.get("src") for x in doc.findall(".//script")] script_srcs = [x.attrib.get("src") for x in doc.findall(".//script")]
for src in pluralize(scripts): for src in pluralize(scripts):
@ -74,6 +75,14 @@ def html5tidy (doc, charset="utf-8", title=None, scripts=None, links=None, inden
if not meta_charsets: if not meta_charsets:
meta = ET.SubElement(doc.find(".//head"), "meta", charset=charset) meta = ET.SubElement(doc.find(".//head"), "meta", charset=charset)
# to add:
# <meta name="viewport" content="width=device-width,initial-scale=1">
# use viewport_meta="width=device-width,initial-scale=1"
if viewport_meta:
meta_viewports = [x.attrib.get("content") for x in doc.findall(".//meta") if x.attrib.get("name") == "viewport"]
if not meta_viewports:
meta = ET.SubElement(doc.find(".//head"), "meta", name="viewport", content=viewport_meta)
if title != None: if title != None:
titleelt = doc.find(".//title") titleelt = doc.find(".//title")
if not titleelt: if not titleelt:

View File

@ -221,7 +221,7 @@ def main (args):
ver["url"] = quote(ver["path"]) ver["url"] = quote(ver["path"])
# doc = html5lib.parse(html, treebuilder="etree", override_encoding="utf-8", namespaceHTMLElements=False) # doc = html5lib.parse(html, treebuilder="etree", override_encoding="utf-8", namespaceHTMLElements=False)
doc = html5lib.parse(html, treebuilder="etree", namespaceHTMLElements=False) doc = html5lib.parse(html, treebuilder="etree", namespaceHTMLElements=False)
html5tidy(doc, indent=True, title=padid, scripts=args.script, links=links) html5tidy(doc, indent=True, title=padid, scripts=args.script, links=links, viewport_meta="width=device-width,initial-scale=1")
with open(ver["path"], "w") as f: with open(ver["path"], "w") as f:
# f.write(html.encode("utf-8")) # f.write(html.encode("utf-8"))
print(ET.tostring(doc, method="html", encoding="unicode"), file=f) print(ET.tostring(doc, method="html", encoding="unicode"), file=f)