Add field value insertion to all formats

This commit is contained in:
Veronica Berglyd Olsen
2024-10-28 18:17:25 +01:00
parent f92521c93a
commit 42f904f1ea
5 changed files with 58 additions and 19 deletions
+14 -1
View File
@@ -84,6 +84,7 @@ class ToHtml(Tokenizer):
self._trMap = {}
self._cssStyles = True
self._usedNotes: dict[str, int] = {}
self._usedFields: list[tuple[int, str]] = []
self.setReplaceUnicode(False)
return
@@ -250,7 +251,15 @@ class ToHtml(Tokenizer):
return
def closeDocument(self) -> None:
"""Append the footnotes in the buffer."""
"""Run close document tasks."""
# Replace fields if there are stats available
if self._usedFields and self._counts:
pages = len(self._pages)
for doc, field in self._usedFields:
if doc >= 0 and doc < pages and (value := self._counts.get(field)) is not None:
self._pages[doc] = self._pages[doc].replace(f"{{{{{field}}}}}", f"{value:n}")
# Add footnotes
if self._usedNotes:
footnotes = self._localLookup("Footnotes")
@@ -416,6 +425,10 @@ class ToHtml(Tokenizer):
tags.append((pos, f"<sup><a href='#footnote_{index}'>{index}</a></sup>"))
else:
tags.append((pos, "<sup>ERR</sup>"))
elif fmt == TextFmt.FIELD:
if field := data.partition(":")[2]:
self._usedFields.append((len(self._pages), field))
tags.append((pos, f"{{{{{field}}}}}"))
# Check all format types and close any tag that is still open. This
# ensures that unclosed tags don't spill over to the next paragraph.