Merge pull request #200 from vkbo/doc_meta

Document Meta Data
This commit is contained in:
Veronica K. Berglyd Olsen
2020-05-08 23:10:34 +02:00
committed by GitHub
22 changed files with 64 additions and 17 deletions
+32 -2
View File
@@ -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)
+6 -8
View File
@@ -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
+1
View File
@@ -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)
@@ -1,3 +1,4 @@
%%~ 14298de4d9524:f7e2d9f330615:f6622b4617424:John Smith
# John Smith
@tag: John
@@ -1,3 +1,4 @@
%%~ 53b69b83cdafc:7031beac91f75:Title Page
# My Novel
**By Jane Doh**
@@ -1,3 +1,4 @@
%%~ 5eaea4e8cdee8:15c4492bd5107:Mars
# Mars
@tag: Mars
@@ -1,3 +1,4 @@
%%~ 636b6aa9b697b:e7ded148d6e4a:7031beac91f75:Making a Scene
### Making a Scene
@pov: Jane
@@ -1,3 +1,4 @@
%%~ 6a2d6d5f4f401:e7ded148d6e4a:7031beac91f75:Chapter One
## So it Begins
@pov: Jane
@@ -1,3 +1,4 @@
%%~ 88706ddc78b1b:e7ded148d6e4a:7031beac91f75:Chapter Two
## Where has John Gone?
@pov: Jane
@@ -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.
@@ -1,3 +1,4 @@
%%~ ae7339df26ded:e7ded148d6e4a:7031beac91f75:We Found John!
### We Found John!
@pov: John
@@ -1,3 +1,4 @@
%%~ b3e74dbc1f584:15c4492bd5107:Earth
# Earth
@tag: Earth
@@ -1,3 +1,4 @@
%%~ b8136a5a774a0:98acd8c76c93a:Delete Me!
### Delete Me!
This scene is trash.
@@ -1,3 +1,4 @@
%%~ ba8a28a246524:e7ded148d6e4a:7031beac91f75:Interlude
## Interlude
% Notice that this is a file with the flag N.Un. The N means its a novel file, and the Un means its 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.
@@ -1,3 +1,4 @@
%%~ bb2c23b3c42cc:f7e2d9f330615:f6622b4617424:Jane Smith
# Jane Smith
@tag: Jane
@@ -1,3 +1,4 @@
%%~ bc0cbd2a407f3:e7ded148d6e4a:7031beac91f75:Another Scene
### Another Scene
@pov: John
@@ -1,3 +1,4 @@
%%~ f1471bef9f2ae:15c4492bd5107:Space
# Space
@tag: Space
+7 -7
View File
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="0.4.5" fileVersion="1.0" saveCount="52" autoCount="8" timeStamp="2020-04-26 18:05:45">
<novelWriterXML appVersion="0.4.5" fileVersion="1.0" saveCount="122" autoCount="15" timeStamp="2020-05-08 22:47:59">
<project>
<name>Sample Project</name>
<title>Sample Project</title>
@@ -10,9 +10,9 @@
<settings>
<spellCheck>True</spellCheck>
<autoOutline>True</autoOutline>
<lastEdited>6a2d6d5f4f401</lastEdited>
<lastEdited>636b6aa9b697b</lastEdited>
<lastViewed>b3e74dbc1f584</lastViewed>
<lastWordCount>869</lastWordCount>
<lastWordCount>875</lastWordCount>
<autoReplace>
<A>B</A>
<B>E</B>
@@ -83,7 +83,7 @@
<charCount>1199</charCount>
<wordCount>216</wordCount>
<paraCount>7</paraCount>
<cursorPos>950</cursorPos>
<cursorPos>0</cursorPos>
</item>
<item handle="bc0cbd2a407f3" order="2" parent="e7ded148d6e4a">
<name>Another Scene</name>
@@ -240,9 +240,9 @@
<status>New</status>
<expanded>False</expanded>
<layout>SCENE</layout>
<charCount>0</charCount>
<wordCount>0</wordCount>
<paraCount>0</paraCount>
<charCount>30</charCount>
<wordCount>6</wordCount>
<paraCount>1</paraCount>
<cursorPos>36</cursorPos>
</item>
</content>
@@ -1,3 +1,4 @@
%%~ 31489056e0916:25fc0e7096fc6:73475cb40a568:New Scene
# Novel
## Chapter
@@ -1,3 +1,4 @@
%%~ 98010bd9270f9:44cb730c42048:New File
# Jane Doe
@tag: Jane
@@ -1,3 +1,4 @@
%%~ 1a6562590ef19:811786ad1ae74:New File
# Main Location
@tag: Home
@@ -1,3 +1,4 @@
%%~ 0e17daca5f3e1:71ee45a3c0db9:New File
# Main Plot
@tag: MainPlot