From 14a0e6bd6e76675584a7eb4d4c287271a9d39714 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 10 Jan 2021 00:13:26 +0100 Subject: [PATCH] Allow indexing of untitled pages, and add a ToC function to the index class --- nw/core/index.py | 92 +++++++++++++++++-- .../coreIndex_LoadSave_tagsIndex.json | 13 ++- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/nw/core/index.py b/nw/core/index.py index c311b1be..6c4ebf65 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -341,6 +341,11 @@ class NWIndex(): lastText = "\n".join(theLines[nTitle-1:]) self._indexWordCounts(tHandle, isNovel, lastText, nTitle) + # Index page with no titles and references + if nTitle == 0: + self._indexPage(tHandle, isNovel, itemLayout) + self._indexWordCounts(tHandle, isNovel, theText, nTitle) + # Update timestamps for index changes nowTime = round(time()) self._timeIndex = nowTime @@ -400,6 +405,29 @@ class NWIndex(): return True + def _indexPage(self, tHandle, isNovel, itemLayout): + """Index a page with no title. + """ + theData = { + "level" : "H0", + "title" : "Untitled Page", + "layout" : itemLayout.name, + "synopsis" : "", + "cCount" : 0, + "wCount" : 0, + "pCount" : 0, + "updated" : round(time()), + } + + if isNovel: + if tHandle in self._novelIndex: + self._novelIndex[tHandle]["T000000"] = theData + else: + if tHandle in self._noteIndex: + self._noteIndex[tHandle]["T000000"] = theData + + return + def _indexWordCounts(self, tHandle, isNovel, theText, nTitle): """Count text stats and save the counts to the index. """ @@ -551,15 +579,61 @@ class NWIndex(): files, but skipping all note files. """ for tItem in self.theProject.projTree: - if tItem is not None: - if not tItem.isExported and skipExcluded: - continue - tHandle = tItem.itemHandle - if tHandle not in self._novelIndex: - continue - for sTitle in sorted(self._novelIndex[tHandle]): - tKey = "%s:%s" % (tHandle, sTitle) - yield tKey, tHandle, sTitle, self._novelIndex[tHandle][sTitle] + if tItem is None: + continue + if not tItem.isExported and skipExcluded: + continue + + tHandle = tItem.itemHandle + if tHandle not in self._novelIndex: + continue + + for sTitle in sorted(self._novelIndex[tHandle]): + tKey = "%s:%s" % (tHandle, sTitle) + yield tKey, tHandle, sTitle, self._novelIndex[tHandle][sTitle] + + def getTableOfContents(self, maxDepth, skipExcluded=True): + """Generate a table of contents up to a maxiumum depth. + """ + hLevel = {"H0": 0, "H1": 1, "H2": 2, "H3": 3, "H4": 4} + + tOrder = [] + tData = {} + pKey = None + for tItem in self.theProject.projTree: + if tItem is None: + continue + if not tItem.isExported and skipExcluded: + continue + + tHandle = tItem.itemHandle + if tHandle not in self._novelIndex: + continue + + for sTitle in sorted(self._novelIndex[tHandle]): + tKey = "%s:%s" % (tHandle, sTitle) + theData = self._novelIndex[tHandle][sTitle] + iLevel = hLevel.get(theData["level"], 0) + if iLevel > maxDepth: + if pKey in tData: + theData["wCount"] + tData[pKey]["words"] += theData["wCount"] + else: + pKey = tKey + tOrder.append(tKey) + tData[tKey] = { + "level": theData["level"], + "title": theData["title"], + "words": theData["wCount"], + } + + theToC = [] + for tKey in tOrder: + theToC.append(( + tKey, tData[tKey]["level"], tData[tKey]["title"], tData[tKey]["words"] + )) + + return theToC def getCounts(self, tHandle, sTitle=None): """Returns the counts for a file, or a section of a file diff --git a/tests/reference/coreIndex_LoadSave_tagsIndex.json b/tests/reference/coreIndex_LoadSave_tagsIndex.json index 281073cc..2aa3df3c 100644 --- a/tests/reference/coreIndex_LoadSave_tagsIndex.json +++ b/tests/reference/coreIndex_LoadSave_tagsIndex.json @@ -300,7 +300,18 @@ "updated": 123 } }, - "8c58a65414c23": {}, + "8c58a65414c23": { + "T000000": { + "level": "H0", + "title": "Untitled Page", + "layout": "PAGE", + "synopsis": "", + "cCount": 1058, + "wCount": 176, + "pCount": 2, + "updated": 123 + } + }, "88d59a277361b": { "T000001": { "level": "H2",