diff --git a/nw/core/document.py b/nw/core/document.py index 1e200d33..17b3203f 100644 --- a/nw/core/document.py +++ b/nw/core/document.py @@ -47,19 +47,31 @@ class NWDoc(): self.docHandle = None self.docEditable = False self.fileLoc = None + self.docMeta = "" # Internal Mapping self.makeAlert = self.theParent.makeAlert return + ## + # Class Methods + ## + def clearDocument(self): + """Clear the document contents. + """ self.theItem = None self.docHandle = None self.docEditable = False + self.fileLoc = None + self.docMeta = "" return def openDocument(self, tHandle, showStatus=True): + """Open a document from handle, capturing potential file system + errors and parse meta data. + """ self.docHandle = tHandle self.theItem = self.theProject.projTree[tHandle] @@ -80,10 +92,19 @@ class NWDoc(): dataDir = path.join(self.theProject.projPath, docDir) docPath = path.join(dataDir, docFile) + theText = "" + self.docMeta = "" if path.isfile(docPath): try: with open(docPath,mode="r",encoding="utf8") as inFile: - theDoc = inFile.read() + fstLine = inFile.readline() + if fstLine.startswith("%%~ "): + # This is the meta line + self.docMeta = fstLine.strip() + else: + theText = fstLine + theText += inFile.read() + except Exception as e: self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR) # Note: Document must be cleared in case of an io error, @@ -97,12 +118,17 @@ class NWDoc(): logger.debug("The requested document does not exist.") return "" + logger.verbose("DocMeta: '%s'" % self.docMeta) + if showStatus: self.theParent.statusBar.setStatus("Opened Document: %s" % self.theItem.itemName) - return theDoc + return theText def saveDocument(self, docText): + """Save the document via temp file in case of save failure, and + in any case keep a backup of the file. + """ if self.docHandle is None or not self.docEditable: return False @@ -118,8 +144,12 @@ class NWDoc(): docTemp = path.join(dataPath, docFile+"~") docBack = path.join(dataPath, docFile[:-3]+"bak") + itemPath = self.theProject.projTree.getItemPath(self.docHandle) + docMeta = "%%~ "+":".join(itemPath)+":"+self.theItem.itemName+"\n" + try: with open(docTemp,mode="w",encoding="utf8") as outFile: + outFile.write(docMeta) outFile.write(docText) except Exception as e: self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) diff --git a/nw/core/project.py b/nw/core/project.py index ef6558cf..57fc7051 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -929,16 +929,14 @@ class NWTree(): self.theProject = theProject - self._projTree = {} # Holds all the items of the project - self._treeOrder = [] # The order of the tree items on the tree view - self._treeRoots = [] # The root items of the tree - self._trashRoot = "" # The handle of the trash root folder - + self._projTree = {} # Holds all the items of the project + self._treeOrder = [] # The order of the tree items on the tree view + self._treeRoots = [] # The root items of the tree + self._trashRoot = None # The handle of the trash root folder self._theLength = 0 # Always the length of _treeOrder self._theIndex = 0 # The current iterator index self._treeChanged = False # True if tree structure has changed - - self._handleSeed = None # Used for generating handles for testing + self._handleSeed = None # Used for generating handles for testing return @@ -952,7 +950,7 @@ class NWTree(): self._projTree = {} self._treeOrder = [] self._treeRoots = [] - self._trashRoot = "" + self._trashRoot = None self._theLength = 0 self._theIndex = 0 self._treeChanged = False diff --git a/nw/gui/elements/doceditor.py b/nw/gui/elements/doceditor.py index a6d76958..96966ddd 100644 --- a/nw/gui/elements/doceditor.py +++ b/nw/gui/elements/doceditor.py @@ -257,6 +257,7 @@ class GuiDocEditor(QTextEdit): self.setPlainText(theDoc) afTime = time() logger.debug("Document highlighted in %.3f milliseconds" % (1000*(afTime-bfTime))) + self.updateDocMargins() if tLine is None: self.setCursorPosition(self.nwDocument.theItem.cursorPos) diff --git a/sample/sampleNovel/data_1/4298de4d9524_main.nwd b/sample/sampleNovel/data_1/4298de4d9524_main.nwd index 9bd4961b..106f640e 100644 --- a/sample/sampleNovel/data_1/4298de4d9524_main.nwd +++ b/sample/sampleNovel/data_1/4298de4d9524_main.nwd @@ -1,3 +1,4 @@ +%%~ 14298de4d9524:f7e2d9f330615:f6622b4617424:John Smith # John Smith @tag: John diff --git a/sample/sampleNovel/data_5/3b69b83cdafc_main.nwd b/sample/sampleNovel/data_5/3b69b83cdafc_main.nwd index 3f99a01e..51c7c8d9 100644 --- a/sample/sampleNovel/data_5/3b69b83cdafc_main.nwd +++ b/sample/sampleNovel/data_5/3b69b83cdafc_main.nwd @@ -1,3 +1,4 @@ +%%~ 53b69b83cdafc:7031beac91f75:Title Page # My Novel **By Jane Doh** diff --git a/sample/sampleNovel/data_5/eaea4e8cdee8_main.nwd b/sample/sampleNovel/data_5/eaea4e8cdee8_main.nwd index 42b7f404..ac8ce5dd 100644 --- a/sample/sampleNovel/data_5/eaea4e8cdee8_main.nwd +++ b/sample/sampleNovel/data_5/eaea4e8cdee8_main.nwd @@ -1,3 +1,4 @@ +%%~ 5eaea4e8cdee8:15c4492bd5107:Mars # Mars @tag: Mars diff --git a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd index e1578fec..ea69cd21 100644 --- a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd +++ b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd @@ -1,3 +1,4 @@ +%%~ 636b6aa9b697b:e7ded148d6e4a:7031beac91f75:Making a Scene ### Making a Scene @pov: Jane diff --git a/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd b/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd index 59cb746f..5e5d8566 100644 --- a/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd +++ b/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd @@ -1,3 +1,4 @@ +%%~ 6a2d6d5f4f401:e7ded148d6e4a:7031beac91f75:Chapter One ## So it Begins @pov: Jane diff --git a/sample/sampleNovel/data_8/8706ddc78b1b_main.nwd b/sample/sampleNovel/data_8/8706ddc78b1b_main.nwd index a29c2f49..94b563cf 100644 --- a/sample/sampleNovel/data_8/8706ddc78b1b_main.nwd +++ b/sample/sampleNovel/data_8/8706ddc78b1b_main.nwd @@ -1,3 +1,4 @@ +%%~ 88706ddc78b1b:e7ded148d6e4a:7031beac91f75:Chapter Two ## Where has John Gone? @pov: Jane diff --git a/sample/sampleNovel/data_9/6b68994dfa3d_main.nwd b/sample/sampleNovel/data_9/6b68994dfa3d_main.nwd index 6615700a..9cd7d322 100644 --- a/sample/sampleNovel/data_9/6b68994dfa3d_main.nwd +++ b/sample/sampleNovel/data_9/6b68994dfa3d_main.nwd @@ -1,3 +1,4 @@ +%%~ 96b68994dfa3d:e7ded148d6e4a:7031beac91f75:A Note on Structure # A Note on Structure This file is just a note. You can save notes anywhere you like in the project tree. Notes can be filtered out when you export the project. diff --git a/sample/sampleNovel/data_a/e7339df26ded_main.nwd b/sample/sampleNovel/data_a/e7339df26ded_main.nwd index c1fd73e6..05ed60d5 100644 --- a/sample/sampleNovel/data_a/e7339df26ded_main.nwd +++ b/sample/sampleNovel/data_a/e7339df26ded_main.nwd @@ -1,3 +1,4 @@ +%%~ ae7339df26ded:e7ded148d6e4a:7031beac91f75:We Found John! ### We Found John! @pov: John diff --git a/sample/sampleNovel/data_b/3e74dbc1f584_main.nwd b/sample/sampleNovel/data_b/3e74dbc1f584_main.nwd index b82ef89a..3f81b275 100644 --- a/sample/sampleNovel/data_b/3e74dbc1f584_main.nwd +++ b/sample/sampleNovel/data_b/3e74dbc1f584_main.nwd @@ -1,3 +1,4 @@ +%%~ b3e74dbc1f584:15c4492bd5107:Earth # Earth @tag: Earth diff --git a/sample/sampleNovel/data_b/8136a5a774a0_main.nwd b/sample/sampleNovel/data_b/8136a5a774a0_main.nwd index e3724c45..4e63373d 100644 --- a/sample/sampleNovel/data_b/8136a5a774a0_main.nwd +++ b/sample/sampleNovel/data_b/8136a5a774a0_main.nwd @@ -1,3 +1,4 @@ +%%~ b8136a5a774a0:98acd8c76c93a:Delete Me! ### Delete Me! This scene is trash. \ No newline at end of file diff --git a/sample/sampleNovel/data_b/a8a28a246524_main.nwd b/sample/sampleNovel/data_b/a8a28a246524_main.nwd index 3b782c75..b30cb63b 100644 --- a/sample/sampleNovel/data_b/a8a28a246524_main.nwd +++ b/sample/sampleNovel/data_b/a8a28a246524_main.nwd @@ -1,3 +1,4 @@ +%%~ ba8a28a246524:e7ded148d6e4a:7031beac91f75:Interlude ## Interlude % Notice that this is a file with the flag ‘N.Un’. The ‘N’ means it’s a novel file, and the ‘Un’ means it’s an unnumbered chapter. Unnumbered chapters can be treated separately from numbered chapters during export. Perfect for when you want to add an interlude, or for a prologue or epilogue. diff --git a/sample/sampleNovel/data_b/b2c23b3c42cc_main.nwd b/sample/sampleNovel/data_b/b2c23b3c42cc_main.nwd index 1a91f10a..869118d7 100644 --- a/sample/sampleNovel/data_b/b2c23b3c42cc_main.nwd +++ b/sample/sampleNovel/data_b/b2c23b3c42cc_main.nwd @@ -1,3 +1,4 @@ +%%~ bb2c23b3c42cc:f7e2d9f330615:f6622b4617424:Jane Smith # Jane Smith @tag: Jane diff --git a/sample/sampleNovel/data_b/c0cbd2a407f3_main.nwd b/sample/sampleNovel/data_b/c0cbd2a407f3_main.nwd index 66d885b6..1fe75e50 100644 --- a/sample/sampleNovel/data_b/c0cbd2a407f3_main.nwd +++ b/sample/sampleNovel/data_b/c0cbd2a407f3_main.nwd @@ -1,3 +1,4 @@ +%%~ bc0cbd2a407f3:e7ded148d6e4a:7031beac91f75:Another Scene ### Another Scene @pov: John diff --git a/sample/sampleNovel/data_f/1471bef9f2ae_main.nwd b/sample/sampleNovel/data_f/1471bef9f2ae_main.nwd index b0f3153a..9b2d9368 100644 --- a/sample/sampleNovel/data_f/1471bef9f2ae_main.nwd +++ b/sample/sampleNovel/data_f/1471bef9f2ae_main.nwd @@ -1,3 +1,4 @@ +%%~ f1471bef9f2ae:15c4492bd5107:Space # Space @tag: Space diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index f2b5d09e..30765dc0 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -10,9 +10,9 @@ True True - 6a2d6d5f4f401 + 636b6aa9b697b b3e74dbc1f584 - 869 + 875 B E @@ -83,7 +83,7 @@ 1199 216 7 - 950 + 0 Another Scene @@ -240,9 +240,9 @@ New False SCENE - 0 - 0 - 0 + 30 + 6 + 1 36 diff --git a/tests/reference/gui/1_1489056e0916_main.nwd b/tests/reference/gui/1_1489056e0916_main.nwd index c5a065c0..fba0c438 100644 --- a/tests/reference/gui/1_1489056e0916_main.nwd +++ b/tests/reference/gui/1_1489056e0916_main.nwd @@ -1,3 +1,4 @@ +%%~ 31489056e0916:25fc0e7096fc6:73475cb40a568:New Scene # Novel ## Chapter diff --git a/tests/reference/gui/1_8010bd9270f9_main.nwd b/tests/reference/gui/1_8010bd9270f9_main.nwd index 4ba77a38..3b0477ca 100644 --- a/tests/reference/gui/1_8010bd9270f9_main.nwd +++ b/tests/reference/gui/1_8010bd9270f9_main.nwd @@ -1,3 +1,4 @@ +%%~ 98010bd9270f9:44cb730c42048:New File # Jane Doe @tag: Jane diff --git a/tests/reference/gui/1_a6562590ef19_main.nwd b/tests/reference/gui/1_a6562590ef19_main.nwd index d4faeeb2..def80bfd 100644 --- a/tests/reference/gui/1_a6562590ef19_main.nwd +++ b/tests/reference/gui/1_a6562590ef19_main.nwd @@ -1,3 +1,4 @@ +%%~ 1a6562590ef19:811786ad1ae74:New File # Main Location @tag: Home diff --git a/tests/reference/gui/1_e17daca5f3e1_main.nwd b/tests/reference/gui/1_e17daca5f3e1_main.nwd index f646f17e..0d9e8ec4 100644 --- a/tests/reference/gui/1_e17daca5f3e1_main.nwd +++ b/tests/reference/gui/1_e17daca5f3e1_main.nwd @@ -1,3 +1,4 @@ +%%~ 0e17daca5f3e1:71ee45a3c0db9:New File # Main Plot @tag: MainPlot