Improve handling of CSS styles, and add viewport meta tag (#2358)

This commit is contained in:
Veronica Berglyd Olsen
2025-06-14 15:28:51 +02:00
parent bae654f260
commit af6b35cc43
+45 -43
View File
@@ -159,34 +159,33 @@ class ToHtml(Tokenizer):
# If we don't have formatting, we can do a plain replace # If we don't have formatting, we can do a plain replace
tText = tText.replace("<", "&lt;").replace(">", "&gt;") tText = tText.replace("<", "&lt;").replace(">", "&gt;")
# Styles # Inline Styles
aStyle = [] aStyle = []
if self._cssStyles: if tStyle & BlockFmt.LEFT:
if tStyle & BlockFmt.LEFT: aStyle.append("text-align: left;")
aStyle.append("text-align: left;") elif tStyle & BlockFmt.RIGHT:
elif tStyle & BlockFmt.RIGHT: aStyle.append("text-align: right;")
aStyle.append("text-align: right;") elif tStyle & BlockFmt.CENTRE:
elif tStyle & BlockFmt.CENTRE: aStyle.append("text-align: center;")
aStyle.append("text-align: center;") elif tStyle & BlockFmt.JUSTIFY:
elif tStyle & BlockFmt.JUSTIFY: aStyle.append("text-align: justify;")
aStyle.append("text-align: justify;")
if tStyle & BlockFmt.PBB: if tStyle & BlockFmt.PBB:
aStyle.append("page-break-before: always;") aStyle.append("page-break-before: always;")
if tStyle & BlockFmt.PBA: if tStyle & BlockFmt.PBA:
aStyle.append("page-break-after: always;") aStyle.append("page-break-after: always;")
if tStyle & BlockFmt.Z_BTM: if tStyle & BlockFmt.Z_BTM:
aStyle.append("margin-bottom: 0;") aStyle.append("margin-bottom: 0;")
if tStyle & BlockFmt.Z_TOP: if tStyle & BlockFmt.Z_TOP:
aStyle.append("margin-top: 0;") aStyle.append("margin-top: 0;")
if tStyle & BlockFmt.IND_L: if tStyle & BlockFmt.IND_L:
aStyle.append(f"margin-left: {self._blockIndent:.2f}em;") aStyle.append(f"margin-left: {self._blockIndent:.2f}em;")
if tStyle & BlockFmt.IND_R: if tStyle & BlockFmt.IND_R:
aStyle.append(f"margin-right: {self._blockIndent:.2f}em;") aStyle.append(f"margin-right: {self._blockIndent:.2f}em;")
if tStyle & BlockFmt.IND_T: if tStyle & BlockFmt.IND_T:
aStyle.append(f"text-indent: {self._firstWidth:.2f}em;") aStyle.append(f"text-indent: {self._firstWidth:.2f}em;")
if aStyle: if aStyle:
stVals = " ".join(aStyle) stVals = " ".join(aStyle)
@@ -288,26 +287,25 @@ class ToHtml(Tokenizer):
json.dump(data, fObj, indent=2) json.dump(data, fObj, indent=2)
else: else:
html = []
html.append("<!DOCTYPE html>")
html.append("<html>")
html.append("<head>")
html.append(f"<title>{self._project.data.name:s}</title>")
html.append("<meta charset='utf-8'>")
if self._cssStyles:
html.append("<meta name='viewport' content='width=device-width, initial-scale=1'>")
html.append("<style>")
html.extend(self.getStyleSheet())
html.append("</style>")
html.append("</head>")
html.append("<body>")
html.append(("".join(self._pages)).replace("\t", "&#09;").rstrip())
html.append("</body>")
html.append("</html>\n")
with open(path, mode="w", encoding="utf-8") as fObj: with open(path, mode="w", encoding="utf-8") as fObj:
fObj.write(( fObj.write("\n".join(html))
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<meta charset='utf-8'>\n"
"<title>{title:s}</title>\n"
"<style>\n"
"{style:s}\n"
"</style>\n"
"</head>\n"
"<body>\n"
"{body:s}\n"
"</body>\n"
"</html>\n"
).format(
title=self._project.data.name,
style="\n".join(self.getStyleSheet()),
body=("".join(self._pages)).replace("\t", "&#09;").rstrip(),
))
logger.info("Wrote file: %s", path) logger.info("Wrote file: %s", path)
@@ -410,7 +408,11 @@ class ToHtml(Tokenizer):
# isn't already open, and only closed if it has previously been opened. # isn't already open, and only closed if it has previously been opened.
tags: list[tuple[int, str]] = [] tags: list[tuple[int, str]] = []
state = dict.fromkeys(HTML_OPENER, False) state = dict.fromkeys(HTML_OPENER, False)
plain = not self._cssStyles
for pos, fmt, data in tFmt: for pos, fmt, data in tFmt:
if plain and fmt in (TextFmt.COL_B, TextFmt.COL_E):
# We ignore colour tags if CSS is off
continue
if m := HTML_OPENER.get(fmt): if m := HTML_OPENER.get(fmt):
if not state.get(fmt, True): if not state.get(fmt, True):
if fmt == TextFmt.COL_B and (color := self._classes.get(data)): if fmt == TextFmt.COL_B and (color := self._classes.get(data)):