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 == "<h1 class='title'>Title</h1>\n" # Header 2 theHtml.theText = "## Chapter Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h1>Chapter Title</h1>\n" # Header 3 theHtml.theText = "### Scene Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h2>Scene Title</h2>\n" # Header 4 theHtml.theText = "#### Section Title\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h3>Section Title</h3>\n" theHtml.isNovel = False theHtml.setLinkHeaders(True) # Header 1 theHtml.theText = "# Heading One\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h1><a name='T000001'></a>Heading One</h1>\n" # Header 2 theHtml.theText = "## Heading Two\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h2><a name='T000001'></a>Heading Two</h2>\n" # Header 3 theHtml.theText = "### Heading Three\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h3><a name='T000001'></a>Heading Three</h3>\n" # Header 4 theHtml.theText = "#### Heading Four\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == "<h4><a name='T000001'></a>Heading Four</h4>\n" # Text theHtml.theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<p>Some <strong>nested bold and <em>italic</em> and " "<del>strikethrough</del> text</strong> here</p>\n") # Text w/Hard Break theHtml.theText = "Line one \nLine two \nLine three\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<p class='break'>Line one<br/>Line two<br/>Line three</p>\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 == ( "<p class='synopsis'><strong>Synopsis:</strong> The synopsis ...</p>\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 == ( "<p class='comment'><strong>Comment:</strong> A comment ...</p>\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 == ( "<p><span class='tags'>Characters:</span> " "<a href='#tag_Bod'>Bod</a>, <a href='#tag_Jane'>Jane</a></p>\n") # Multiple Keywords theHtml.setKeywords(True) theHtml.theText = "## Chapter\n\n@pov: Bod\n@plot: Main\n@location: Europe\n\n" theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<h2>" "<a name='T000001'></a>Chapter</h2>\n" "<p style='margin-bottom: 0;'>" "<span class='tags'>Point of View:</span> <a href='#tag_Bod'>Bod</a>" "</p>\n" "<p style='margin-bottom: 0; margin-top: 0;'>" "<span class='tags'>Plot:</span> <a href='#tag_Main'>Main</a>" "</p>\n" "<p style='margin-top: 0;'>" "<span class='tags'>Locations:</span> <a href='#tag_Europe'>Europe</a>" "</p>\n") # Direct Tests # ============ theHtml.isNovel = True # Title theHtml.theTokens = [ (theHtml.T_TITLE, 1, "A Title", None, theHtml.A_PBB_AUT | theHtml.A_CENTRE), (theHtml.T_EMPTY, 1, "", None, theHtml.A_NONE), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' style='text-align: center; page-break-before: auto;'>" "<a name='T000001'></a>A Title</h1>\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 == "<p class='sep'>* * *</p>\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 == "<p class='skip'> </p>\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 == ("<h1 class='title'>A Title</h1>\n") theHtml.setStyles(True) # Align Left theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_LEFT), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' style='text-align: left;'>A Title</h1>\n") # Align Right theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_RIGHT), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' style='text-align: right;'>A Title</h1>\n") # Align Centre theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_CENTRE), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' style='text-align: center;'>A Title</h1>\n") # Align Justify theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_JUSTIFY), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' style='text-align: justify;'>A Title</h1>\n") # Page Break Always theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB | theHtml.A_PBA), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' " "style='page-break-before: always; page-break-after: always;'>A Title</h1>\n" ) # Page Break Auto theHtml.theTokens = [ (theHtml.T_HEAD1, 1, "A Title", None, theHtml.A_PBB_AUT | theHtml.A_PBA_AUT), ] theHtml.doConvert() assert theHtml.theResult == ( "<h1 class='title' " "style='page-break-before: auto; page-break-after: auto;'>A Title</h1>\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 == ( "<p>Some <b>nested bold and <i>italic</i> and " "<span style='text-decoration: line-through;'>strikethrough</span> " "text</b> here</p>\n")
def loadText(self, tHandle, updateHistory=True): """Load text into the viewer from an item handle. """ tItem = self.theProject.projTree[tHandle] if tItem is None: logger.warning("Item not found") return False if tItem.itemType != nwItemType.FILE: return False logger.debug("Generating preview for item %s" % tHandle) qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) sPos = self.verticalScrollBar().value() aDoc = ToHtml(self.theProject, self.theParent) aDoc.setPreview(True, self.mainConf.viewComments, self.mainConf.viewSynopsis) aDoc.setLinkHeaders(True) # Be extra careful here to prevent crashes when first opening a # project as a crash here leaves no way of recovering. # See issue #298 try: aDoc.setText(tHandle) aDoc.doAutoReplace() aDoc.tokenizeText() aDoc.doConvert() aDoc.doPostProcessing() except Exception as e: logger.error( "Failed to generate preview for document with handle '%s'" % tHandle) logger.error(str(e)) self.setText("An error occurred while generating the preview.") return False # Refresh the tab stops if self.mainConf.verQtValue >= 51000: self.setTabStopDistance(self.mainConf.getTabWidth()) else: self.setTabStopWidth(self.mainConf.getTabWidth()) # Must be before setHtml if updateHistory: self.docHistory.append(tHandle) self.setHtml(aDoc.theResult.replace("\t", "!!tab!!")) self.setDocumentTitle(tHandle) # Loop through the text and put back in the tabs. Tabs are removed by # the setHtml function, so the ToHtml class puts in a placeholder. while self.find("!!tab!!"): theCursor = self.textCursor() theCursor.insertText("\t") if self.theHandle == tHandle: self.verticalScrollBar().setValue(sPos) self.theHandle = tHandle self.theProject.setLastViewed(tHandle) self.docHeader.setTitleFromHandle(self.theHandle) self.updateDocMargins() # Make sure the main GUI knows we changed the content self.theParent.viewMeta.refreshReferences(tHandle) # Since we change the content while it may still be rendering, we mark # the document dirty again to make sure it's re-rendered properly. self.redrawText() qApp.restoreOverrideCursor() return True