def testCoreItem_LayoutSetter(dummyGUI): """Test the setter for all the nwItemLayout values for the NWItem class. """ theProject = NWProject(dummyGUI) theItem = NWItem(theProject) # Layout theItem.setLayout(None) assert theItem.itemLayout == nwItemLayout.NO_LAYOUT theItem.setLayout("NONSENSE") assert theItem.itemLayout == nwItemLayout.NO_LAYOUT theItem.setLayout("NO_LAYOUT") assert theItem.itemLayout == nwItemLayout.NO_LAYOUT theItem.setLayout("TITLE") assert theItem.itemLayout == nwItemLayout.TITLE theItem.setLayout("BOOK") assert theItem.itemLayout == nwItemLayout.BOOK theItem.setLayout("PAGE") assert theItem.itemLayout == nwItemLayout.PAGE theItem.setLayout("PARTITION") assert theItem.itemLayout == nwItemLayout.PARTITION theItem.setLayout("UNNUMBERED") assert theItem.itemLayout == nwItemLayout.UNNUMBERED theItem.setLayout("CHAPTER") assert theItem.itemLayout == nwItemLayout.CHAPTER theItem.setLayout("SCENE") assert theItem.itemLayout == nwItemLayout.SCENE theItem.setLayout("NOTE") assert theItem.itemLayout == nwItemLayout.NOTE theItem.setLayout(nwItemLayout.NOTE) assert theItem.itemLayout == nwItemLayout.NOTE
def testCoreToHtml_Methods(dummyGUI): """Test all the other methods of the ToHtml class. """ theProject = NWProject(dummyGUI) theHtml = ToHtml(theProject, dummyGUI) theHtml.setKeepMarkdown(True) # Auto-Replace, keep Unicode docText = "Text with <brackets> & short–dash, long—dash …\n" theHtml.theText = docText theHtml.setReplaceUnicode(False) theHtml.doPreProcessing() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<p>Text with <brackets> & short–dash, long—dash …</p>\n") # Auto-Replace, replace Unicode docText = "Text with <brackets> & short–dash, long—dash …\n" theHtml.theText = docText theHtml.setReplaceUnicode(True) theHtml.doPreProcessing() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<p>Text with <brackets> & short–dash, long—dash …</p>\n" ) # With Preview theHtml.setPreview(True, True) theHtml.theText = docText theHtml.doPreProcessing() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theMarkdown[-1] == ( "Text with <brackets> & short–dash, long—dash …\n\n" ) theHtml.doPostProcessing() assert theHtml.theMarkdown[-1] == ( "Text with <brackets> & short–dash, long—dash …\n\n" ) # Result Size assert theHtml.getFullResultSize() == 147 # CSS # === assert len(theHtml.getStyleSheet()) > 1 assert "p {text-align: left;" in " ".join(theHtml.getStyleSheet()) assert "p {text-align: justify;" not in " ".join(theHtml.getStyleSheet()) theHtml.setJustify(True) assert "p {text-align: left;" not in " ".join(theHtml.getStyleSheet()) assert "p {text-align: justify;" in " ".join(theHtml.getStyleSheet()) theHtml.setStyles(False) assert theHtml.getStyleSheet() == []
def testCoreDocument_Methods(monkeypatch, dummyGUI, nwMinimal): """Test other methods of the NWDoc class. """ theProject = NWProject(dummyGUI) assert theProject.openProject(nwMinimal) assert theProject.projPath == nwMinimal theDoc = NWDoc(theProject, dummyGUI) sHandle = "8c659a11cd429" docPath = os.path.join(nwMinimal, "content", sHandle+".nwd") assert theDoc.openDocument(sHandle) == "### New Scene\n\n" # Check location assert theDoc.getFileLocation() == docPath # Check the item assert theDoc.getCurrentItem() is not None assert theDoc.getCurrentItem().itemHandle == sHandle # Check the meta theName, theParent, theClass, theLayout = theDoc.getMeta() assert theName == "New Scene" assert theParent == "a6d311a93600a" assert theClass == nwItemClass.NOVEL assert theLayout == nwItemLayout.SCENE # Add meta data garbage assert theDoc.saveDocument("%%~ stuff\n### Test File\n\nText ...\n\n") with open(docPath, mode="r", encoding="utf8") as inFile: assert inFile.read() == ( "%%~name: New Scene\n" f"%%~path: a6d311a93600a/{sHandle}\n" "%%~kind: NOVEL/SCENE\n" "%%~ stuff\n" "### Test File\n\n" "Text ...\n\n" ) assert theDoc.openDocument(sHandle) == "### Test File\n\nText ...\n\n"
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 <brackets> & short–dash, long—dash …\n" theHtml.theText = docText theHtml.doAutoReplace() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == ( "<p>Text with <brackets> & short–dash, long—dash …</p>\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() == []
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") == ( "<p class='synopsis'><strong>Synopsis:</strong> synopsis text</p>\n") assert theHtml._formatComments("comment text") == ( "<p class='comment'><strong>Comment:</strong> comment text</p>\n") assert theHtml._formatKeywords("") == "" assert theHtml._formatKeywords("tag: Jane") == ( "<div><span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a></div>\n" ) assert theHtml._formatKeywords("char: Bod, Jane") == ( "<div>" "<span class='tags'>Characters:</span> " "<a href='#tag_Bod'>Bod</a>, " "<a href='#tag_Jane'>Jane</a>" "</div>\n") # Preview Mode # ============ theHtml.setPreview(True, True) assert theHtml._formatSynopsis("synopsis text") == ( "<p class='comment'><span class='synopsis'>Synopsis:</span> synopsis text</p>\n" ) assert theHtml._formatComments("comment text") == ( "<p class='comment'>comment text</p>\n") assert theHtml._formatKeywords("") == "" assert theHtml._formatKeywords("tag: Jane") == ( "<div><span class='tags'>Tag:</span> <a name='tag_Jane'>Jane</a></div>\n" ) assert theHtml._formatKeywords("char: Bod, Jane") == ( "<div>" "<span class='tags'>Characters:</span> " "<a href='#char=Bod'>Bod</a>, " "<a href='#char=Jane'>Jane</a>" "</div>\n")
def testCoreOptions_SetGet(monkeypatch, dummyGUI, tmpDir): """Test setting and getting values from the OptionState class. """ theProject = NWProject(dummyGUI) theOpts = OptionState(theProject) # Set invalid values assert not theOpts.setValue("DummyGroup", "dummyItem", None) assert not theOpts.setValue("GuiBuildNovel", "dummyItem", None) # Set valid value assert theOpts.setValue("GuiBuildNovel", "winWidth", 100) # Set some values of different types assert theOpts.setValue("GuiBuildNovel", "winWidth", 100) assert theOpts.setValue("GuiBuildNovel", "winHeight", 12.34) assert theOpts.setValue("GuiBuildNovel", "addNovel", True) assert theOpts.setValue("GuiBuildNovel", "textFont", "Cantarell") # Generic get, doesn't check type assert theOpts.getValue("GuiBuildNovel", "winWidth", None) == 100 assert theOpts.getValue("GuiBuildNovel", "winHeight", None) == 12.34 assert theOpts.getValue("GuiBuildNovel", "addNovel", None) is True assert theOpts.getValue("GuiBuildNovel", "textFont", None) == "Cantarell" assert theOpts.getValue("GuiBuildNovel", "dummyItem", None) is None # Get type-specific assert theOpts.getString("GuiBuildNovel", "winWidth", None) == "100" assert theOpts.getString("GuiBuildNovel", "dummyItem", None) is None assert theOpts.getInt("GuiBuildNovel", "winWidth", None) == 100 assert theOpts.getInt("GuiBuildNovel", "textFont", None) is None assert theOpts.getInt("GuiBuildNovel", "dummyItem", None) is None assert theOpts.getFloat("GuiBuildNovel", "winWidth", None) == 100.0 assert theOpts.getFloat("GuiBuildNovel", "textFont", None) is None assert theOpts.getFloat("GuiBuildNovel", "dummyItem", None) is None assert theOpts.getBool("GuiBuildNovel", "addNovel", None) is True assert theOpts.getBool("GuiBuildNovel", "dummyItem", None) is None # Check integer validators assert theOpts.validIntRange(5, 0, 9, 3) == 5 assert theOpts.validIntRange(5, 0, 4, 3) == 3 assert theOpts.validIntRange(5, 0, 5, 3) == 5 assert theOpts.validIntRange(0, 0, 5, 3) == 0 assert theOpts.validIntTuple(0, (0, 1, 2), 3) == 0 assert theOpts.validIntTuple(5, (0, 1, 2), 3) == 3
def testCoreItem_ClassSetter(dummyGUI): """Test the setter for all the nwItemClass values for the NWItem class. """ theProject = NWProject(dummyGUI) theItem = NWItem(theProject) # Class theItem.setClass(None) assert theItem.itemClass == nwItemClass.NO_CLASS theItem.setClass("NONSENSE") assert theItem.itemClass == nwItemClass.NO_CLASS theItem.setClass("NO_CLASS") assert theItem.itemClass == nwItemClass.NO_CLASS theItem.setClass("NOVEL") assert theItem.itemClass == nwItemClass.NOVEL theItem.setClass("PLOT") assert theItem.itemClass == nwItemClass.PLOT theItem.setClass("CHARACTER") assert theItem.itemClass == nwItemClass.CHARACTER theItem.setClass("WORLD") assert theItem.itemClass == nwItemClass.WORLD theItem.setClass("TIMELINE") assert theItem.itemClass == nwItemClass.TIMELINE theItem.setClass("OBJECT") assert theItem.itemClass == nwItemClass.OBJECT theItem.setClass("ENTITY") assert theItem.itemClass == nwItemClass.ENTITY theItem.setClass("CUSTOM") assert theItem.itemClass == nwItemClass.CUSTOM theItem.setClass("ARCHIVE") assert theItem.itemClass == nwItemClass.ARCHIVE theItem.setClass("TRASH") assert theItem.itemClass == nwItemClass.TRASH theItem.setClass(nwItemClass.NOVEL) assert theItem.itemClass == nwItemClass.NOVEL
def testCoreItem_TypeSetter(dummyGUI): """Test the setter for all the nwItemType values for the NWItem class. """ theProject = NWProject(dummyGUI) theItem = NWItem(theProject) # Type theItem.setType(None) assert theItem.itemType == nwItemType.NO_TYPE theItem.setType("NONSENSE") assert theItem.itemType == nwItemType.NO_TYPE theItem.setType("NO_TYPE") assert theItem.itemType == nwItemType.NO_TYPE theItem.setType("ROOT") assert theItem.itemType == nwItemType.ROOT theItem.setType("FOLDER") assert theItem.itemType == nwItemType.FOLDER theItem.setType("FILE") assert theItem.itemType == nwItemType.FILE theItem.setType("TRASH") assert theItem.itemType == nwItemType.TRASH theItem.setType(nwItemType.ROOT) assert theItem.itemType == nwItemType.ROOT
def testCoreToken_Headers(dummyGUI): """Test the header and page parser of the Tokenizer class. """ theProject = NWProject(dummyGUI) theProject.projLang = "en" theProject._loadProjectLocalisation() theToken = Tokenizer(theProject, dummyGUI) # Nothing theToken.theText = "Some text ...\n" assert theToken.doHeaders() is True theToken.isNone = True assert theToken.doHeaders() is False theToken.isNone = False assert theToken.doHeaders() is True theToken.isNote = True assert theToken.doHeaders() is False theToken.isNote = False ## # Novel ## theToken.isNovel = True # Titles # ====== # H1: Title theToken.theText = "# Novel Title\n" theToken.setTitleFormat(r"T: %title%") theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD1, 1, "T: Novel Title", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # Chapters # ======== # H2: Chapter theToken.theText = "## Chapter One\n" theToken.setChapterFormat(r"C: %title%") theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "C: Chapter One", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H2: Unnumbered Chapter theToken.theText = "## Chapter One\n" theToken.setUnNumberedFormat(r"U: %title%") theToken.isUnNum = True theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "U: Chapter One", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H2: Unnumbered Chapter with Star theToken.theText = "## *Prologue\n" theToken.setUnNumberedFormat(r"U: %title%") theToken.isUnNum = False theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "U: Prologue", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H2: Chapter Word Number theToken.theText = "## Chapter\n" theToken.setChapterFormat(r"Chapter %chw%") theToken.numChapter = 0 theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "Chapter One", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H2: Chapter Roman Number Upper Case theToken.theText = "## Chapter\n" theToken.setChapterFormat(r"Chapter %chI%") theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "Chapter II", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H2: Chapter Roman Number Lower Case theToken.theText = "## Chapter\n" theToken.setChapterFormat(r"Chapter %chi%") theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "Chapter iii", None, Tokenizer.A_PBB), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # Scenes # ====== # H3: Scene w/Title theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"S: %title%", False) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD3, 1, "S: Scene One", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene Hidden wo/Format theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"", True) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene wo/Format, first theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"", False) theToken.firstScene = True theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene wo/Format, not first theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"", False) theToken.firstScene = False theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_SKIP, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene Separator, first theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"* * *", False) theToken.firstScene = True theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene Separator, not first theToken.theText = "### Scene One\n" theToken.setSceneFormat(r"* * *", False) theToken.firstScene = False theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_SEP, 1, "* * *", None, Tokenizer.A_CENTRE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene w/Absolute Number theToken.theText = "### A Scene\n" theToken.setSceneFormat(r"Scene %sca%", False) theToken.numAbsScene = 0 theToken.numChScene = 0 theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD3, 1, "Scene 1", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H3: Scene w/Chapter Number theToken.theText = "### A Scene\n" theToken.setSceneFormat(r"Scene %ch%.%sc%", False) theToken.numAbsScene = 0 theToken.numChScene = 1 theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD3, 1, "Scene 3.2", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # Sections # ======== # H4: Section Hidden wo/Format theToken.theText = "#### A Section\n" theToken.setSectionFormat(r"", True) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H4: Section Visible wo/Format theToken.theText = "#### A Section\n" theToken.setSectionFormat(r"", False) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_SKIP, 1, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H4: Section w/Format theToken.theText = "#### A Section\n" theToken.setSectionFormat(r"X: %title%", False) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD4, 1, "X: A Section", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # H4: Section Separator theToken.theText = "#### A Section\n" theToken.setSectionFormat(r"* * *", False) theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_SEP, 1, "* * *", None, Tokenizer.A_CENTRE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] # Check the first scene detector assert theToken.firstScene is False theToken.firstScene = True assert theToken.firstScene is True theToken.theText = "Some text ...\n" theToken.tokenizeText() theToken.doHeaders() assert theToken.firstScene is False ## # Title or Partition ## theToken.isNovel = False # H1: Title theToken.theText = "# Novel Title\n" theToken.tokenizeText() theToken.isTitle = True theToken.isPart = False theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_TITLE, 1, "Novel Title", None, Tokenizer.A_PBB_AUT | Tokenizer.A_CENTRE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_PBA | Tokenizer.A_CENTRE), ] # H1: Partition theToken.theText = "# Partition Title\n" theToken.setTitleFormat(r"T: %title%") theToken.tokenizeText() theToken.isTitle = False theToken.isPart = True theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_HEAD1, 1, "Partition Title", None, Tokenizer.A_PBB | Tokenizer.A_CENTRE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_PBA | Tokenizer.A_CENTRE), ] ## # Page ## theToken.isNovel = False theToken.isTitle = False theToken.isPart = False theToken.isPage = True # Some Page Text theToken.theText = "Page text\n\nMore text\n" theToken.tokenizeText() theToken.doHeaders() assert theToken.theTokens == [ (Tokenizer.T_TEXT, 1, "Page text", [], Tokenizer.A_PBB | Tokenizer.A_LEFT), (Tokenizer.T_EMPTY, 2, "", None, Tokenizer.A_LEFT), (Tokenizer.T_TEXT, 3, "More text", [], Tokenizer.A_LEFT), (Tokenizer.T_EMPTY, 3, "", None, Tokenizer.A_LEFT), ]
def testCoreToken_Setters(dummyGUI): """Test all the setters for the Tokenizer class. """ theProject = NWProject(dummyGUI) theToken = Tokenizer(theProject, dummyGUI) # Verify defaults assert theToken.fmtTitle == "%title%" assert theToken.fmtChapter == "%title%" assert theToken.fmtUnNum == "%title%" assert theToken.fmtScene == "%title%" assert theToken.fmtSection == "%title%" assert theToken.textFont == "Serif" assert theToken.textSize == 11 assert theToken.textFixed is False assert theToken.lineHeight == 1.15 assert theToken.doJustify is False assert theToken.marginTitle == (1.000, 0.500) assert theToken.marginHead1 == (1.000, 0.500) assert theToken.marginHead2 == (0.834, 0.500) assert theToken.marginHead3 == (0.584, 0.500) assert theToken.marginHead4 == (0.584, 0.500) assert theToken.marginText == (0.000, 0.584) assert theToken.marginMeta == (0.000, 0.584) assert theToken.hideScene is False assert theToken.hideSection is False assert theToken.linkHeaders is False assert theToken.doBodyText is True assert theToken.doSynopsis is False assert theToken.doComments is False assert theToken.doKeywords is False # Set new values theToken.setTitleFormat("T: %title%") theToken.setChapterFormat("C: %title%") theToken.setUnNumberedFormat("U: %title%") theToken.setSceneFormat("S: %title%", True) theToken.setSectionFormat("X: %title%", True) theToken.setFont("Monospace", 10, True) theToken.setLineHeight(2) theToken.setJustify(True) theToken.setTitleMargins(2.0, 2.0) theToken.setHead1Margins(2.0, 2.0) theToken.setHead2Margins(2.0, 2.0) theToken.setHead3Margins(2.0, 2.0) theToken.setHead4Margins(2.0, 2.0) theToken.setTextMargins(2.0, 2.0) theToken.setMetaMargins(2.0, 2.0) theToken.setLinkHeaders(True) theToken.setBodyText(False) theToken.setSynopsis(True) theToken.setComments(True) theToken.setKeywords(True) # Check new values assert theToken.fmtTitle == "T: %title%" assert theToken.fmtChapter == "C: %title%" assert theToken.fmtUnNum == "U: %title%" assert theToken.fmtScene == "S: %title%" assert theToken.fmtSection == "X: %title%" assert theToken.textFont == "Monospace" assert theToken.textSize == 10 assert theToken.textFixed is True assert theToken.lineHeight == 2.0 assert theToken.doJustify is True assert theToken.marginTitle == (2.0, 2.0) assert theToken.marginHead1 == (2.0, 2.0) assert theToken.marginHead2 == (2.0, 2.0) assert theToken.marginHead3 == (2.0, 2.0) assert theToken.marginHead4 == (2.0, 2.0) assert theToken.marginText == (2.0, 2.0) assert theToken.marginMeta == (2.0, 2.0) assert theToken.hideScene is True assert theToken.hideSection is True assert theToken.linkHeaders is True assert theToken.doBodyText is False assert theToken.doSynopsis is True assert theToken.doComments is True assert theToken.doKeywords is True
def testCoreToken_Tokenize(dummyGUI): """Test the tokenization of the Tokenizer class. """ theProject = NWProject(dummyGUI) theToken = Tokenizer(theProject, dummyGUI) theToken.setKeepMarkdown(True) # Header 1 theToken.theText = "# Novel Title\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_HEAD1, 1, "Novel Title", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "# Novel Title\n\n" # Header 2 theToken.theText = "## Chapter One\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_HEAD2, 1, "Chapter One", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "## Chapter One\n\n" # Header 3 theToken.theText = "### Scene One\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_HEAD3, 1, "Scene One", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "### Scene One\n\n" # Header 4 theToken.theText = "#### A Section\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_HEAD4, 1, "A Section", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "#### A Section\n\n" # Comment theToken.theText = "% A comment\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_COMMENT, 1, "A comment", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "\n" theToken.setComments(True) theToken.tokenizeText() assert theToken.theMarkdown[-1] == "% A comment\n\n" # Symopsis theToken.theText = "%synopsis: The synopsis\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_SYNOPSIS, 1, "The synopsis", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] theToken.theText = "% synopsis: The synopsis\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_SYNOPSIS, 1, "The synopsis", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "\n" theToken.setSynopsis(True) theToken.tokenizeText() assert theToken.theMarkdown[-1] == "% synopsis: The synopsis\n\n" # Keyword theToken.theText = "@char: Bod\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_KEYWORD, 1, "char: Bod", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "\n" theToken.setKeywords(True) theToken.tokenizeText() assert theToken.theMarkdown[-1] == "@char: Bod\n\n" theToken.theText = "@pov: Bod\n@plot: Main\n@location: Europe\n" theToken.tokenizeText() styTop = Tokenizer.A_NONE | Tokenizer.A_Z_BTMMRG styMid = Tokenizer.A_NONE | Tokenizer.A_Z_BTMMRG | Tokenizer.A_Z_TOPMRG styBtm = Tokenizer.A_NONE | Tokenizer.A_Z_TOPMRG assert theToken.theTokens == [ (Tokenizer.T_KEYWORD, 1, "pov: Bod", None, styTop), (Tokenizer.T_KEYWORD, 2, "plot: Main", None, styMid), (Tokenizer.T_KEYWORD, 3, "location: Europe", None, styBtm), (Tokenizer.T_EMPTY, 3, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "@pov: Bod\n@plot: Main\n@location: Europe\n\n" # Text theToken.theText = "Some plain text\non two lines\n\n\n" theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_TEXT, 1, "Some plain text", [], Tokenizer.A_NONE), (Tokenizer.T_TEXT, 2, "on two lines", [], Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 3, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 4, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 4, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "Some plain text\non two lines\n\n\n\n" theToken.setBodyText(False) theToken.tokenizeText() assert theToken.theTokens == [ (Tokenizer.T_EMPTY, 3, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 4, "", None, Tokenizer.A_NONE), (Tokenizer.T_EMPTY, 4, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "\n\n\n" theToken.setBodyText(True) # Text Emphasis theToken.theText = "Some **bolded text** on this lines\n" theToken.tokenizeText() assert theToken.theTokens == [ ( Tokenizer.T_TEXT, 1, "Some **bolded text** on this lines", [ [5, 2, Tokenizer.FMT_B_B], [18, 2, Tokenizer.FMT_B_E], ], Tokenizer.A_NONE ), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "Some **bolded text** on this lines\n\n" theToken.theText = "Some _italic text_ on this lines\n" theToken.tokenizeText() assert theToken.theTokens == [ ( Tokenizer.T_TEXT, 1, "Some _italic text_ on this lines", [ [5, 1, Tokenizer.FMT_I_B], [17, 1, Tokenizer.FMT_I_E], ], Tokenizer.A_NONE ), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "Some _italic text_ on this lines\n\n" theToken.theText = "Some **_bold italic text_** on this lines\n" theToken.tokenizeText() assert theToken.theTokens == [ ( Tokenizer.T_TEXT, 1, "Some **_bold italic text_** on this lines", [ [5, 2, Tokenizer.FMT_B_B], [7, 1, Tokenizer.FMT_I_B], [24, 1, Tokenizer.FMT_I_E], [25, 2, Tokenizer.FMT_B_E], ], Tokenizer.A_NONE ), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "Some **_bold italic text_** on this lines\n\n" theToken.theText = "Some ~~strikethrough text~~ on this lines\n" theToken.tokenizeText() assert theToken.theTokens == [ ( Tokenizer.T_TEXT, 1, "Some ~~strikethrough text~~ on this lines", [ [5, 2, Tokenizer.FMT_D_B], [25, 2, Tokenizer.FMT_D_E], ], Tokenizer.A_NONE ), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == "Some ~~strikethrough text~~ on this lines\n\n" theToken.theText = "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n" theToken.tokenizeText() assert theToken.theTokens == [ ( Tokenizer.T_TEXT, 1, "Some **nested bold and _italic_ and ~~strikethrough~~ text** here", [ [5, 2, Tokenizer.FMT_B_B], [23, 1, Tokenizer.FMT_I_B], [30, 1, Tokenizer.FMT_I_E], [36, 2, Tokenizer.FMT_D_B], [51, 2, Tokenizer.FMT_D_E], [58, 2, Tokenizer.FMT_B_E], ], Tokenizer.A_NONE ), (Tokenizer.T_EMPTY, 1, "", None, Tokenizer.A_NONE), ] assert theToken.theMarkdown[-1] == ( "Some **nested bold and _italic_ and ~~strikethrough~~ text** here\n\n" )
def testCoreToken_TextOps(monkeypatch, nwMinimal, dummyGUI): """Test handling files and text in the Tokenizer class. """ theProject = NWProject(dummyGUI) theProject.projTree.setSeed(42) theProject.projLang = "en" theProject._loadProjectLocalisation() theToken = Tokenizer(theProject, dummyGUI) theToken.setKeepMarkdown(True) assert theProject.openProject(nwMinimal) sHandle = "8c659a11cd429" # Set some content to work with docText = ( "### Scene Six\n\n" "This is text with _italic text_, some **bold text**, some ~~deleted text~~, " "and some **_mixed text_** and **some _nested_ text**.\n\n" "#### Replace\n\n" "Also, replace <A> and <B>.\n\n" ) docTextR = docText.replace("<A>", "this").replace("<B>", "that") nDoc = NWDoc(theProject, dummyGUI) nDoc.openDocument(sHandle) nDoc.saveDocument(docText) nDoc.clearDocument() theProject.setAutoReplace({"A": "this", "B": "that"}) assert theProject.saveProject() # Root heading assert theToken.addRootHeading("dummy") is False assert theToken.addRootHeading(sHandle) is False assert theToken.addRootHeading("7695ce551d265") is True assert theToken.theMarkdown[-1] == "# Notes: Plot\n\n" # Set text assert theToken.setText("dummy") is False assert theToken.setText(sHandle) is True assert theToken.theText == docText with monkeypatch.context() as mp: mp.setattr("nw.constants.nwConst.MAX_DOCSIZE", 100) assert theToken.setText(sHandle, docText) is True assert theToken.theText == ( "# ERROR\n\n" "Document 'New Scene' is too big (0.00 MB). Skipping.\n\n" ) assert theToken.setText(sHandle, docText) is True assert theToken.theText == docText assert theToken.isNone is False assert theToken.isTitle is False assert theToken.isBook is False assert theToken.isPage is False assert theToken.isPart is False assert theToken.isUnNum is False assert theToken.isChap is False assert theToken.isScene is True assert theToken.isNote is False assert theToken.isNovel is True # Pre Processing theToken.doPreProcessing() assert theToken.theText == docTextR # Post Processing theToken.theResult = r"This is text with escapes: \** \~~ \__" theToken.doPostProcessing() assert theToken.theResult == "This is text with escapes: ** ~~ __" # Save File savePath = os.path.join(nwMinimal, "dump.nwd") theToken.saveRawMarkdown(savePath) assert readFile(savePath) == "# Notes: Plot\n\n"
def testCoreOptions_LoadSave(monkeypatch, dummyGUI, tmpDir): """Test loading and saving from the OptionState class. """ theProject = NWProject(dummyGUI) theOpts = OptionState(theProject) # Write a test file optFile = os.path.join(tmpDir, nwFiles.OPTS_FILE) with open(optFile, mode="w+", encoding="utf8") as outFile: json.dump( { "GuiBuildNovel": { "winWidth": 1000, "winHeight": 700, "addNovel": True, "addNotes": False, "textFont": "Cantarell", "dummyItem": None, }, "DummyGroup": { "dummyItem": None, }, }, outFile) # Load and save with no path set theProject.projMeta = None assert not theOpts.loadSettings() assert not theOpts.saveSettings() # Set path theProject.projMeta = tmpDir assert theProject.projMeta == tmpDir # Cause open() to fail monkeypatch.setattr("builtins.open", causeOSError) assert not theOpts.loadSettings() assert not theOpts.saveSettings() monkeypatch.undo() # Load proper assert theOpts.loadSettings() # Check that unwanted items have been removed assert theOpts.theState == { "GuiBuildNovel": { "winWidth": 1000, "winHeight": 700, "addNovel": True, "addNotes": False, "textFont": "Cantarell", }, } # Save proper assert theOpts.saveSettings() # Load again to check we get the values back assert theOpts.loadSettings() assert theOpts.theState == { "GuiBuildNovel": { "winWidth": 1000, "winHeight": 700, "addNovel": True, "addNotes": False, "textFont": "Cantarell", }, }
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 __init__(self): QMainWindow.__init__(self) logger.debug("Initialising GUI ...") self.setObjectName("GuiMain") self.mainConf = nw.CONFIG self.threadPool = QThreadPool() # System Info # =========== logger.info("OS: %s" % self.mainConf.osType) logger.info("Kernel: %s" % self.mainConf.kernelVer) logger.info("Host: %s" % self.mainConf.hostName) logger.info("Qt5 Version: %s (%d)" % ( self.mainConf.verQtString, self.mainConf.verQtValue) ) logger.info("PyQt5 Version: %s (%d)" % ( self.mainConf.verPyQtString, self.mainConf.verPyQtValue) ) logger.info("Python Version: %s (0x%x)" % ( self.mainConf.verPyString, self.mainConf.verPyHexVal) ) # Core Classes # ============ # Core Classes and Settings self.theTheme = GuiTheme(self) self.theProject = NWProject(self) self.theIndex = NWIndex(self.theProject, self) self.hasProject = False self.isFocusMode = False # Prepare Main Window self.resize(*self.mainConf.getWinSize()) self._updateWindowTitle() self.setWindowIcon(QIcon(self.mainConf.appIcon)) # Build the GUI # ============= # Main GUI Elements self.statusBar = GuiMainStatus(self) self.treeView = GuiProjectTree(self) self.docEditor = GuiDocEditor(self) self.viewMeta = GuiDocViewDetails(self) self.docViewer = GuiDocViewer(self) self.treeMeta = GuiItemDetails(self) self.projView = GuiOutline(self) self.projMeta = GuiOutlineDetails(self) self.mainMenu = GuiMainMenu(self) # Minor Gui Elements self.statusIcons = [] self.importIcons = [] # Project Tree View self.treePane = QWidget() self.treeBox = QVBoxLayout() self.treeBox.setContentsMargins(0, 0, 0, 0) self.treeBox.addWidget(self.treeView) self.treeBox.addWidget(self.treeMeta) self.treePane.setLayout(self.treeBox) # Splitter : Document Viewer / Document Meta self.splitView = QSplitter(Qt.Vertical) self.splitView.addWidget(self.docViewer) self.splitView.addWidget(self.viewMeta) self.splitView.setSizes(self.mainConf.getViewPanePos()) # Splitter : Document Editor / Document Viewer self.splitDocs = QSplitter(Qt.Horizontal) self.splitDocs.addWidget(self.docEditor) self.splitDocs.addWidget(self.splitView) # Splitter : Project Outlie / Outline Details self.splitOutline = QSplitter(Qt.Vertical) self.splitOutline.addWidget(self.projView) self.splitOutline.addWidget(self.projMeta) self.splitOutline.setSizes(self.mainConf.getOutlinePanePos()) # Main Tabs : Edirot / Outline self.tabWidget = QTabWidget() self.tabWidget.setTabPosition(QTabWidget.East) self.tabWidget.setStyleSheet("QTabWidget::pane {border: 0;}") self.tabWidget.addTab(self.splitDocs, "Editor") self.tabWidget.addTab(self.splitOutline, "Outline") self.tabWidget.currentChanged.connect(self._mainTabChanged) # Splitter : Project Tree / Main Tabs xCM = self.mainConf.pxInt(4) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.setContentsMargins(xCM, xCM, xCM, xCM) self.splitMain.addWidget(self.treePane) self.splitMain.addWidget(self.tabWidget) self.splitMain.setSizes(self.mainConf.getMainPanePos()) # Indices of All Splitter Widgets self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.tabWidget) self.idxEditor = self.splitDocs.indexOf(self.docEditor) self.idxViewer = self.splitDocs.indexOf(self.splitView) self.idxViewDoc = self.splitView.indexOf(self.docViewer) self.idxViewMeta = self.splitView.indexOf(self.viewMeta) self.idxTabEdit = self.tabWidget.indexOf(self.splitDocs) self.idxTabProj = self.tabWidget.indexOf(self.splitOutline) # Splitter Behaviour self.splitMain.setCollapsible(self.idxTree, False) self.splitMain.setCollapsible(self.idxMain, False) self.splitDocs.setCollapsible(self.idxEditor, False) self.splitDocs.setCollapsible(self.idxViewer, True) self.splitView.setCollapsible(self.idxViewDoc, False) self.splitView.setCollapsible(self.idxViewMeta, False) # Editor / Viewer Default State self.splitView.setVisible(False) self.docEditor.closeSearch() # Initialise the Project Tree self.treeView.itemSelectionChanged.connect(self._treeSingleClick) self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) self.rebuildTree() # Set Main Window Elements self.setMenuBar(self.mainMenu) self.setCentralWidget(self.splitMain) self.setStatusBar(self.statusBar) # Finalise Initialisation # ======================= # Set Up Auto-Save Project Timer self.asProjTimer = QTimer() self.asProjTimer.timeout.connect(self._autoSaveProject) # Set Up Auto-Save Document Timer self.asDocTimer = QTimer() self.asDocTimer.timeout.connect(self._autoSaveDocument) # Shortcuts and Actions self._connectMenuActions() keyReturn = QShortcut(self.treeView) keyReturn.setKey(QKeySequence(Qt.Key_Return)) keyReturn.activated.connect(self._treeKeyPressReturn) keyEscape = QShortcut(self) keyEscape.setKey(QKeySequence(Qt.Key_Escape)) keyEscape.activated.connect(self._keyPressEscape) # Forward Functions self.setStatus = self.statusBar.setStatus self.setProjectStatus = self.statusBar.setProjectStatus # Force a show of the GUI self.show() # Check that config loaded fine self.reportConfErr() # Initialise Main GUI self.initMain() self.asProjTimer.start() self.asDocTimer.start() self.statusBar.clearStatus() # Handle Windows Mode self.showNormal() if self.mainConf.isFullScreen: self.toggleFullScreenMode() logger.debug("GUI initialisation complete") # Check if a project path was provided at command line, and if # not, open the project manager instead. if self.mainConf.cmdOpen is not None: logger.debug("Opening project from additional command line option") self.openProject(self.mainConf.cmdOpen) else: if self.mainConf.showGUI: self.showProjectLoadDialog() # Show the latest release notes, if they haven't been shown before if hexToInt(self.mainConf.lastNotes) < hexToInt(nw.__hexversion__): if self.mainConf.showGUI: self.showAboutNWDialog(showNotes=True) self.mainConf.lastNotes = nw.__hexversion__ logger.debug("novelWriter is ready ...") self.setStatus("novelWriter is ready ...") return
def testCoreToHtml_Complex(dummyGUI, fncDir): """Test the ave method of the ToHtml class. """ theProject = NWProject(dummyGUI) theHtml = ToHtml(theProject, dummyGUI) # 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 = [ "<h1>My Novel</h1>\n<p><strong>By Jane Doh</strong></p>\n", "<h2>Chapter 1</h2>\n<p>The text of chapter one.</p>\n", "<h3>Scene 1</h3>\n<p>The text of scene one.</p>\n", "<h4>A Section</h4>\n<p>More text in scene one.</p>\n", "<h2>Chapter 2</h2>\n<p>The text of chapter two.</p>\n", "<h3>Scene 2</h3>\n<p>The text of scene two.</p>\n", "<h4>A Section</h4>\n<p>\tMore text in scene two.</p>\n", ] for i in range(len(docText)): theHtml.theText = docText[i] theHtml.doPreProcessing() theHtml.tokenizeText() theHtml.doConvert() assert theHtml.theResult == resText[i] assert theHtml.fullHTML == resText theHtml.replaceTabs(nSpaces=2, spaceChar=" ") resText[ 6] = "<h4>A Section</h4>\n<p> More text in scene two.</p>\n" # Check File # ========== theStyle = theHtml.getStyleSheet() theStyle.append("article {width: 800px; margin: 40px auto;}") htmlDoc = ("<!DOCTYPE html>\n" "<html>\n" "<head>\n" "<meta charset='utf-8'>\n" "<title></title>\n" "</head>\n" "<style>\n" "{htmlStyle:s}\n" "</style>\n" "<body>\n" "<article>\n" "{bodyText:s}\n" "</article>\n" "</body>\n" "</html>\n").format(htmlStyle="\n".join(theStyle), bodyText="".join(resText).rstrip()) saveFile = os.path.join(fncDir, "outFile.htm") theHtml.saveHTML5(saveFile) assert readFile(saveFile) == htmlDoc
class GuiMain(QMainWindow): def __init__(self): QMainWindow.__init__(self) logger.debug("Initialising GUI ...") self.setObjectName("GuiMain") self.mainConf = nw.CONFIG self.threadPool = QThreadPool() # System Info # =========== logger.info("OS: %s" % self.mainConf.osType) logger.info("Kernel: %s" % self.mainConf.kernelVer) logger.info("Host: %s" % self.mainConf.hostName) logger.info("Qt5 Version: %s (%d)" % ( self.mainConf.verQtString, self.mainConf.verQtValue) ) logger.info("PyQt5 Version: %s (%d)" % ( self.mainConf.verPyQtString, self.mainConf.verPyQtValue) ) logger.info("Python Version: %s (0x%x)" % ( self.mainConf.verPyString, self.mainConf.verPyHexVal) ) # Core Classes # ============ # Core Classes and Settings self.theTheme = GuiTheme(self) self.theProject = NWProject(self) self.theIndex = NWIndex(self.theProject, self) self.hasProject = False self.isFocusMode = False # Prepare Main Window self.resize(*self.mainConf.getWinSize()) self._updateWindowTitle() self.setWindowIcon(QIcon(self.mainConf.appIcon)) # Build the GUI # ============= # Main GUI Elements self.statusBar = GuiMainStatus(self) self.treeView = GuiProjectTree(self) self.docEditor = GuiDocEditor(self) self.viewMeta = GuiDocViewDetails(self) self.docViewer = GuiDocViewer(self) self.treeMeta = GuiItemDetails(self) self.projView = GuiOutline(self) self.projMeta = GuiOutlineDetails(self) self.mainMenu = GuiMainMenu(self) # Minor Gui Elements self.statusIcons = [] self.importIcons = [] # Project Tree View self.treePane = QWidget() self.treeBox = QVBoxLayout() self.treeBox.setContentsMargins(0, 0, 0, 0) self.treeBox.addWidget(self.treeView) self.treeBox.addWidget(self.treeMeta) self.treePane.setLayout(self.treeBox) # Splitter : Document Viewer / Document Meta self.splitView = QSplitter(Qt.Vertical) self.splitView.addWidget(self.docViewer) self.splitView.addWidget(self.viewMeta) self.splitView.setSizes(self.mainConf.getViewPanePos()) # Splitter : Document Editor / Document Viewer self.splitDocs = QSplitter(Qt.Horizontal) self.splitDocs.addWidget(self.docEditor) self.splitDocs.addWidget(self.splitView) # Splitter : Project Outlie / Outline Details self.splitOutline = QSplitter(Qt.Vertical) self.splitOutline.addWidget(self.projView) self.splitOutline.addWidget(self.projMeta) self.splitOutline.setSizes(self.mainConf.getOutlinePanePos()) # Main Tabs : Edirot / Outline self.tabWidget = QTabWidget() self.tabWidget.setTabPosition(QTabWidget.East) self.tabWidget.setStyleSheet("QTabWidget::pane {border: 0;}") self.tabWidget.addTab(self.splitDocs, "Editor") self.tabWidget.addTab(self.splitOutline, "Outline") self.tabWidget.currentChanged.connect(self._mainTabChanged) # Splitter : Project Tree / Main Tabs xCM = self.mainConf.pxInt(4) self.splitMain = QSplitter(Qt.Horizontal) self.splitMain.setContentsMargins(xCM, xCM, xCM, xCM) self.splitMain.addWidget(self.treePane) self.splitMain.addWidget(self.tabWidget) self.splitMain.setSizes(self.mainConf.getMainPanePos()) # Indices of All Splitter Widgets self.idxTree = self.splitMain.indexOf(self.treePane) self.idxMain = self.splitMain.indexOf(self.tabWidget) self.idxEditor = self.splitDocs.indexOf(self.docEditor) self.idxViewer = self.splitDocs.indexOf(self.splitView) self.idxViewDoc = self.splitView.indexOf(self.docViewer) self.idxViewMeta = self.splitView.indexOf(self.viewMeta) self.idxTabEdit = self.tabWidget.indexOf(self.splitDocs) self.idxTabProj = self.tabWidget.indexOf(self.splitOutline) # Splitter Behaviour self.splitMain.setCollapsible(self.idxTree, False) self.splitMain.setCollapsible(self.idxMain, False) self.splitDocs.setCollapsible(self.idxEditor, False) self.splitDocs.setCollapsible(self.idxViewer, True) self.splitView.setCollapsible(self.idxViewDoc, False) self.splitView.setCollapsible(self.idxViewMeta, False) # Editor / Viewer Default State self.splitView.setVisible(False) self.docEditor.closeSearch() # Initialise the Project Tree self.treeView.itemSelectionChanged.connect(self._treeSingleClick) self.treeView.itemDoubleClicked.connect(self._treeDoubleClick) self.rebuildTree() # Set Main Window Elements self.setMenuBar(self.mainMenu) self.setCentralWidget(self.splitMain) self.setStatusBar(self.statusBar) # Finalise Initialisation # ======================= # Set Up Auto-Save Project Timer self.asProjTimer = QTimer() self.asProjTimer.timeout.connect(self._autoSaveProject) # Set Up Auto-Save Document Timer self.asDocTimer = QTimer() self.asDocTimer.timeout.connect(self._autoSaveDocument) # Shortcuts and Actions self._connectMenuActions() keyReturn = QShortcut(self.treeView) keyReturn.setKey(QKeySequence(Qt.Key_Return)) keyReturn.activated.connect(self._treeKeyPressReturn) keyEscape = QShortcut(self) keyEscape.setKey(QKeySequence(Qt.Key_Escape)) keyEscape.activated.connect(self._keyPressEscape) # Forward Functions self.setStatus = self.statusBar.setStatus self.setProjectStatus = self.statusBar.setProjectStatus # Force a show of the GUI self.show() # Check that config loaded fine self.reportConfErr() # Initialise Main GUI self.initMain() self.asProjTimer.start() self.asDocTimer.start() self.statusBar.clearStatus() # Handle Windows Mode self.showNormal() if self.mainConf.isFullScreen: self.toggleFullScreenMode() logger.debug("GUI initialisation complete") # Check if a project path was provided at command line, and if # not, open the project manager instead. if self.mainConf.cmdOpen is not None: logger.debug("Opening project from additional command line option") self.openProject(self.mainConf.cmdOpen) else: if self.mainConf.showGUI: self.showProjectLoadDialog() # Show the latest release notes, if they haven't been shown before if hexToInt(self.mainConf.lastNotes) < hexToInt(nw.__hexversion__): if self.mainConf.showGUI: self.showAboutNWDialog(showNotes=True) self.mainConf.lastNotes = nw.__hexversion__ logger.debug("novelWriter is ready ...") self.setStatus("novelWriter is ready ...") return def clearGUI(self): """Wrapper function to clear all sub-elements of the main GUI. """ # Project Area self.treeView.clearTree() self.treeMeta.clearDetails() # Work Area self.docEditor.clearEditor() self.docEditor.setDictionaries() self.closeDocViewer() self.projMeta.clearDetails() # General self.statusBar.clearStatus() self._updateWindowTitle() return True def initMain(self): """Initialise elements that depend on user settings. """ self.asProjTimer.setInterval(int(self.mainConf.autoSaveProj*1000)) self.asDocTimer.setInterval(int(self.mainConf.autoSaveDoc*1000)) return True ## # Project Actions ## def newProject(self, projData=None): """Create new project via the new project wizard. """ if self.hasProject: if not self.closeProject(): self.makeAlert( "Cannot create new project when another project is open.", nwAlert.ERROR ) return False if projData is None: projData = self.showNewProjectDialog() if projData is None: return False projPath = projData.get("projPath", None) if projPath is None or projData is None: logger.error("No projData or projPath set") return False if os.path.isfile(os.path.join(projPath, self.theProject.projFile)): self.makeAlert( "A project already exists in that location. Please choose another folder.", nwAlert.ERROR ) return False logger.info("Creating new project") if self.theProject.newProject(projData): self.rebuildTree() self.saveProject() self.hasProject = True self.docEditor.setDictionaries() self.rebuildIndex(beQuiet=True) self.statusBar.setRefTime(self.theProject.projOpened) self.statusBar.setProjectStatus(True) self.statusBar.setDocumentStatus(None) self.statusBar.setStatus("New project created ...") self._updateWindowTitle(self.theProject.projName) else: self.theProject.clearProject() return False return True def closeProject(self, isYes=False): """Closes the project if one is open. isYes is passed on from the close application event so the user doesn't get prompted twice to confirm. """ if not self.hasProject: # There is no project loaded, everything OK return True if not isYes: msgYes = self.askQuestion( "Close Project", "Close the current project?<br>Changes are saved automatically." ) if not msgYes: return False if self.docEditor.docChanged: self.saveDocument() if self.theProject.projAltered: saveOK = self.saveProject() doBackup = False if self.theProject.doBackup and self.mainConf.backupOnClose: doBackup = True if self.mainConf.askBeforeBackup: msgYes = self.askQuestion( "Backup Project", "Backup the current project?" ) if not msgYes: doBackup = False if doBackup: self.theProject.zipIt(False) else: saveOK = True if saveOK: self.closeDocument() self.docViewer.clearNavHistory() self.projView.closeOutline() self.theProject.closeProject() self.theIndex.clearIndex() self.clearGUI() self.hasProject = False self.tabWidget.setCurrentWidget(self.splitDocs) return saveOK def openProject(self, projFile): """Open a project from a projFile path. """ if projFile is None: return False # Make sure any open project is cleared out first before we load # another one if not self.closeProject(): return False # Switch main tab to editor view self.tabWidget.setCurrentWidget(self.splitDocs) # Try to open the project if not self.theProject.openProject(projFile): # The project open failed. if self.theProject.lockedBy is None: # The project is not locked, so failed for some other # reason handled by the project class. return False try: lockDetails = ( "<br><br>The project was locked by the computer " "'%s' (%s %s), last active on %s" ) % ( self.theProject.lockedBy[0], self.theProject.lockedBy[1], self.theProject.lockedBy[2], datetime.fromtimestamp( int(self.theProject.lockedBy[3]) ).strftime("%x %X") ) except Exception: lockDetails = "" msgBox = QMessageBox() msgRes = msgBox.warning( self, "Project Locked", ( "The project is already open by another instance of novelWriter, and " "is therefore locked. Override lock and continue anyway?<br><br>" "Note: If the program or the computer previously crashed, the lock " "can safely be overridden. If, however, another instance of " "novelWriter has the project open, overriding the lock may corrupt " "the project, and is not recommended.%s" ) % lockDetails, QMessageBox.Yes | QMessageBox.No, QMessageBox.No ) if msgRes == QMessageBox.Yes: if not self.theProject.openProject(projFile, overrideLock=True): return False else: return False # Project is loaded self.hasProject = True # Load the tag index self.theIndex.loadIndex() # Update GUI self._updateWindowTitle(self.theProject.projName) self.rebuildTree() self.docEditor.setDictionaries() self.docEditor.setSpellCheck(self.theProject.spellCheck) self.mainMenu.setAutoOutline(self.theProject.autoOutline) self.statusBar.setRefTime(self.theProject.projOpened) self.statusBar.setStats(self.theProject.currWCount, 0) # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited, doScroll=True) if self.theProject.lastViewed is not None: self.viewDocument(self.theProject.lastViewed) # Check if we need to rebuild the index if self.theIndex.indexBroken: self.rebuildIndex() # Make sure the changed status is set to false on all that was # just opened qApp.processEvents() self.docEditor.setDocumentChanged(False) self.theProject.setProjectChanged(False) logger.debug("Project load complete") return True def saveProject(self, autoSave=False): """Save the current project. """ if not self.hasProject: logger.error("No project open") return False # If the project is new, it may not have a path, so we need one if self.theProject.projPath is None: projPath = self.selectProjectPath() self.theProject.setProjectPath(projPath) if self.theProject.projPath is None: return False self.treeView.saveTreeOrder() self.theProject.saveProject(autoSave=autoSave) self.theIndex.saveIndex() return True ## # Document Actions ## def closeDocument(self): """Close the document and clear the editor and title field. """ if not self.hasProject: logger.error("No project open") return False self.docEditor.saveCursorPosition() if self.docEditor.docChanged: self.saveDocument() self.docEditor.clearEditor() return True def openDocument(self, tHandle, tLine=None, changeFocus=True, doScroll=False): """Open a specific document, optionally at a given line. """ if not self.hasProject: logger.error("No project open") return False self.closeDocument() self.tabWidget.setCurrentWidget(self.splitDocs) if self.docEditor.loadText(tHandle, tLine): if changeFocus: self.docEditor.setFocus() self.theProject.setLastEdited(tHandle) self.treeView.setSelectedHandle(tHandle, doScroll=doScroll) else: return False return True def openNextDocument(self, tHandle, wrapAround=False): """Opens the next document in the project tree, following the document with the given handle. Stops when reaching the end. """ if not self.hasProject: logger.error("No project open") return False self.treeView.flushTreeOrder() nHandle = None # The next handle after tHandle fHandle = None # The first file handle we encounter foundIt = False # We've found tHandle, pick the next we see for tItem in self.theProject.projTree: if tItem is None: continue if tItem.itemType != nwItemType.FILE: continue if fHandle is None: fHandle = tItem.itemHandle if tItem.itemHandle == tHandle: foundIt = True elif foundIt: nHandle = tItem.itemHandle break if nHandle is not None: self.openDocument(nHandle, tLine=0, doScroll=True) return True elif wrapAround: self.openDocument(fHandle, tLine=0, doScroll=True) return False return False def saveDocument(self): """Save the current documents. """ if not self.hasProject: logger.error("No project open") return False self.docEditor.saveText() return True def viewDocument(self, tHandle=None, tAnchor=None): """Load a document for viewing in the view panel. """ if not self.hasProject: logger.error("No project open") return False if tHandle is None: logger.debug("Viewing document, but no handle provided") if self.docEditor.hasFocus(): logger.verbose("Trying editor document") tHandle = self.docEditor.theHandle if tHandle is not None: self.saveDocument() else: logger.verbose("Trying selected document") tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.verbose("Trying last viewed document") tHandle = self.theProject.lastViewed if tHandle is None: logger.verbose("No document to view, giving up") return False # Make sure main tab is in Editor view self.tabWidget.setCurrentWidget(self.splitDocs) logger.debug("Viewing document with handle %s" % tHandle) if self.docViewer.loadText(tHandle): if not self.splitView.isVisible(): bPos = self.splitMain.sizes() self.splitView.setVisible(True) vPos = [0, 0] vPos[0] = int(bPos[1]/2) vPos[1] = bPos[1] - vPos[0] self.splitDocs.setSizes(vPos) self.viewMeta.setVisible(self.mainConf.showRefPanel) self.docViewer.navigateTo(tAnchor) return True def importDocument(self): """Import the text contained in an out-of-project text file, and insert the text into the currently open document. """ if not self.hasProject: logger.error("No project open") return False lastPath = self.mainConf.lastPath extFilter = [ "Text files (*.txt)", "Markdown files (*.md)", "novelWriter files (*.nwd)", "All files (*.*)", ] dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.DontUseNativeDialog loadFile, _ = QFileDialog.getOpenFileName( self, "Import File", lastPath, options=dlgOpt, filter=";;".join(extFilter) ) if not loadFile: return False if loadFile.strip() == "": return False theText = None try: with open(loadFile, mode="rt", encoding="utf8") as inFile: theText = inFile.read() self.mainConf.setLastPath(loadFile) except Exception as e: self.makeAlert( ["Could not read file. The file must be an existing text file.", str(e)], nwAlert.ERROR ) return False if self.docEditor.theHandle is None: self.makeAlert( "Please open a document to import the text file into.", nwAlert.ERROR ) return False if not self.docEditor.isEmpty(): msgYes = self.askQuestion("Import Document", ( "Importing the file will overwrite the current content of the document. " "Do you want to proceed?" )) if not msgYes: return False self.docEditor.replaceText(theText) return True def mergeDocuments(self): """Merge multiple documents to one single new document. """ if not self.hasProject: logger.error("No project open") return False dlgMerge = GuiDocMerge(self, self.theProject) dlgMerge.exec_() return True def splitDocument(self): """Split a single document into multiple documents. """ if not self.hasProject: logger.error("No project open") return False dlgSplit = GuiDocSplit(self, self.theProject) dlgSplit.exec_() return True def passDocumentAction(self, theAction): """Pass on document action theAction to the document viewer if it has focus, otherwise pass it to the document editor. """ if self.docViewer.hasFocus(): self.docViewer.docAction(theAction) else: self.docEditor.docAction(theAction) return True ## # Tree Item Actions ## def openSelectedItem(self): """Open the selected documents. """ if not self.hasProject: logger.error("No project open") return False tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return False logger.verbose("Opening item %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, doScroll=False) else: logger.verbose("Requested item %s is not a file" % tHandle) return True def editItem(self, tHandle=None): """Open the edit item dialog. """ if not self.hasProject: logger.error("No project open") return False if tHandle is None: tHandle = self.treeView.getSelectedHandle() if tHandle is None: logger.warning("No item selected") return tItem = self.theProject.projTree[tHandle] if tItem is None: return if tItem.itemType not in nwLists.REG_TYPES: return logger.verbose("Requesting change to item %s" % tHandle) dlgProj = GuiItemEditor(self, self.theProject, tHandle) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: self.treeView.setTreeItemValues(tHandle) self.treeMeta.updateViewBox(tHandle) self.docEditor.updateDocInfo(tHandle) self.docViewer.updateDocInfo(tHandle) return def rebuildTree(self): """Rebuild the project tree. """ self._makeStatusIcons() self._makeImportIcons() self.treeView.clearTree() self.treeView.buildTree() return def rebuildIndex(self, beQuiet=False): """Rebuild the entire index. """ if not self.hasProject: logger.error("No project open") return False logger.debug("Rebuilding index ...") qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) tStart = time() self.treeView.saveTreeOrder() self.theIndex.clearIndex() theDoc = NWDoc(self.theProject, self) for nDone, tItem in enumerate(self.theProject.projTree): if tItem is not None: self.setStatus("Indexing: '%s'" % tItem.itemName) else: self.setStatus("Indexing: Unknown item") if tItem is not None and tItem.itemType == nwItemType.FILE: logger.verbose("Scanning: %s" % tItem.itemName) theText = theDoc.openDocument(tItem.itemHandle, showStatus=False) # Build tag index self.theIndex.scanText(tItem.itemHandle, theText) # Get Word Counts cC, wC, pC = self.theIndex.getCounts(tItem.itemHandle) tItem.setCharCount(cC) tItem.setWordCount(wC) tItem.setParaCount(pC) self.treeView.propagateCount(tItem.itemHandle, wC) self.treeView.projectWordCount() tEnd = time() self.setStatus("Indexing completed in %.1f ms" % ((tEnd - tStart)*1000.0)) self.docEditor.updateTagHighLighting() qApp.restoreOverrideCursor() if not beQuiet: self.makeAlert("The project index has been successfully rebuilt.", nwAlert.INFO) return True def rebuildOutline(self): """Force a rebuild of the Outline view. """ if not self.hasProject: logger.error("No project open") return False logger.verbose("Forcing a rebuild of the Project Outline") self.tabWidget.setCurrentWidget(self.splitOutline) self.projView.refreshTree(overRide=True) return True ## # Main Dialogs ## def selectProjectPath(self): """Select where to save project. """ dlgOpt = QFileDialog.Options() dlgOpt |= QFileDialog.ShowDirsOnly dlgOpt |= QFileDialog.DontUseNativeDialog projPath = QFileDialog.getExistingDirectory( self, "Save novelWriter Project", "", options=dlgOpt ) if projPath: return projPath return None def showProjectLoadDialog(self): """Opens the projects dialog for selecting either existing projects from a cache of recently opened projects, or provide a browse button for projects not yet cached. Selecting to create a new project is forwarded to the new project wizard. """ dlgProj = GuiProjectLoad(self) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: if dlgProj.openState == GuiProjectLoad.OPEN_STATE: self.openProject(dlgProj.openPath) elif dlgProj.openState == GuiProjectLoad.NEW_STATE: self.newProject() return True def showNewProjectDialog(self): """Open the wizard and assemble a project options dict. """ newProj = GuiProjectWizard(self) newProj.exec_() if newProj.result() == QDialog.Accepted: return self._assembleProjectWizardData(newProj) return None def showPreferencesDialog(self): """Open the preferences dialog. """ dlgConf = GuiPreferences(self, self.theProject) dlgConf.exec_() if dlgConf.result() == QDialog.Accepted: logger.debug("Applying new preferences") self.initMain() self.theTheme.updateTheme() self.saveDocument() self.docEditor.initEditor() self.docViewer.initViewer() self.treeView.initTree() self.projView.initOutline() self.projMeta.initDetails() return def showProjectSettingsDialog(self): """Open the project settings dialog. """ if not self.hasProject: logger.error("No project open") return dlgProj = GuiProjectSettings(self, self.theProject) dlgProj.exec_() if dlgProj.result() == QDialog.Accepted: logger.debug("Applying new project settings") self.docEditor.setDictionaries() self._updateWindowTitle(self.theProject.projName) return def showBuildProjectDialog(self): """Open the build project dialog. """ if not self.hasProject: logger.error("No project open") return dlgBuild = getGuiItem("GuiBuildNovel") if dlgBuild is None: dlgBuild = GuiBuildNovel(self, self.theProject) dlgBuild.setModal(False) dlgBuild.show() qApp.processEvents() dlgBuild.viewCachedDoc() return def showWritingStatsDialog(self): """Open the session log dialog. """ if not self.hasProject: logger.error("No project open") return dlgStats = getGuiItem("GuiWritingStats") if dlgStats is None: dlgStats = GuiWritingStats(self, self.theProject) dlgStats.setModal(False) dlgStats.show() qApp.processEvents() dlgStats.populateGUI() return def showAboutNWDialog(self, showNotes=False): """Show the about dialog for novelWriter. """ dlgAbout = GuiAbout(self) dlgAbout.setModal(True) dlgAbout.show() qApp.processEvents() dlgAbout.populateGUI() if showNotes: dlgAbout.showReleaseNotes() return def showAboutQtDialog(self): """Show the about dialog for Qt. """ msgBox = QMessageBox() msgBox.aboutQt(self, "About Qt") return def makeAlert(self, theMessage, theLevel=nwAlert.INFO): """Alert both the user and the logger at the same time. Message can be either a string or an array of strings. """ if isinstance(theMessage, list): popMsg = "<br>".join(theMessage) logMsg = theMessage else: popMsg = theMessage logMsg = [theMessage] # Write to Log if theLevel == nwAlert.INFO: for msgLine in logMsg: logger.info(msgLine) elif theLevel == nwAlert.WARN: for msgLine in logMsg: logger.warning(msgLine) elif theLevel == nwAlert.ERROR: for msgLine in logMsg: logger.error(msgLine) elif theLevel == nwAlert.BUG: for msgLine in logMsg: logger.error(msgLine) # Popup msgBox = QMessageBox() if theLevel == nwAlert.INFO: msgBox.information(self, "Information", popMsg) elif theLevel == nwAlert.WARN: msgBox.warning(self, "Warning", popMsg) elif theLevel == nwAlert.ERROR: msgBox.critical(self, "Error", popMsg) elif theLevel == nwAlert.BUG: popMsg += "<br>This is a bug!" msgBox.critical(self, "Internal Error", popMsg) return def askQuestion(self, theTitle, theQuestion): """Ask the user a Yes/No question. """ msgBox = QMessageBox() msgRes = msgBox.question(self, theTitle, theQuestion) return msgRes == QMessageBox.Yes def reportConfErr(self): """Checks if the Config module has any errors to report, and let the user know if this is the case. The Config module caches errors since it is initialised before the GUI itself. """ if self.mainConf.hasError: self.makeAlert(self.mainConf.getErrData(), nwAlert.ERROR) return True return False ## # Main Window Actions ## def closeMain(self): """Save everything, and close novelWriter. """ if self.hasProject: msgYes = self.askQuestion( "Exit", "Do you want to exit novelWriter?<br>Changes are saved automatically." ) if not msgYes: return False logger.info("Exiting novelWriter") if not self.isFocusMode: self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setDocPanePos(self.splitDocs.sizes()) self.mainConf.setOutlinePanePos(self.splitOutline.sizes()) if self.viewMeta.isVisible(): self.mainConf.setViewPanePos(self.splitView.sizes()) self.mainConf.setShowRefPanel(self.viewMeta.isVisible()) self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) if not self.mainConf.isFullScreen: self.mainConf.setWinSize(self.width(), self.height()) if self.hasProject: self.closeProject(True) self.mainConf.saveConfig() self.reportConfErr() self.mainMenu.closeHelp() qApp.quit() return True def setFocus(self, paneNo): """Switch focus to one of the three main GUI panes. """ if paneNo == 1: self.treeView.setFocus() elif paneNo == 2: self.docEditor.setFocus() elif paneNo == 3: self.docViewer.setFocus() return def closeDocEditor(self): """Close the document edit panel. This does not hide the editor. """ self.closeDocument() self.theProject.setLastEdited(None) return def closeDocViewer(self): """Close the document view panel. """ self.docViewer.clearViewer() self.theProject.setLastViewed(None) bPos = self.splitMain.sizes() self.splitView.setVisible(False) vPos = [bPos[1], 0] self.splitDocs.setSizes(vPos) return not self.splitView.isVisible() def toggleFocusMode(self): """Main GUI Focus Mode hides tree, view pane and optionally also statusbar and menu. """ if self.docEditor.theHandle is None: logger.error("No document open, so not activating Focus Mode") self.mainMenu.aFocusMode.setChecked(self.isFocusMode) return False self.isFocusMode = not self.isFocusMode self.mainMenu.aFocusMode.setChecked(self.isFocusMode) if self.isFocusMode: logger.debug("Activating Focus Mode") self.tabWidget.setCurrentWidget(self.splitDocs) else: logger.debug("Deactivating Focus Mode") isVisible = not self.isFocusMode self.treePane.setVisible(isVisible) self.statusBar.setVisible(isVisible) self.mainMenu.setVisible(isVisible) self.tabWidget.tabBar().setVisible(isVisible) hideDocFooter = self.isFocusMode and self.mainConf.hideFocusFooter self.docEditor.docFooter.setVisible(not hideDocFooter) if self.splitView.isVisible(): self.splitView.setVisible(False) elif self.docViewer.theHandle is not None: self.splitView.setVisible(True) return True def toggleFullScreenMode(self): """Main GUI full screen mode. The mode is tracked by the flag in config. This only tracks whether the window has been maximised using the internal commands, and may not be correct if the user uses the system window manager. Currently, Qt doesn't have access to the exact state of the window. """ self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) winState = self.windowState() & Qt.WindowFullScreen == Qt.WindowFullScreen if winState: logger.debug("Activated full screen mode") else: logger.debug("Deactivated full screen mode") self.mainConf.isFullScreen = winState return ## # Internal Functions ## def _connectMenuActions(self): """Connect to the main window all menu actions that need to be available also when the main menu is hidden. """ # Project self.addAction(self.mainMenu.aSaveProject) self.addAction(self.mainMenu.aExitNW) # Document self.addAction(self.mainMenu.aSaveDoc) self.addAction(self.mainMenu.aFileDetails) # Edit self.addAction(self.mainMenu.aEditUndo) self.addAction(self.mainMenu.aEditRedo) self.addAction(self.mainMenu.aEditCut) self.addAction(self.mainMenu.aEditCopy) self.addAction(self.mainMenu.aEditPaste) self.addAction(self.mainMenu.aSelectAll) self.addAction(self.mainMenu.aSelectPar) # View self.addAction(self.mainMenu.aFocusMode) self.addAction(self.mainMenu.aFullScreen) # Insert self.addAction(self.mainMenu.aInsENDash) self.addAction(self.mainMenu.aInsEMDash) self.addAction(self.mainMenu.aInsEllipsis) self.addAction(self.mainMenu.aInsQuoteLS) self.addAction(self.mainMenu.aInsQuoteRS) self.addAction(self.mainMenu.aInsQuoteLD) self.addAction(self.mainMenu.aInsQuoteRD) self.addAction(self.mainMenu.aInsMSApos) self.addAction(self.mainMenu.aInsHardBreak) self.addAction(self.mainMenu.aInsNBSpace) self.addAction(self.mainMenu.aInsThinSpace) self.addAction(self.mainMenu.aInsThinNBSpace) for mAction, _ in self.mainMenu.mInsKWItems.values(): self.addAction(mAction) # Format self.addAction(self.mainMenu.aFmtEmph) self.addAction(self.mainMenu.aFmtStrong) self.addAction(self.mainMenu.aFmtStrike) self.addAction(self.mainMenu.aFmtDQuote) self.addAction(self.mainMenu.aFmtSQuote) self.addAction(self.mainMenu.aFmtHead1) self.addAction(self.mainMenu.aFmtHead2) self.addAction(self.mainMenu.aFmtHead3) self.addAction(self.mainMenu.aFmtHead4) self.addAction(self.mainMenu.aFmtComment) self.addAction(self.mainMenu.aFmtNoFormat) # Tools self.addAction(self.mainMenu.aSpellCheck) self.addAction(self.mainMenu.aReRunSpell) self.addAction(self.mainMenu.aPreferences) # Help if self.mainConf.hasHelp and self.mainConf.hasAssistant: self.addAction(self.mainMenu.aHelpLoc) self.addAction(self.mainMenu.aHelpWeb) return True def _updateWindowTitle(self, projName=None): """Set the window title and add the project's working title. """ winTitle = self.mainConf.appName if projName is not None: winTitle += " - %s" % projName self.setWindowTitle(winTitle) return True def _autoSaveProject(self): """Triggered by the auto-save project timer to save the project. """ doSave = self.hasProject doSave &= self.theProject.projChanged doSave &= self.theProject.projPath is not None if doSave: logger.debug("Autosaving project") self.saveProject(autoSave=True) return def _autoSaveDocument(self): """Triggered by the auto-save document timer to save the document. """ if self.hasProject and self.docEditor.docChanged: logger.debug("Autosaving document") self.saveDocument() return def _makeStatusIcons(self): """Generate all the item status icons based on project settings. """ self.statusIcons = {} iPx = self.mainConf.pxInt(32) for sLabel, sCol, _ in self.theProject.statusItems: theIcon = QPixmap(iPx, iPx) theIcon.fill(QColor(*sCol)) self.statusIcons[sLabel] = QIcon(theIcon) return def _makeImportIcons(self): """Generate all the item importance icons based on project settings. """ self.importIcons = {} iPx = self.mainConf.pxInt(32) for sLabel, sCol, _ in self.theProject.importItems: theIcon = QPixmap(iPx, iPx) theIcon.fill(QColor(*sCol)) self.importIcons[sLabel] = QIcon(theIcon) return def _assembleProjectWizardData(self, newProj): """Extract the user choices from the New Project Wizard and store them in a dictionary. """ projData = { "projName": newProj.field("projName"), "projTitle": newProj.field("projTitle"), "projAuthors": newProj.field("projAuthors"), "projPath": newProj.field("projPath"), "popSample": newProj.field("popSample"), "popMinimal": newProj.field("popMinimal"), "popCustom": newProj.field("popCustom"), "addRoots": [], "numChapters": 0, "numScenes": 0, "chFolders": False, } if newProj.field("popCustom"): addRoots = [] if newProj.field("addPlot"): addRoots.append(nwItemClass.PLOT) if newProj.field("addChar"): addRoots.append(nwItemClass.CHARACTER) if newProj.field("addWorld"): addRoots.append(nwItemClass.WORLD) if newProj.field("addTime"): addRoots.append(nwItemClass.TIMELINE) if newProj.field("addObject"): addRoots.append(nwItemClass.OBJECT) if newProj.field("addEntity"): addRoots.append(nwItemClass.ENTITY) projData["addRoots"] = addRoots projData["numChapters"] = newProj.field("numChapters") projData["numScenes"] = newProj.field("numScenes") projData["chFolders"] = newProj.field("chFolders") return projData ## # Events ## def closeEvent(self, theEvent): """Capture the closing event of the GUI and call the close function to handle all the close process steps. """ if self.closeMain(): theEvent.accept() else: theEvent.ignore() return ## # Signal Handlers ## def _treeSingleClick(self): """Single click on a project tree item just updates the details panel below the tree. """ sHandle = self.treeView.getSelectedHandle() if sHandle is not None: self.treeMeta.updateViewBox(sHandle) return def _treeDoubleClick(self, tItem, colNo): """The user double-clicked an item in the tree. If it is a file, we open it. Otherwise, we do nothing. """ tHandle = tItem.data(self.treeView.C_NAME, Qt.UserRole) logger.verbose("User double clicked tree item with handle %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem is not None: if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, changeFocus=False, doScroll=False) else: logger.verbose("Requested item %s is a folder" % tHandle) return def _treeKeyPressReturn(self): """The user pressed return on an item in the tree. If it is a file, we open it. Otherwise, we do nothing. Pressing return does not change focus to the editor as double click does. """ tHandle = self.treeView.getSelectedHandle() logger.verbose("User pressed return on tree item with handle %s" % tHandle) nwItem = self.theProject.projTree[tHandle] if nwItem is not None: if nwItem.itemType == nwItemType.FILE: logger.verbose("Requested item %s is a file" % tHandle) self.openDocument(tHandle, changeFocus=False, doScroll=False) else: logger.verbose("Requested item %s is a folder" % tHandle) return def _keyPressEscape(self): """When the escape key is pressed somewhere in the main window, do the following, in order: """ if self.docEditor.docSearch.isVisible(): self.docEditor.closeSearch() elif self.isFocusMode: self.toggleFocusMode() return def _mainTabChanged(self, tabIndex): """Activated when the main window tab is changed. """ if tabIndex == self.idxTabEdit: logger.verbose("Editor tab activated") elif tabIndex == self.idxTabProj: logger.verbose("Project outline tab activated") if self.hasProject: self.projView.refreshTree() return
def testCoreToOdt_Convert(dummyGUI): """Test the converter of the ToHtml class. """ theProject = NWProject(dummyGUI) dummyGUI.theIndex = NWIndex(theProject, dummyGUI) theDoc = ToOdt(theProject, dummyGUI, isFlat=True) # Export Mode # =========== theDoc.isNovel = True # Header 1 theDoc.theText = "# Title\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_1" text:outline-level="1">Title</text:h>' '</office:text>') # Header 1 theDoc.theText = "## Chapter Title\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:h text:style-name="Heading_2" text:outline-level="2">Chapter Title</text:h>' '</office:text>') # Nested Text theDoc.theText = "Some ~~nested **bold** and _italics_ text~~ text.\nNo format\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_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. No format</text:p>' '</office:text>') # Hard Break theDoc.theText = "Some text. \nNext line\n" theDoc.tokenizeText() theDoc.initDocument() theDoc.doConvert() theDoc.closeDocument() assert xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_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 xmlToText(theDoc._xText) == ( '<office:text>' '<text:p text:style-name="Text_Body"><text:tab/>Item 1<text:tab/>Item 2</text:p>' '</office:text>')
def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal): """Test loading and saving a document with the NWDoc class. """ theProject = NWProject(dummyGUI) assert theProject.openProject(nwMinimal) assert theProject.projPath == nwMinimal theDoc = NWDoc(theProject, dummyGUI) sHandle = "8c659a11cd429" # Not a valid handle assert theDoc.openDocument("dummy") is None # Non-existent handle assert theDoc.openDocument("0000000000000") is None # Cause open() to fail while loading def dummyOpen(*args, **kwargs): raise OSError with monkeypatch.context() as mp: mp.setattr("builtins.open", dummyOpen) assert theDoc.openDocument(sHandle) is None # Load the text assert theDoc.openDocument(sHandle) == "### New Scene\n\n" # Try to open a new (non-existent) file nHandle = theProject.projTree.findRoot(nwItemClass.NOVEL) assert nHandle is not None xHandle = theProject.newFile("New File", nwItemClass.NOVEL, nHandle) assert theDoc.openDocument(xHandle) == "" # Check cached item assert isinstance(theDoc._theItem, NWItem) assert theDoc.openDocument(xHandle, isOrphan=True) == "" assert theDoc._theItem is None # Set handle and save again theText = "### Test File\n\nText ...\n\n" assert theDoc.openDocument(xHandle) == "" assert theDoc.saveDocument(theText) # Save again to ensure temp file and previous file is handled assert theDoc.saveDocument(theText) # Check file content docPath = os.path.join(nwMinimal, "content", xHandle + ".nwd") with open(docPath, mode="r", encoding="utf8") as inFile: assert inFile.read() == ("%%~name: New File\n" f"%%~path: a508bb932959c/{xHandle}\n" "%%~kind: NOVEL/SCENE\n" "### Test File\n\n" "Text ...\n\n") # Force no meta data theDoc._theItem = None assert theDoc.saveDocument(theText) with open(docPath, mode="r", encoding="utf8") as inFile: assert inFile.read() == theText # Cause open() to fail while saving with monkeypatch.context() as mp: mp.setattr("builtins.open", causeOSError) assert not theDoc.saveDocument(theText) # Saving with no handle theDoc.clearDocument() assert not theDoc.saveDocument(theText) # Delete the last document assert not theDoc.deleteDocument("dummy") assert os.path.isfile(docPath) # Cause the delete to fail with monkeypatch.context() as mp: mp.setattr("os.unlink", causeOSError) assert not theDoc.deleteDocument(xHandle) # Make the delete pass assert theDoc.deleteDocument(xHandle) assert not os.path.isfile(docPath)
def testCoreItem_Setters(dummyGUI): """Test all the simple setters for the NWItem class. """ theProject = NWProject(dummyGUI) theItem = NWItem(theProject) # Name theItem.setName("A Name") assert theItem.itemName == "A Name" theItem.setName("\t A Name ") assert theItem.itemName == "A Name" theItem.setName(123) assert theItem.itemName == "" # Handle theItem.setHandle(123) assert theItem.itemHandle is None theItem.setHandle("0123456789abcdef") assert theItem.itemHandle is None theItem.setHandle("0123456789abg") assert theItem.itemHandle is None theItem.setHandle("0123456789abc") assert theItem.itemHandle == "0123456789abc" # Parent theItem.setParent(None) assert theItem.itemParent is None theItem.setParent(123) assert theItem.itemParent is None theItem.setParent("0123456789abcdef") assert theItem.itemParent is None theItem.setParent("0123456789abg") assert theItem.itemParent is None theItem.setParent("0123456789abc") assert theItem.itemParent == "0123456789abc" # Order theItem.setOrder(None) assert theItem.itemOrder == 0 theItem.setOrder("1") assert theItem.itemOrder == 1 theItem.setOrder(1) assert theItem.itemOrder == 1 # Status theItem.setStatus("Nonsense") assert theItem.itemStatus == "New" theItem.setStatus("New") assert theItem.itemStatus == "New" theItem.setStatus("Minor") assert theItem.itemStatus == "Minor" theItem.setStatus("Major") assert theItem.itemStatus == "Major" theItem.setStatus("Main") assert theItem.itemStatus == "Main" # Importance theItem.itemClass = nwItemClass.NOVEL theItem.setStatus("Nonsense") assert theItem.itemStatus == "New" theItem.setStatus("New") assert theItem.itemStatus == "New" theItem.setStatus("Note") assert theItem.itemStatus == "Note" theItem.setStatus("Draft") assert theItem.itemStatus == "Draft" theItem.setStatus("Finished") assert theItem.itemStatus == "Finished" # Expanded theItem.setExpanded(8) assert not theItem.isExpanded theItem.setExpanded(None) assert not theItem.isExpanded theItem.setExpanded("None") assert not theItem.isExpanded theItem.setExpanded("What?") assert not theItem.isExpanded theItem.setExpanded("True") assert theItem.isExpanded theItem.setExpanded(True) assert theItem.isExpanded # Exported theItem.setExported(8) assert not theItem.isExported theItem.setExported(None) assert not theItem.isExported theItem.setExported("None") assert not theItem.isExported theItem.setExported("What?") assert not theItem.isExported theItem.setExported("True") assert theItem.isExported theItem.setExported(True) assert theItem.isExported # CharCount theItem.setCharCount(None) assert theItem.charCount == 0 theItem.setCharCount("1") assert theItem.charCount == 1 theItem.setCharCount(1) assert theItem.charCount == 1 # WordCount theItem.setWordCount(None) assert theItem.wordCount == 0 theItem.setWordCount("1") assert theItem.wordCount == 1 theItem.setWordCount(1) assert theItem.wordCount == 1 # ParaCount theItem.setParaCount(None) assert theItem.paraCount == 0 theItem.setParaCount("1") assert theItem.paraCount == 1 theItem.setParaCount(1) assert theItem.paraCount == 1 # CursorPos theItem.setCursorPos(None) assert theItem.cursorPos == 0 theItem.setCursorPos("1") assert theItem.cursorPos == 1 theItem.setCursorPos(1) assert theItem.cursorPos == 1 # Initial Count theItem.setWordCount(234) theItem.saveInitialCount() assert theItem.initCount == 234
def testCoreItem_XMLPackUnpack(dummyGUI): """Test packing and unpacking XML objects for the NWItem class. """ theProject = NWProject(dummyGUI) nwXML = etree.Element("novelWriterXML") # File # ==== theItem = NWItem(theProject) theItem.setHandle("0123456789abc") theItem.setParent("0123456789abc") theItem.setOrder(1) theItem.setName("A Name") theItem.setClass("NOVEL") theItem.setType("FILE") theItem.setStatus("Main") theItem.setLayout("NOTE") theItem.setExported(False) theItem.setParaCount(3) theItem.setWordCount(5) theItem.setCharCount(7) theItem.setCursorPos(11) # Pack xContent = etree.SubElement(nwXML, "content") theItem.packXML(xContent) assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == ( b"<content>" b"<item handle=\"0123456789abc\" order=\"1\" parent=\"0123456789abc\">" b"<name>A Name</name><type>FILE</type><class>NOVEL</class><status>New</status>" b"<exported>False</exported><layout>NOTE</layout><charCount>7</charCount>" b"<wordCount>5</wordCount><paraCount>3</paraCount><cursorPos>11</cursorPos></item>" b"</content>") # Unpack theItem = NWItem(theProject) assert theItem.unpackXML(xContent[0]) assert theItem.itemHandle == "0123456789abc" assert theItem.itemParent == "0123456789abc" assert theItem.itemOrder == 1 assert theItem.isExported is False assert theItem.paraCount == 3 assert theItem.wordCount == 5 assert theItem.charCount == 7 assert theItem.cursorPos == 11 assert theItem.itemClass == nwItemClass.NOVEL assert theItem.itemType == nwItemType.FILE assert theItem.itemLayout == nwItemLayout.NOTE # Folder # ====== theItem = NWItem(theProject) theItem.setHandle("0123456789abc") theItem.setParent("0123456789abc") theItem.setOrder(1) theItem.setName("A Name") theItem.setClass("NOVEL") theItem.setType("FOLDER") theItem.setStatus("Main") theItem.setLayout("NOTE") theItem.setExpanded(True) theItem.setExported(False) theItem.setParaCount(3) theItem.setWordCount(5) theItem.setCharCount(7) theItem.setCursorPos(11) # Pack xContent = etree.SubElement(nwXML, "content") theItem.packXML(xContent) assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == ( b"<content>" b"<item handle=\"0123456789abc\" order=\"1\" parent=\"0123456789abc\">" b"<name>A Name</name><type>FOLDER</type><class>NOVEL</class><status>New</status>" b"<expanded>True</expanded></item>" b"</content>") # Unpack theItem = NWItem(theProject) assert theItem.unpackXML(xContent[0]) assert theItem.itemHandle == "0123456789abc" assert theItem.itemParent == "0123456789abc" assert theItem.itemOrder == 1 assert theItem.isExpanded is True assert theItem.isExported is True assert theItem.paraCount == 0 assert theItem.wordCount == 0 assert theItem.charCount == 0 assert theItem.cursorPos == 0 assert theItem.itemClass == nwItemClass.NOVEL assert theItem.itemType == nwItemType.FOLDER assert theItem.itemLayout == nwItemLayout.NO_LAYOUT # Errors ## Not an Item xDummy = etree.SubElement(nwXML, "stuff") assert not theItem.unpackXML(xDummy) ## Item without Handle xDummy = etree.SubElement(nwXML, "item", attrib={"stuff": "nah"}) assert not theItem.unpackXML(xDummy) ## Item with Invalid SubElement xDummy = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"}) xParam = etree.SubElement(xDummy, "invalid") xParam.text = "stuff" assert not theItem.unpackXML(xDummy) # Pack Valid Item xDummy = etree.SubElement(nwXML, "group") theItem._subPack(xDummy, "subGroup", {"one": "two"}, "value", False) assert etree.tostring(xDummy, pretty_print=False, encoding="utf-8") == ( b"<group><subGroup one=\"two\">value</subGroup></group>") # Pack Not Allowed None xDummy = etree.SubElement(nwXML, "group") assert theItem._subPack(xDummy, "subGroup", {}, None, False) is None assert theItem._subPack(xDummy, "subGroup", {}, "None", False) is None assert etree.tostring(xDummy, pretty_print=False, encoding="utf-8") == (b"<group/>")