diff --git a/novelwriter/core/tohtml.py b/novelwriter/core/tohtml.py index 40df4a2c..0781202a 100644 --- a/novelwriter/core/tohtml.py +++ b/novelwriter/core/tohtml.py @@ -146,10 +146,25 @@ class ToHtml(Tokenizer): parStyle = None tmpResult = [] - for tType, tLine, tText, tFormat, tStyle in self.theTokens: + for tType, tLine, tDirty, tFormat, tStyle in self.theTokens: - # Replace < and > before adding html tags - tText = tText.replace("<", "<").replace(">", ">") + # Replace < and > and recompute formatting positions + cText = [] + i = 0 + for c in tDirty: + if c == "<": + cText.append("<") + tFormat = [[a + 3 if a > i else a, b, c] for a, b, c in tFormat] + i += 4 + elif c == ">": + cText.append(">") + tFormat = [[a + 3 if a > i else a, b, c] for a, b, c in tFormat] + i += 4 + else: + cText.append(c) + i += 1 + + tText = "".join(cText) # Styles aStyle = [] diff --git a/tests/test_core/test_core_tohtml.py b/tests/test_core/test_core_tohtml.py index 9ee396d0..f8a852d4 100644 --- a/tests/test_core/test_core_tohtml.py +++ b/tests/test_core/test_core_tohtml.py @@ -378,6 +378,48 @@ def testCoreToHtml_ConvertDirect(mockGUI): # END Test testCoreToHtml_ConvertDirect +@pytest.mark.core +def testCoreToHtml_SpecialCases(mockGUI): + """Test some special cases that has caused errors in the past. + """ + theProject = NWProject(mockGUI) + theHtml = ToHtml(theProject) + theHtml.isNovel = True + + # Greater/Lesser than symbols + # =========================== + + theHtml.theText = "Text with > and < with some **bold text** in it.\n" + theHtml.tokenizeText() + theHtml.doConvert() + assert theHtml.theResult == ( + "

Text with > and < with some bold text in it.

\n" + ) + + theHtml.theText = "Text with some <**bold text**> in it.\n" + theHtml.tokenizeText() + theHtml.doConvert() + assert theHtml.theResult == ( + "

Text with some <bold text> in it.

\n" + ) + + theHtml.theText = "Let's > be > _difficult **shall** > we_?\n" + theHtml.tokenizeText() + theHtml.doConvert() + assert theHtml.theResult == ( + "

Let's > be > difficult shall > we?

\n" + ) + + theHtml.theText = "Test > text _<**bold**>_ and more.\n" + theHtml.tokenizeText() + theHtml.doConvert() + assert theHtml.theResult == ( + "

Test > text <bold> and more.

\n" + ) + +# END Test testCoreToHtml_SpecialCases + + @pytest.mark.core def testCoreToHtml_Complex(mockGUI, fncDir): """Test the save method of the ToHtml class.