diff --git a/nw/constants/constants.py b/nw/constants/constants.py index a0325482..d43167a3 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -26,6 +26,7 @@ class nwFiles(): PROJ_DICT = "wordlist.txt" SESS_INFO = "sessionInfo.log" INDEX_FILE = "tagsIndex.json" + META_FILE = "projectMeta.json" EXPORT_OPT = "exportOptions.json" TLINE_OPT = "timelineOptions.json" SLOG_OPT = "sessionLogOptions.json" diff --git a/nw/gui/tools/dochighlight.py b/nw/gui/tools/dochighlight.py index e8ac2b04..f3b0a11b 100644 --- a/nw/gui/tools/dochighlight.py +++ b/nw/gui/tools/dochighlight.py @@ -140,6 +140,13 @@ class GuiDocHighlighter(QSyntaxHighlighter): 0 : self.hStyles["hidden"], } )) + self.hRules.append(( + r"^(%)(synopsis:\s+)(.*)$", { + 1 : self.hStyles["hidden"], + 2 : self.hStyles["keyword"], + 3 : self.hStyles["hidden"], + } + )) # Trailing Spaces, 2+ self.hRules.append(( diff --git a/nw/guimain.py b/nw/guimain.py index 5d6557c5..4053d0c0 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -523,17 +523,17 @@ class GuiMain(QMainWindow): theDoc = NWDoc(self.theProject, self) theText = theDoc.openDocument(tHandle, False) - # Run Word Count - cC, wC, pC = countWords(theText) + # Build tag index + self.theIndex.scanText(tHandle, theText) + + # Get Word Counts + cC, wC, pC = self.theIndex.getCounts(tHandle) tItem.setCharCount(cC) tItem.setWordCount(wC) tItem.setParaCount(pC) self.treeView.propagateCount(tHandle, wC) self.treeView.projectWordCount() - # Build tag index - self.theIndex.scanText(tHandle, theText) - nDone += 1 if dlgProg.wasCanceled(): break diff --git a/nw/project/index.py b/nw/project/index.py index dc5edb75..382abbae 100644 --- a/nw/project/index.py +++ b/nw/project/index.py @@ -19,6 +19,7 @@ from os import path from nw.constants import ( nwFiles, nwKeyWords, nwItemType, nwItemClass, nwAlert ) +from nw.tools import countWords logger = logging.getLogger(__name__) @@ -60,6 +61,10 @@ class NWIndex(): self.novelIndex = {} self.noteIndex = {} + # Meta Data + self.textCounts = {} + self.fileSynopsis = {} + # Lists self.novelList = [] @@ -70,10 +75,12 @@ class NWIndex(): ## def clearIndex(self): - self.tagIndex = {} - self.refIndex = {} - self.novelIndex = {} - self.noteIndex = {} + self.tagIndex = {} + self.refIndex = {} + self.novelIndex = {} + self.noteIndex = {} + self.textCounts = {} + self.fileSynopsis = {} return def deleteHandle(self, tHandle): @@ -101,7 +108,10 @@ class NWIndex(): """ theData = {} + loadsOK = False indexFile = path.join(self.theProject.projMeta, nwFiles.INDEX_FILE) + metaFile = path.join(self.theProject.projMeta, nwFiles.META_FILE) + if path.isfile(indexFile): logger.debug("Loading index file") try: @@ -122,11 +132,29 @@ class NWIndex(): if "noteIndex" in theData.keys(): self.noteIndex = theData["noteIndex"] - self.checkIndex() + loadsOK = True - return True + if path.isfile(indexFile): + logger.debug("Loading meta file") + try: + with open(metaFile,mode="r",encoding="utf8") as inFile: + theJson = inFile.read() + theData = json.loads(theJson) + except Exception as e: + logger.error("Failed to load meta file") + logger.error(str(e)) + return False - return False + if "textCounts" in theData.keys(): + self.textCounts = theData["textCounts"] + if "fileSynopsis" in theData.keys(): + self.fileSynopsis = theData["fileSynopsis"] + + loadsOK &= True + + self.checkIndex() + + return loadsOK def saveIndex(self): """Save the current index as a json file in the project meta @@ -134,11 +162,14 @@ class NWIndex(): """ indexFile = path.join(self.theProject.projMeta, nwFiles.INDEX_FILE) - logger.debug("Saving index file") + metaFile = path.join(self.theProject.projMeta, nwFiles.META_FILE) + + logger.debug("Saving index and meta files") if self.mainConf.debugInfo: nIndent = 2 else: nIndent = None + try: with open(indexFile,mode="w+",encoding="utf8") as outFile: outFile.write(json.dumps({ @@ -152,6 +183,17 @@ class NWIndex(): logger.error(str(e)) return False + try: + with open(metaFile,mode="w+",encoding="utf8") as outFile: + outFile.write(json.dumps({ + "textCounts" : self.textCounts, + "fileSynopsis" : self.fileSynopsis, + }, indent=nIndent)) + except Exception as e: + logger.error("Failed to save meta file") + logger.error(str(e)) + return False + return True def checkIndex(self): @@ -180,6 +222,10 @@ class NWIndex(): if len(tEntry) != 4: self.indexBroken = True + for tHandle in self.textCounts: + if len(self.textCounts[tHandle]) != 3: + self.indexBroken = True + if self.indexBroken: self.clearIndex() self.theParent.makeAlert( @@ -230,17 +276,23 @@ class NWIndex(): nLine = 0 nTitle = 0 for aLine in theText.splitlines(): - aLine = aLine.strip() + aLine = aLine nLine += 1 - nChar = len(aLine) + nChar = len(aLine.strip()) if nChar == 0: continue - if aLine[0] == "#": + if aLine.startswith(r"#"): isTitle = self.indexTitle(tHandle, isNovel, aLine, nLine, itemLayout) if isTitle: nTitle = nLine - elif aLine[0] == "@": + elif aLine.startswith(r"@"): self.indexNoteRef(tHandle, aLine, nLine, nTitle) self.indexTag(tHandle, aLine, nLine, itemClass) + elif aLine.startswith(r"%synopsis:"): + self.fileSynopsis[tHandle] = aLine[10:].strip() + + # Run word counter + cC, wC, pC = countWords(theText) + self.textCounts[tHandle] = [cC, wC, pC] return True @@ -386,6 +438,16 @@ class NWIndex(): # Extract Data ## + def getCounts(self, tHandle): + cC = 0 + wC = 0 + pC = 0 + if tHandle in self.textCounts: + cC = self.textCounts[tHandle][0] + wC = self.textCounts[tHandle][1] + pC = self.textCounts[tHandle][2] + return cC, wC, pC + def buildNovelList(self): """Build a list of the content of the novel. """ diff --git a/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd b/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd index ee002568..cee10674 100644 --- a/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd +++ b/sample/sampleNovel/data_6/a2d6d5f4f401_main.nwd @@ -3,4 +3,4 @@ @pov: Jane @location: Earth -% We can add a chapter file, but keep the scene files separate. In the chapter file we can set the meta data that applies to the whole chapter if we wish. \ No newline at end of file +%synopsis: We can add a chapter file, but keep the scene files separate. In the chapter file we can set the meta data that applies to the whole chapter if we wish to. \ No newline at end of file diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index 63b80110..d98b72a4 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -9,9 +9,9 @@ True - 96b68994dfa3d + 6a2d6d5f4f401 b3e74dbc1f584 - 855 + 849 B E @@ -70,7 +70,7 @@ 12 3 0 - 45 + 211 Making a Scene @@ -118,7 +118,7 @@ 1692 313 6 - 216 + 144 Chapter Two @@ -239,9 +239,9 @@ New False SCENE - 30 - 6 - 1 + 0 + 0 + 0 36