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) mockGUI.theIndex = NWIndex(theProject) 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_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_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>')