"""
novelWriter – ToHtml Class Tester
=================================
This file is a part of novelWriter
Copyright 2018–2024, Veronica Berglyd Olsen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
Some nested bold and italic and "
"strikethrough text here
Some bold, italic, strike, "
"underline, mark, "
"superscript, subscript here
Line one
Line two
Line three
" "Synopsis: " "The synopsis ..." "
\n" ) html.setSynopsis(True) html._text = "%short: A short description ...\n" html.tokenizeText() html.doConvert() assert html.result == ( "" "Short Description: " "A short description ..." "
\n" ) # Comment html._text = "% A comment ...\n" html.tokenizeText() html.doConvert() assert html.result == "" html.setComments(True) html._text = "% A comment ...\n" html.tokenizeText() html.doConvert() assert html.result == ( "" "Comment: " "A comment ..." "
\n" ) # Keywords html._text = "@char: Bod, Jane\n" html.tokenizeText() html.doConvert() assert html.result == "" html.setKeywords(True) html._text = "@char: Bod, Jane\n" html.tokenizeText() html.doConvert() assert html.result == ( "\n" ) # Tags html._text = "@tag: Bod\n" html.tokenizeText() html.doConvert() assert html.result == ( "\n" ) html._text = "@tag: Bod | Nobody Owens\n" html.tokenizeText() html.doConvert() assert html.result == ( "\n" ) # Multiple Keywords html._isFirst = False html.setKeywords(True) html._text = "## Chapter\n\n@pov: Bod\n@plot: Main\n@location: Europe\n\n" html.tokenizeText() html.doConvert() assert html.result == ( "This text “has dialogue” in it.
\n" ) # Alt. Dialogue CONFIG.altDialogOpen = "::" CONFIG.altDialogClose = "::" html.setDialogueHighlight(True) html._text = "## Chapter\n\nThis text ::has alt dialogue:: in it.\n\n" html.tokenizeText() html.doConvert() assert html.result == ( "This text ::has alt dialogue:: in it.
\n" ) # Footnotes # ========= html._text = ( "Text with one[footnote:fa] or two[footnote:fb] footnotes.\n\n" "%footnote.fa: Footnote text A.\n\n" ) html.tokenizeText() html.doConvert() assert html.result == ( "Text with one1 " "or twoERR footnotes.
\n" ) html.appendFootnotes() assert html.result == ( "Text with one1 " "or twoERR footnotes.
\n" "Footnote text A.
Text "
"text text text.
Text "
"text text text.
Text text text text.
\n" ) @pytest.mark.core def testFmtToHtml_ConvertDirect(mockGUI): """Test the converter directly using the ToHtml class.""" project = NWProject() html = ToHtml(project) html.initDocument() html._isNovel = True html._handle = "0000000000000" html.setLinkHeadings(True) # Special Titles # ============== # Title html._blocks = [ (BlockTyp.TITLE, 1, "A Title", [], BlockFmt.PBB | BlockFmt.CENTRE), ] html.doConvert() assert html.result == ( "* * *
\n" # Skip html._blocks = [ (BlockTyp.SKIP, 1, "", [], BlockFmt.NONE), ] html.doConvert() assert html.result == "\n" # Alignment # ========= html.setLinkHeadings(False) # Align Left html.setStyles(False) html._blocks = [ (BlockTyp.HEAD1, 1, "A Title", [], BlockFmt.LEFT), ] html.doConvert() assert html.result == ( "
Some text ...
\n" ) # Indent Right html._blocks = [ (BlockTyp.TEXT, 1, "Some text ...", [], BlockFmt.IND_R), ] html.doConvert() assert html.result == ( "Some text ...
\n" ) # Text Indent html._blocks = [ (BlockTyp.TEXT, 1, "Some text ...", [], BlockFmt.IND_T), ] html.doConvert() assert html.result == ( "Some text ...
\n" ) @pytest.mark.core def testFmtToHtml_SpecialCases(mockGUI): """Test some special cases that have caused errors in the past.""" project = NWProject() html = ToHtml(project) html.initDocument() html._isNovel = True # Greater/Lesser than symbols # =========================== html._text = "Text with > and < with some **bold text** in it.\n" html.tokenizeText() html.doConvert() assert html.result == ( "Text with > and < with some bold text in it.
\n" ) html._text = "Text with some <**bold text**> in it.\n" html.tokenizeText() html.doConvert() assert html.result == ( "Text with some <bold text> in it.
\n" ) html._text = "Let's > be > _difficult **shall** > we_?\n" html.tokenizeText() html.doConvert() assert html.result == ( "Let's > be > difficult shall > we?
\n" ) html._text = "Test > text _<**bold**>_ and more.\n" html.tokenizeText() html.doConvert() assert html.result == ( "Test > text <bold> and more.
\n" ) # Test for issue #950 # =================== # See: https://github.com/vkbo/novelWriter/issues/950 html.setComments(True) html._text = "% Test > text _<**bold**>_ and more.\n" html.tokenizeText() html.doConvert() assert html.result == ( "" "Comment: " "Test > text <bold> " "and more." "
\n" ) html._isFirst = False html._text = "## Heading <1>\n" html.tokenizeText() html.doConvert() assert html.result == ( "Test text **bold** and more.
\n" ) @pytest.mark.core def testFmtToHtml_Save(mockGUI, fncPath): """Test the save method of the ToHtml class.""" project = NWProject() html = ToHtml(project) html.initDocument() html._isNovel = True # Build Project # ============= docText = [ "# My Novel\n**By Jane Doh**\n", "## Chapter 1\n\nThe text of chapter one.\n", "### Scene 1\n\nThe text of scene one.\n", "#### A Section\n\nMore text in scene one.\n", "## Chapter 2\n\nThe text of chapter two.\n", "### Scene 2\n\nThe text of scene two.\n", "#### A Section\n\n\tMore text in scene two.\n", ] resText = [( "By Jane Doh
\n" ), ( "The text of chapter one.
\n" ), ( "The text of scene one.
\n" ), ( "More text in scene one.
\n" ), ( "The text of chapter two.
\n" ), ( "The text of scene two.
\n" ), ( "\tMore text in scene two.
\n" )] for i in range(len(docText)): html._text = docText[i] html.doPreProcessing() html.tokenizeText() html.doConvert() assert html.result == resText[i] assert html.fullHTML == resText html.replaceTabs(nSpaces=2, spaceChar=" ") resText[6] = "More text in scene two.
\n" # Check Files # =========== # HTML hStyle = html.getStyleSheet() htmlDoc = ( "\n" "\n" "\n" "\n" "Text with <brackets> & short–dash, long—dash …
\n" ) # Auto-Replace, replace Unicode docText = "Text withText with <brackets> & short–dash, long—dash …
\n" ) # Result Size assert html.getFullResultSize() == 147 # CSS # === assert len(html.getStyleSheet()) > 1 assert "p {text-align: left;" in " ".join(html.getStyleSheet()) assert "p {text-align: justify;" not in " ".join(html.getStyleSheet()) html.setStyles(False) assert html.getStyleSheet() == []