From 0566fc38af75122dddd4d7b9d1bfc23d91f41ff6 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 25 Feb 2024 16:34:24 +0100 Subject: [PATCH] Update document build formats to support text highlighting --- novelwriter/core/tohtml.py | 4 ++++ novelwriter/core/tomd.py | 4 ++++ novelwriter/core/toodt.py | 21 +++++++++++++++++++-- novelwriter/tools/manuscript.py | 2 ++ sample/content/636b6aa9b697b.nwd | 6 +++--- sample/nwProject.nwx | 8 ++++---- 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/novelwriter/core/tohtml.py b/novelwriter/core/tohtml.py index 51e0d04a..2c293070 100644 --- a/novelwriter/core/tohtml.py +++ b/novelwriter/core/tohtml.py @@ -133,6 +133,8 @@ class ToHtml(Tokenizer): self.FMT_D_E: "", self.FMT_U_B: "", self.FMT_U_E: "", + self.FMT_M_B: "", + self.FMT_M_E: "", } else: htmlTags = { # HTML5 (for export) @@ -144,6 +146,8 @@ class ToHtml(Tokenizer): self.FMT_D_E: "", self.FMT_U_B: "", self.FMT_U_E: "", + self.FMT_M_B: "", + self.FMT_M_E: "", } htmlTags[self.FMT_SUP_B] = "" diff --git a/novelwriter/core/tomd.py b/novelwriter/core/tomd.py index ad51e3b7..fbc2e463 100644 --- a/novelwriter/core/tomd.py +++ b/novelwriter/core/tomd.py @@ -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: "~", diff --git a/novelwriter/core/toodt.py b/novelwriter/core/toodt.py index e4bf57c5..494b12c4 100644 --- a/novelwriter/core/toodt.py +++ b/novelwriter/core/toodt.py @@ -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%" diff --git a/novelwriter/tools/manuscript.py b/novelwriter/tools/manuscript.py index aaf09141..8d95da6a 100644 --- a/novelwriter/tools/manuscript.py +++ b/novelwriter/tools/manuscript.py @@ -768,6 +768,8 @@ class _PreviewWidget(QTextBrowser): html = html.replace("\t", "!!tab!!") html = html.replace("", "") html = html.replace("", "") + html = html.replace("", "") + html = html.replace("", "") self.setHtml(html) qApp.processEvents() while self.find("!!tab!!"): diff --git a/sample/content/636b6aa9b697b.nwd b/sample/content/636b6aa9b697b.nwd index 78c6d8b9..ed5bacb6 100644 --- a/sample/content/636b6aa9b697b.nwd +++ b/sample/content/636b6aa9b697b.nwd @@ -1,8 +1,8 @@ %%~name: Making a Scene %%~path: 6a2d6d5f4f401/636b6aa9b697b %%~kind: NOVEL/DOCUMENT -%%~hash: d7920fe95fb48140278ae160fe7c3c1ce530072c -%%~date: Unknown/2023-10-20 22:57:56 +%%~hash: e1c58699b05b512306a534da70c2952aafc60e8b +%%~date: Unknown/2024-02-25 16:33:40 ### Making a Scene @pov: Jane @@ -13,7 +13,7 @@ A scene is defined by a level three heading, like the one at the top of this pag Each paragraph in the scene is separated by a blank line. The text supports minimal formatting, like **bold**, _italic_ and **_bold italic_**. You can also ~~strike through~~ text. There is **some support for _nested_ emphasis**, but there are some known limitations. If the syntax highlighter doesn’t show it correctly, the export tool will not either. -For special formatting aside from standard emphasis, you can use “shortcodes”. Here we have super[sup]script[/sup] and sub[sub]script[/sub], and [b]part[/b]ial bold too, which ends in the middle of a word. The shortcodes also support [u]underline[/u]. +For special formatting aside from standard emphasis, you can use “shortcodes”. Here we have super[sup]script[/sup] and sub[sub]script[/sub], and [b]part[/b]ial bold too, which ends in the middle of a word. The shortcodes also support [u]underline[/u], and you can [m]highlight text as well[/m]. In addition, the editor supports automatic formatting of “quotes”, both double and ‘single’. Depending on the syntax highlighter settings and colour theme, these can be in different colours. “You can of course use **bold** and _italic_ text inside of quotes too.” diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index 9f504c5a..82a35ce8 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,6 +1,6 @@ - - + + Sample Project Jane Smith @@ -35,7 +35,7 @@ Main - + Novel @@ -57,7 +57,7 @@ Chapter One - + Making a Scene