# -*- coding: utf-8 -*- """novelWriter ToHtml Class Tester """ import pytest from nw.core import NWProject, NWIndex, ToHtml @pytest.mark.core def testCoreToHtml_Format(dummyGUI): """Test all the formatters for the ToHtml class. """ theProject = NWProject(dummyGUI) dummyGUI.theIndex = NWIndex(theProject, dummyGUI) theHtml = ToHtml(theProject, dummyGUI) # Export Mode # =========== assert theHtml._formatSynopsis("synopsis text") == ( "

Synopsis: synopsis text

\n" ) assert theHtml._formatComments("comment text") == ( "

Comment: comment text

\n" ) assert theHtml._formatKeywords("") == "" assert theHtml._formatKeywords("tag: Jane") == ( "
Tag: Jane
" ) assert theHtml._formatKeywords("char: Bod, Jane") == ( "
" "Characters: " "Bod, " "Jane" "
" ) # Preview Mode # ============ theHtml.setPreview(True, True) assert theHtml._formatSynopsis("synopsis text") == ( "

Synopsis: synopsis text

\n" ) assert theHtml._formatComments("comment text") == ( "

comment text

\n" ) assert theHtml._formatKeywords("") == "" assert theHtml._formatKeywords("tag: Jane") == ( "
Tag: Jane
" ) assert theHtml._formatKeywords("char: Bod, Jane") == ( "
" "Characters: " "Bod, " "Jane" "
" ) # END Test testCoreToHtml_Format @pytest.mark.core def testCoreToHtml_Convert(dummyGUI): """Test the converter of the ToHtml class. """ theProject = NWProject(dummyGUI) dummyGUI.theIndex = NWIndex(theProject, dummyGUI) theHtml = ToHtml(theProject, dummyGUI) # Export Mode # =========== theHtml.isNovel = True # Header 1 theHtml.theText = "# Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Title

\n" # Header 2 theHtml.theText = "## Chapter Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Chapter Title

\n" # Header 3 theHtml.theText = "### Scene Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Scene Title

\n" # Header 4 theHtml.theText = "#### Section Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Section Title

\n" theHtml.isNovel = False theHtml.setLinkHeaders(True) # Header 1 theHtml.theText = "# Heading One\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Heading One

\n" # Header 2 theHtml.theText = "## Heading Two\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Heading Two

\n" # Header 3 theHtml.theText = "### Heading Three\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Heading Three

\n" # Header 4 theHtml.theText = "#### Heading Four\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "

Heading Four

\n" # Text theHtml.theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Some nested bold and italic and " "strikethrough text here

\n" ) # Text w/Hard Break theHtml.theText = "Line one \nLine two \nLine three\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Line one
Line two
Line three

\n" ) # Synopsis theHtml.theText = "%synopsis: The synopsis ...\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "" theHtml.setSynopsis(True) theHtml.theText = "%synopsis: The synopsis ...\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Synopsis: The synopsis ...

\n" ) # Comment theHtml.theText = "% A comment ...\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "" theHtml.setComments(True) theHtml.theText = "% A comment ...\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Comment: A comment ...

\n" ) # Keywords theHtml.theText = "@char: Bod, Jane\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "" theHtml.setKeywords(True) theHtml.theText = "@char: Bod, Jane\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "
Characters: " "Bod, Jane
" ) # Direct Tests # ============ theHtml.isNovel = True # Title theHtml.theTokens = [ (theHtml.T_TITLE, 1, "A Title", None, theHtml.A_PBB_NO | theHtml.A_CENTRE), (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE), ] theHtml.doConvert() assert theHtml.theResult == ( "

" "A Title

\n" ) # Separator theHtml.theTokens = [ (theHtml.T_SEP, 1, "* * *", None, theHtml.A_CENTRE), (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE), ] theHtml.doConvert() assert theHtml.theResult == "

* * *

\n" # Skip theHtml.theTokens = [ (theHtml.T_SKIP, 1, "", None, theHtml.A_NONE), (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE), ] theHtml.doConvert() assert theHtml.theResult == "

 

\n" # Styles # ====== theHtml.setLinkHeaders(False) # Align Left theHtml.setStyles(False) theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_LEFT), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) theHtml.setStyles(True) # Align Left theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_LEFT), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Align Right theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_RIGHT), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Align Centre theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_CENTRE), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Align Justify theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_JUSTIFY), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Page Break Always theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB | theHtml.A_PBA), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Page Break Avoid theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB_AV | theHtml.A_PBA_AV), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Page Break ANever theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB_NO | theHtml.A_PBA_NO), ] theHtml.doConvert() assert theHtml.theResult == ( "

A Title

\n" ) # Preview Mode # ============ theHtml.setPreview(True, True) # Text (HTML4) theHtml.theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Some nested bold and italic and " "strikethrough " "text here

\n" ) # END Test testCoreToHtml_Convert @pytest.mark.core def testCoreToHtml_Methods(dummyGUI): """Test all the other methods of the ToHtml class. """ theProject = NWProject(dummyGUI) theHtml = ToHtml(theProject, dummyGUI) # Auto-Replace docText = "Text with & short–dash, long—dash …\n" theHtml.theText = docText theHtml.doAutoReplace() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "

Text with <brackets> & short–dash, long—dash …

\n" ) # Revert on MD assert theHtml.theMarkdown == ( "Text with <brackets> & short–dash, long—dash …\n\n" ) theHtml.doPostProcessing() assert theHtml.theMarkdown == docText + "\n" # With Preview, No Revert theHtml.setPreview(True, True) theHtml.theText = docText theHtml.doAutoReplace() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theMarkdown == ( "Text with <brackets> & short–dash, long—dash …\n\n" ) theHtml.doPostProcessing() assert theHtml.theMarkdown == ( "Text with <brackets> & short–dash, long—dash …\n\n" ) # CSS # === assert len(theHtml.getStyleSheet()) > 1 assert "p {text-align: left;}" in theHtml.getStyleSheet() assert "p {text-align: justify;}" not in theHtml.getStyleSheet() theHtml.setJustify(True) assert "p {text-align: left;}" not in theHtml.getStyleSheet() assert "p {text-align: justify;}" in theHtml.getStyleSheet() theHtml.setStyles(False) assert theHtml.getStyleSheet() == [] # END Test testCoreToHtml_Methods