Allow indexing of untitled pages, and add a ToC function to the index class

This commit is contained in:
Veronica K. B. Olsen
2021-01-10 00:13:26 +01:00
parent f2b2952909
commit 14a0e6bd6e
2 changed files with 95 additions and 10 deletions
+83 -9
View File
@@ -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
@@ -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",