def testCoreToOdt_Init(mockGUI): """Test initialisation of the ODT document. """ theProject = NWProject(mockGUI) mockGUI.theIndex = NWIndex(theProject) # Flat Doc # ======== theDoc = ToOdt(theProject, isFlat=True) theDoc.initDocument() # Document XML assert theDoc._dFlat is not None assert theDoc._dCont is None assert theDoc._dMeta is None assert theDoc._dStyl is None # Content XML assert theDoc._xMeta is not None assert theDoc._xFont is not None assert theDoc._xFnt2 is None assert theDoc._xStyl is not None assert theDoc._xAuto is not None assert theDoc._xAut2 is None assert theDoc._xMast is not None assert theDoc._xBody is not None assert theDoc._xText is not None # ODT Doc # ======= theDoc = ToOdt(theProject, isFlat=False) theDoc.initDocument() # Document XML assert theDoc._dFlat is None assert theDoc._dCont is not None assert theDoc._dMeta is not None assert theDoc._dStyl is not None # Content XML assert theDoc._xMeta is not None assert theDoc._xFont is not None assert theDoc._xFnt2 is not None assert theDoc._xStyl is not None assert theDoc._xAuto is not None assert theDoc._xAut2 is not None assert theDoc._xMast is not None assert theDoc._xBody is not None assert theDoc._xText is not None
def testCoreToOdt_Format(mockGUI): """Test the formatters for the ToOdt class. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=True) assert theDoc._formatSynopsis("synopsis text") == ( "**Synopsis:** synopsis text", "_B b_ ") assert theDoc._formatComments("comment text") == ( "**Comment:** comment text", "_B b_ ") assert theDoc._formatKeywords("") == "" assert theDoc._formatKeywords("tag: Jane") == ("**Tag:** Jane", "_B b_ ") assert theDoc._formatKeywords("char: Bod, Jane") == ( "**Characters:** Bod, Jane", "_B b_ ")
def testCoreToOdt_SaveFull(mockGUI, fncDir, outDir, refDir): """Test the document save functions. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=False) theDoc._isNovel = True theDoc._theText = ("## Chapter One\n\n" "Text\n\n" "## Chapter Two\n\n" "Text\n\n") theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() fullFile = os.path.join(fncDir, "document.odt") theDoc.saveOpenDocText(fullFile) assert os.path.isfile(fullFile) assert zipfile.is_zipfile(fullFile) maniFile = os.path.join(outDir, "coreToOdt_SaveFull_manifest.xml") settFile = os.path.join(outDir, "coreToOdt_SaveFull_settings.xml") contFile = os.path.join(outDir, "coreToOdt_SaveFull_content.xml") metaFile = os.path.join(outDir, "coreToOdt_SaveFull_meta.xml") stylFile = os.path.join(outDir, "coreToOdt_SaveFull_styles.xml") maniComp = os.path.join(refDir, "coreToOdt_SaveFull_manifest.xml") settComp = os.path.join(refDir, "coreToOdt_SaveFull_settings.xml") contComp = os.path.join(refDir, "coreToOdt_SaveFull_content.xml") metaComp = os.path.join(refDir, "coreToOdt_SaveFull_meta.xml") stylComp = os.path.join(refDir, "coreToOdt_SaveFull_styles.xml") extaxtTo = os.path.join(outDir, "coreToOdt_SaveFull") with zipfile.ZipFile(fullFile, mode="r") as theZip: theZip.extract("META-INF/manifest.xml", extaxtTo) theZip.extract("settings.xml", extaxtTo) theZip.extract("content.xml", extaxtTo) theZip.extract("meta.xml", extaxtTo) theZip.extract("styles.xml", extaxtTo) maniOut = os.path.join(outDir, "coreToOdt_SaveFull", "META-INF", "manifest.xml") settOut = os.path.join(outDir, "coreToOdt_SaveFull", "settings.xml") contOut = os.path.join(outDir, "coreToOdt_SaveFull", "content.xml") metaOut = os.path.join(outDir, "coreToOdt_SaveFull", "meta.xml") stylOut = os.path.join(outDir, "coreToOdt_SaveFull", "styles.xml") def prettifyXml(inFile, outFile): with open(outFile, mode="wb") as fileStream: fileStream.write( etree.tostring(etree.parse(inFile), pretty_print=True, encoding="utf-8", xml_declaration=True)) prettifyXml(maniOut, maniFile) prettifyXml(settOut, settFile) prettifyXml(contOut, contFile) prettifyXml(metaOut, metaFile) prettifyXml(stylOut, stylFile) assert cmpFiles(maniFile, maniComp) assert cmpFiles(settFile, settComp) assert cmpFiles(contFile, contComp) assert cmpFiles(metaFile, metaComp, [4, 5]) assert cmpFiles(stylFile, stylComp)
def testCoreToOdt_SaveFlat(mockGUI, fncDir, outDir, refDir): """Test the document save functions. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=True) theDoc._isNovel = True assert theDoc.setLanguage(None) is False assert theDoc.setLanguage("nb_NO") is True theDoc.setColourHeaders(True) theDoc._theText = ("## Chapter One\n\n" "Text\n\n" "## Chapter Two\n\n" "Text\n\n") theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() flatFile = os.path.join(fncDir, "document.fodt") testFile = os.path.join(outDir, "coreToOdt_SaveFlat_document.fodt") compFile = os.path.join(refDir, "coreToOdt_SaveFlat_document.fodt") theDoc.saveFlatXML(flatFile) assert os.path.isfile(flatFile) copyfile(flatFile, testFile) assert cmpFiles(testFile, compFile, [4, 5])
def testCoreToOdt_ConvertDirect(mockGUI): """Test the converter directly using the ToOdt class to reach some otherwise hard to reach conditions. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=True) theDoc._isNovel = True # Justified theDoc = ToOdt(theProject, isFlat=True) theDoc._theTokens = [ (theDoc.T_TEXT, 1, "This is a paragraph", [], theDoc.A_JUSTIFY), (theDoc.T_EMPTY, 1, "", None, theDoc.A_NONE), ] theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert ('<style:style style:name="P1" style:family="paragraph" ' 'style:parent-style-name="Text_20_body">' '<style:paragraph-properties fo:text-align="justify"/>' '</style:style>') in xmlToText(theDoc._xAuto) assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="P1">This is a paragraph</text:p>' '</office:text>') # Page Break After theDoc = ToOdt(theProject, isFlat=True) theDoc._theTokens = [ (theDoc.T_TEXT, 1, "This is a paragraph", [], theDoc.A_PBA), (theDoc.T_EMPTY, 1, "", None, theDoc.A_NONE), ] theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert ('<style:style style:name="P1" style:family="paragraph" ' 'style:parent-style-name="Text_20_body">' '<style:paragraph-properties fo:break-after="page"/>' '</style:style>') in xmlToText(theDoc._xAuto) assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="P1">This is a paragraph</text:p>' '</office:text>')
def testCoreToOdt_Convert(mockGUI): """Test the converter of the ToOdt class. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=True) theDoc._isNovel = True def getStyle(styleName): for aSet in theDoc._autoPara.values(): if aSet[0] == styleName: return aSet[1] return None # Headers # ======= # Header 1 theDoc._theText = "# Title\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="P1" text:outline-level="1">Title</text:h>' '</office:text>') # Header 2 theDoc._theText = "## Chapter\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="P2" text:outline-level="2">Chapter</text:h>' '</office:text>') # Header 3 theDoc._theText = "### Scene\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>' '</office:text>') # Header 4 theDoc._theText = "#### Section\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_4" text:outline-level="4">Section</text:h>' '</office:text>') # Title theDoc._theText = "#! Title\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText( theDoc._xText) == ('<office:text>' '<text:h text:style-name="Title">Title</text:h>' '</office:text>') # Unnumbered chapter theDoc._theText = "##! Prologue\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="P2" text:outline-level="2">Prologue</text:h>' '</office:text>') # Paragraphs # ========== # Nested Text theDoc._theText = "Some ~~nested **bold** and _italics_ text~~ text." theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_20_body">Some ' '<text:span text:style-name="T1">nested </text:span>' '<text:span text:style-name="T2">bold</text:span>' '<text:span text:style-name="T1"> and </text:span>' '<text:span text:style-name="T3">italics</text:span>' '<text:span text:style-name="T1"> text</text:span> text.</text:p>' '</office:text>') # Hard Break theDoc._theText = "Some text.\nNext line\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_20_body">Some text.<text:line-break/>Next line</text:p>' '</office:text>') # Tab theDoc._theText = "\tItem 1\tItem 2\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_20_body"><text:tab/>Item 1<text:tab/>Item 2</text:p>' '</office:text>') # Tab in Format theDoc._theText = "Some **bold\ttext**" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_20_body">Some <text:span text:style-name="T4">' 'bold<text:tab/>text</text:span></text:p>' '</office:text>') # Multiple Spaces theDoc._theText = ("### Scene\n\n" "Hello World\n\n" "Hello World\n\n" "Hello World\n\n") theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>' '<text:p text:style-name="Text_20_body">Hello World</text:p>' '<text:p text:style-name="Text_20_body">Hello <text:s/>World</text:p>' '<text:p text:style-name="Text_20_body">Hello <text:s text:c="2"/>World</text:p>' '</office:text>') # Synopsis, Comment, Keywords theDoc._theText = ("### Scene\n\n" "@pov: Jane\n\n" "% synopsis: So it begins\n\n" "% a plain comment\n\n") theDoc.setSynopsis(True) theDoc.setComments(True) theDoc.setKeywords(True) theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>' '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">' 'Point of View:</text:span> Jane</text:p>' '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">' 'Synopsis:</text:span> So it begins</text:p>' '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">' 'Comment:</text:span> a plain comment</text:p>' '</office:text>') # Scene Separator theDoc._theText = "### Scene One\n\nText\n\n### Scene Two\n\nText" theDoc.setSceneFormat("* * *", False) theDoc.tokenizeText() theDoc.doHeaders() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="P3">* * *</text:p>' '<text:p text:style-name="Text_20_body">Text</text:p>' '<text:p text:style-name="P3">* * *</text:p>' '<text:p text:style-name="Text_20_body">Text</text:p>' '</office:text>') # Scene Break theDoc._theText = "### Scene One\n\nText\n\n### Scene Two\n\nText" theDoc.setSceneFormat("", False) theDoc.tokenizeText() theDoc.doHeaders() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_20_body"></text:p>' '<text:p text:style-name="Text_20_body">Text</text:p>' '<text:p text:style-name="Text_20_body"></text:p>' '<text:p text:style-name="Text_20_body">Text</text:p>' '</office:text>') # Paragraph Styles theDoc._theText = ("### Scene\n\n" "@pov: Jane\n" "@char: John\n" "@plot: Main\n\n" ">> Right align\n\n" "Left Align <<\n\n" ">> Centered <<\n\n" "> Left indent\n\n" "Right indent <\n\n") theDoc.setKeywords(True) theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>' '<text:p text:style-name="P4"><text:span text:style-name="T4">' 'Point of View:</text:span> Jane</text:p>' '<text:p text:style-name="P5"><text:span text:style-name="T4">' 'Characters:</text:span> John</text:p>' '<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T4">' 'Plot:</text:span> Main</text:p>' '<text:p text:style-name="P6">Right align</text:p>' '<text:p text:style-name="Text_20_body">Left Align</text:p>' '<text:p text:style-name="P3">Centered</text:p>' '<text:p text:style-name="P7">Left indent</text:p>' '<text:p text:style-name="P8">Right indent</text:p>' '</office:text>') assert getStyle("P4")._pAttr["margin-bottom"] == ["fo", "0.000cm"] assert getStyle("P5")._pAttr["margin-bottom"] == ["fo", "0.000cm"] assert getStyle("P5")._pAttr["margin-top"] == ["fo", "0.000cm"] assert getStyle("P6")._pAttr["text-align"] == ["fo", "right"] assert getStyle("P3")._pAttr["text-align"] == ["fo", "center"] assert getStyle("P7")._pAttr["margin-left"] == ["fo", "1.693cm"] assert getStyle("P8")._pAttr["margin-right"] == ["fo", "1.693cm"] # Justified theDoc._theText = ("### Scene\n\n" "Regular paragraph\n\n" "with\nbreak\n\n" "Left Align <<\n\n") theDoc.setJustify(True) theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>' '<text:p text:style-name="Text_20_body">Regular paragraph</text:p>' '<text:p text:style-name="P9">with<text:line-break/>break</text:p>' '<text:p text:style-name="P9">Left Align</text:p>' '</office:text>') assert getStyle("P9")._pAttr["text-align"] == ["fo", "left"] # Page Breaks theDoc._theText = ("## Chapter One\n\n" "Text\n\n" "## Chapter Two\n\n" "Text\n\n") theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="P2" text:outline-level="2">Chapter One</text:h>' '<text:p text:style-name="Text_20_body">Text</text:p>' '<text:h text:style-name="P2" text:outline-level="2">Chapter Two</text:h>' '<text:p text:style-name="Text_20_body">Text</text:p>' '</office:text>')
def testCoreToOdt_TextFormatting(mockGUI): """Test formatting of paragraphs. """ theProject = NWProject(mockGUI) theDoc = ToOdt(theProject, isFlat=True) theDoc.initDocument() assert xmlToText(theDoc._xText) == "<office:text/>" # Paragraph Style # =============== oStyle = ODTParagraphStyle() assert theDoc._paraStyle("stuff", oStyle) == "Standard" assert theDoc._paraStyle("Text_20_body", oStyle) == "Text_20_body" # Create new para style oStyle.setTextAlign("center") assert theDoc._paraStyle("Text_20_body", oStyle) == "P1" # Return the same style on second call assert theDoc._paraStyle("Text_20_body", oStyle) == "P1" assert list(theDoc._mainPara.keys()) == [ "Text_20_body", "Text_20_Meta", "Title", "Heading_20_1", "Heading_20_2", "Heading_20_3", "Heading_20_4", "Header" ] theKey = "071d6b2e4764749f8c78d3c1ab9099fa04c07d2d53fd3de61eb1bdf1cb4845c3" assert theDoc._autoPara[theKey][0] == "P1" assert isinstance(theDoc._autoPara[theKey][1], ODTParagraphStyle) # Paragraph Formatting # ==================== oStyle = ODTParagraphStyle() # No Text theDoc.initDocument() theDoc._addTextPar("Standard", oStyle, "") assert xmlToText( theDoc._xText) == ("<office:text>" "<text:p text:style-name=\"Standard\"></text:p>" "</office:text>") # No Format theDoc.initDocument() theDoc._addTextPar("Standard", oStyle, "Hello World") assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:p text:style-name=\"Standard\">Hello World</text:p>" "</office:text>") # Heading Level None theDoc.initDocument() theDoc._addTextPar("Standard", oStyle, "Hello World", isHead=True) assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:h text:style-name=\"Standard\">Hello World</text:h>" "</office:text>") # Heading Level 1 theDoc.initDocument() theDoc._addTextPar("Standard", oStyle, "Hello World", isHead=True, oLevel="1") assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:h text:style-name=\"Standard\" text:outline-level=\"1\">Hello World</text:h>" "</office:text>") # Formatted Text theDoc.initDocument() theTxt = "A **few** _words_ from ~~our~~ sponsor" theFmt = " _B b_ I i _S s_ " theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt) assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:p text:style-name=\"Standard\">A <text:span text:style-name=\"T1\">few</text:span> " "<text:span text:style-name=\"T2\">words</text:span> from <text:span text:style-name=\"T3" "\">our</text:span> sponsor</text:p>" "</office:text>") # Incorrectly Formatted Text theDoc.initDocument() theTxt = "A **few** _wordsXXX" theFmt = " _b b_ I XXX" theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt) assert theDoc.getErrors() == ["Unknown format tag encountered"] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:p text:style-name=\"Standard\">" "A few <text:span text:style-name=\"T2\">words</text:span>" "</text:p>" "</office:text>") # Formatted Text theDoc.initDocument() theTxt = "Hello\n\tWorld" theFmt = " " theDoc._addTextPar("Standard", oStyle, theTxt, theFmt=theFmt) assert theDoc.getErrors() == [] assert xmlToText(theDoc._xText) == ( "<office:text>" "<text:p text:style-name=\"Standard\">Hello<text:line-break/><text:tab/>World</text:p>" "</office:text>")
def _saveDocument(self, theFmt): """Save the document to various formats. """ replaceTabs = self.replaceTabs.isChecked() fileExt = "" textFmt = "" # Settings # ======== if theFmt == self.FMT_ODT: fileExt = "odt" textFmt = self.tr("Open Document") elif theFmt == self.FMT_FODT: fileExt = "fodt" textFmt = self.tr("Flat Open Document") elif theFmt == self.FMT_HTM: fileExt = "htm" textFmt = self.tr("Plain HTML") elif theFmt == self.FMT_NWD: fileExt = "nwd" textFmt = self.tr("novelWriter Markdown") elif theFmt == self.FMT_MD: fileExt = "md" textFmt = self.tr("Standard Markdown") elif theFmt == self.FMT_GH: fileExt = "md" textFmt = self.tr("GitHub Markdown") elif theFmt == self.FMT_JSON_H: fileExt = "json" textFmt = self.tr("JSON + novelWriter HTML") elif theFmt == self.FMT_JSON_M: fileExt = "json" textFmt = self.tr("JSON + novelWriter Markdown") elif theFmt == self.FMT_PDF: fileExt = "pdf" textFmt = self.tr("PDF") else: return False # Generate File Name # ================== cleanName = makeFileNameSafe(self.theProject.projName) fileName = "%s.%s" % (cleanName, fileExt) saveDir = self.mainConf.lastPath if not os.path.isdir(saveDir): saveDir = os.path.expanduser("~") savePath = os.path.join(saveDir, fileName) savePath, _ = QFileDialog.getSaveFileName( self, self.tr("Save Document As"), savePath ) if not savePath: return False self.mainConf.setLastPath(savePath) # Build and Write # =============== errMsg = "" wSuccess = False if theFmt == self.FMT_ODT: makeOdt = ToOdt(self.theProject, isFlat=False) self._doBuild(makeOdt) try: makeOdt.saveOpenDocText(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_FODT: makeOdt = ToOdt(self.theProject, isFlat=True) self._doBuild(makeOdt) try: makeOdt.saveFlatXML(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_HTM: makeHtml = ToHtml(self.theProject) self._doBuild(makeHtml) if replaceTabs: makeHtml.replaceTabs() try: makeHtml.saveHTML5(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_NWD: makeNwd = ToMarkdown(self.theProject) makeNwd.setKeepMarkdown(True) self._doBuild(makeNwd, doConvert=False) if replaceTabs: makeNwd.replaceTabs(spaceChar=" ") try: makeNwd.saveRawMarkdown(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt in (self.FMT_MD, self.FMT_GH): makeMd = ToMarkdown(self.theProject) if theFmt == self.FMT_GH: makeMd.setGitHubMarkdown() else: makeMd.setStandardMarkdown() self._doBuild(makeMd) if replaceTabs: makeMd.replaceTabs(nSpaces=4, spaceChar=" ") try: makeMd.saveMarkdown(savePath) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_JSON_H or theFmt == self.FMT_JSON_M: jsonData = { "meta": { "workingTitle": self.theProject.projName, "novelTitle": self.theProject.bookTitle, "authors": self.theProject.bookAuthors, "buildTime": self.buildTime, } } if theFmt == self.FMT_JSON_H: makeHtml = ToHtml(self.theProject) self._doBuild(makeHtml) if replaceTabs: makeHtml.replaceTabs() theBody = [] for htmlPage in makeHtml.fullHTML: theBody.append(htmlPage.rstrip("\n").split("\n")) jsonData["text"] = { "css": self.htmlStyle, "html": theBody, } elif theFmt == self.FMT_JSON_M: makeMd = ToHtml(self.theProject) makeMd.setKeepMarkdown(True) self._doBuild(makeMd, doConvert=False) if replaceTabs: makeMd.replaceTabs(spaceChar=" ") theBody = [] for nwdPage in makeMd.theMarkdown: theBody.append(nwdPage.split("\n")) jsonData["text"] = { "nwd": theBody, } try: with open(savePath, mode="w", encoding="utf-8") as outFile: outFile.write(json.dumps(jsonData, indent=2)) wSuccess = True except Exception as exc: errMsg = formatException(exc) elif theFmt == self.FMT_PDF: try: thePrinter = QPrinter() thePrinter.setOutputFormat(QPrinter.PdfFormat) thePrinter.setOrientation(QPrinter.Portrait) thePrinter.setDuplex(QPrinter.DuplexLongSide) thePrinter.setFontEmbeddingEnabled(True) thePrinter.setColorMode(QPrinter.Color) thePrinter.setOutputFileName(savePath) self.docView.document().print(thePrinter) wSuccess = True except Exception as exc: errMsg = formatException(exc) else: # If the if statements above and here match, it should not # be possible to reach this else statement. return False # pragma: no cover # Report to User # ============== if wSuccess: self.mainGui.makeAlert([ self.tr("{0} file successfully written to:").format(textFmt), savePath ], nwAlert.INFO) else: self.mainGui.makeAlert(self.tr( "Failed to write {0} file. {1}" ).format(textFmt, errMsg), nwAlert.ERROR) return wSuccess