Update document build formats to support text highlighting

This commit is contained in:
Veronica Berglyd Olsen
2024-02-25 16:34:24 +01:00
parent 69bf6a64aa
commit 0566fc38af
6 changed files with 36 additions and 9 deletions
+4
View File
@@ -133,6 +133,8 @@ class ToHtml(Tokenizer):
self.FMT_D_E: "</span>",
self.FMT_U_B: "<u>",
self.FMT_U_E: "</u>",
self.FMT_M_B: "<span style='background-color: yellow;'>",
self.FMT_M_E: "</span>",
}
else:
htmlTags = { # HTML5 (for export)
@@ -144,6 +146,8 @@ class ToHtml(Tokenizer):
self.FMT_D_E: "</del>",
self.FMT_U_B: "<span style='text-decoration: underline;'>",
self.FMT_U_E: "</span>",
self.FMT_M_B: "<mark>",
self.FMT_M_E: "</mark>",
}
htmlTags[self.FMT_SUP_B] = "<sup>"
+4
View File
@@ -95,6 +95,8 @@ class ToMarkdown(Tokenizer):
self.FMT_D_E: "",
self.FMT_U_B: "",
self.FMT_U_E: "",
self.FMT_M_B: "",
self.FMT_M_E: "",
self.FMT_SUP_B: "",
self.FMT_SUP_E: "",
self.FMT_SUB_B: "",
@@ -111,6 +113,8 @@ class ToMarkdown(Tokenizer):
self.FMT_D_E: "~~",
self.FMT_U_B: "",
self.FMT_U_E: "",
self.FMT_M_B: "==",
self.FMT_M_E: "==",
self.FMT_SUP_B: "^",
self.FMT_SUP_E: "^",
self.FMT_SUB_B: "~",
+19 -2
View File
@@ -84,14 +84,16 @@ X_BLD = 0x01 # Bold format
X_ITA = 0x02 # Italic format
X_DEL = 0x04 # Strikethrough format
X_UND = 0x08 # Underline format
X_SUP = 0x10 # Superscript
X_SUB = 0x20 # Subscript
X_MRK = 0x10 # Marked format
X_SUP = 0x20 # Superscript
X_SUB = 0x40 # Subscript
# Formatting Masks
M_BLD = ~X_BLD
M_ITA = ~X_ITA
M_DEL = ~X_DEL
M_UND = ~X_UND
M_MRK = ~X_MRK
M_SUP = ~X_SUP
M_SUB = ~X_SUB
@@ -189,6 +191,7 @@ class ToOdt(Tokenizer):
self._opaHead34 = None
self._colMetaTx = None
self._opaMetaTx = None
self._markText = "#ffffa6"
return
@@ -643,6 +646,10 @@ class ToOdt(Tokenizer):
xFmt |= X_UND
elif fFmt == self.FMT_U_E:
xFmt &= M_UND
elif fFmt == self.FMT_M_B:
xFmt |= X_MRK
elif fFmt == self.FMT_M_E:
xFmt &= M_MRK
elif fFmt == self.FMT_SUP_B:
xFmt |= X_SUP
elif fFmt == self.FMT_SUP_E:
@@ -710,6 +717,8 @@ class ToOdt(Tokenizer):
newStyle.setUnderlineStyle("solid")
newStyle.setUnderlineWidth("auto")
newStyle.setUnderlineColour("font-color")
if hFmt & X_MRK:
newStyle.setBackgroundColor(self._markText)
if hFmt & X_SUP:
newStyle.setTextPosition("super")
if hFmt & X_SUB:
@@ -1279,6 +1288,7 @@ class ODTTextStyle:
self._tAttr = {
"font-weight": ["fo", None],
"font-style": ["fo", None],
"background-color": ["fo", None],
"text-position": ["style", None],
"text-line-through-style": ["style", None],
"text-line-through-type": ["style", None],
@@ -1306,6 +1316,13 @@ class ODTTextStyle:
self._tAttr["font-style"][1] = None
return
def setBackgroundColor(self, value: str | None) -> None:
if value and len(value) == 7 and value[0] == "#":
self._tAttr["background-color"][1] = value
else:
self._tAttr["background-color"][1] = None
return
def setTextPosition(self, value: str | None) -> None:
if value in self.VALID_POS:
self._tAttr["text-position"][1] = f"{value} 58%"
+2
View File
@@ -768,6 +768,8 @@ class _PreviewWidget(QTextBrowser):
html = html.replace("\t", "!!tab!!")
html = html.replace("<del>", "<span style='text-decoration: line-through;'>")
html = html.replace("</del>", "</span>")
html = html.replace("<mark>", "<span style='background-color: #ffffa6;'>")
html = html.replace("</mark>", "</span>")
self.setHtml(html)
qApp.processEvents()
while self.find("!!tab!!"):