Cleaned up index, and fixed data extraction

This commit is contained in:
Veronica K. B. Olsen
2019-11-17 14:05:09 +01:00
parent 816c708853
commit 6500d23bd7
2 changed files with 59 additions and 55 deletions
+13 -17
View File
@@ -118,28 +118,24 @@ class GuiProjectOutline(QWidget):
if tHandle not in self.theIndex.novelIndex: if tHandle not in self.theIndex.novelIndex:
continue continue
nwItem = self.theProject.getItem(tHandle) for sTitle in self.theIndex.novelIndex[tHandle]:
if nwItem.itemLayout == nwItemLayout.NOTE:
continue
for tEntry in self.theIndex.novelIndex[tHandle]: nwItem = self.theProject.getItem(tHandle)
tTitle = self.theIndex.novelIndex[tHandle][sTitle][1]
nTitle = str(tEntry[0]) tLevel = self.theIndex.novelIndex[tHandle][sTitle][0]
tTitle = tEntry[2]
tLevel = str(tEntry[1])
tLabel = nwItem.itemName tLabel = nwItem.itemName
tItem = self._createTreeItem(tHandle, nTitle, tTitle, tLevel, tLabel) tItem = self._createTreeItem(tHandle, sTitle, tTitle, tLevel, tLabel)
if tEntry[1] == 1: if tLevel == "H1":
currTitle = tItem currTitle = tItem
self.mainTree.addTopLevelItem(tItem) self.mainTree.addTopLevelItem(tItem)
elif tEntry[1] == 2: elif tLevel == "H2":
if currTitle is None: if currTitle is None:
self.mainTree.addTopLevelItem(tItem) self.mainTree.addTopLevelItem(tItem)
else: else:
currTitle.addChild(tItem) currTitle.addChild(tItem)
currChapter = tItem currChapter = tItem
elif tEntry[1] == 3: elif tLevel == "H3":
if currChapter is None: if currChapter is None:
if currTitle is None: if currTitle is None:
self.mainTree.addTopLevelItem(tItem) self.mainTree.addTopLevelItem(tItem)
@@ -148,7 +144,7 @@ class GuiProjectOutline(QWidget):
else: else:
currChapter.addChild(tItem) currChapter.addChild(tItem)
currScene = tItem currScene = tItem
elif tEntry[1] == 4: elif tLevel == "H4":
if currScene is None: if currScene is None:
if currChapter is None: if currChapter is None:
if currTitle is None: if currTitle is None:
@@ -168,15 +164,15 @@ class GuiProjectOutline(QWidget):
# Internal Functions # Internal Functions
## ##
def _createTreeItem(self, tHandle, nTitle, tTitle, tLevel, tLabel): def _createTreeItem(self, tHandle, sTitle, tTitle, tLevel, tLabel):
newItem = QTreeWidgetItem() newItem = QTreeWidgetItem()
newItem.setText(self.I_TITLE, tTitle) newItem.setText(self.I_TITLE, tTitle)
newItem.setText(self.I_LEVEL, tLevel) newItem.setText(self.I_LEVEL, tLevel)
newItem.setText(self.I_LABEL, tLabel) newItem.setText(self.I_LABEL, tLabel)
newItem.setText(self.I_LINE, nTitle) newItem.setText(self.I_LINE, sTitle)
cC, wC, pC = self.theIndex.getCounts(tHandle, nTitle) cC, wC, pC = self.theIndex.getCounts(tHandle, sTitle)
newItem.setText(self.I_CCOUNT, str(cC)) newItem.setText(self.I_CCOUNT, str(cC))
newItem.setText(self.I_WCOUNT, str(wC)) newItem.setText(self.I_WCOUNT, str(wC))
newItem.setText(self.I_PCOUNT, str(pC)) newItem.setText(self.I_PCOUNT, str(pC))
@@ -192,7 +188,7 @@ class GuiProjectOutline(QWidget):
objectList = [] objectList = []
entityList = [] entityList = []
customList = [] customList = []
for tKey, tTag in self.theIndex.getReferences(tHandle, nTitle): for tKey, tTag in self.theIndex.getReferences(tHandle, sTitle):
if tKey == nwKeyWords.POV_KEY: if tKey == nwKeyWords.POV_KEY:
povList.append(tTag) povList.append(tTag)
elif tKey == nwKeyWords.CHAR_KEY: elif tKey == nwKeyWords.CHAR_KEY:
+44 -36
View File
@@ -37,14 +37,14 @@ class NWIndex():
nwKeyWords.CUSTOM_KEY nwKeyWords.CUSTOM_KEY
] ]
TAG_CLASS = { TAG_CLASS = {
nwKeyWords.CHAR_KEY : [nwItemClass.CHARACTER, 1], nwKeyWords.CHAR_KEY : nwItemClass.CHARACTER,
nwKeyWords.POV_KEY : [nwItemClass.CHARACTER, 2], nwKeyWords.POV_KEY : nwItemClass.CHARACTER,
nwKeyWords.PLOT_KEY : [nwItemClass.PLOT, 1], nwKeyWords.PLOT_KEY : nwItemClass.PLOT,
nwKeyWords.TIME_KEY : [nwItemClass.TIMELINE, 1], nwKeyWords.TIME_KEY : nwItemClass.TIMELINE,
nwKeyWords.WORLD_KEY : [nwItemClass.WORLD, 1], nwKeyWords.WORLD_KEY : nwItemClass.WORLD,
nwKeyWords.OBJECT_KEY : [nwItemClass.OBJECT, 1], nwKeyWords.OBJECT_KEY : nwItemClass.OBJECT,
nwKeyWords.ENTITY_KEY : [nwItemClass.ENTITY, 1], nwKeyWords.ENTITY_KEY : nwItemClass.ENTITY,
nwKeyWords.CUSTOM_KEY : [nwItemClass.CUSTOM, 1], nwKeyWords.CUSTOM_KEY : nwItemClass.CUSTOM,
} }
def __init__(self, theProject, theParent): def __init__(self, theProject, theParent):
@@ -211,18 +211,19 @@ class NWIndex():
self.indexBroken = True self.indexBroken = True
for tHandle in self.refIndex: for tHandle in self.refIndex:
for tEntry in self.refIndex[tHandle]: for sTitle in self.refIndex[tHandle]:
if len(tEntry) != 4: for tEntry in self.refIndex[tHandle][sTitle]:
self.indexBroken = True if len(tEntry) != 3:
self.indexBroken = True
for tHandle in self.novelIndex: for tHandle in self.novelIndex:
for tEntry in self.novelIndex[tHandle]: for sLine in self.novelIndex[tHandle]:
if len(tEntry) != 4: if len(self.novelIndex[tHandle][sLine]) != 3:
self.indexBroken = True self.indexBroken = True
for tHandle in self.noteIndex: for tHandle in self.noteIndex:
for tEntry in self.noteIndex[tHandle]: for sLine in self.noteIndex[tHandle]:
if len(tEntry) != 4: if len(self.noteIndex[tHandle][sLine]) != 3:
self.indexBroken = True self.indexBroken = True
for tHandle in self.fileCounts: for tHandle in self.fileCounts:
@@ -240,7 +241,7 @@ class NWIndex():
if self.indexBroken: if self.indexBroken:
self.clearIndex() self.clearIndex()
self.theParent.makeAlert( self.theParent.makeAlert(
"The project index loaded from cache contains errors. Triggering Rebuild Index.", "The index loaded from project cache contains errors. Rebuilding index.",
nwAlert.WARN nwAlert.WARN
) )
@@ -273,12 +274,12 @@ class NWIndex():
logger.debug("Indexing item with handle %s" % tHandle) logger.debug("Indexing item with handle %s" % tHandle)
# Check file type, and reset its old index # Check file type, and reset its old index
self.refIndex[tHandle] = [] self.refIndex[tHandle] = {}
if itemLayout == nwItemLayout.NOTE: if itemLayout == nwItemLayout.NOTE:
self.noteIndex[tHandle] = [] self.noteIndex[tHandle] = {}
isNovel = False isNovel = False
else: else:
self.novelIndex[tHandle] = [] self.novelIndex[tHandle] = {}
isNovel = True isNovel = True
# Also clear references to file in tag index # Also clear references to file in tag index
@@ -309,7 +310,7 @@ class NWIndex():
if nTitle > 0: if nTitle > 0:
lastText = "\n".join(theLines[nTitle-1:nLine-1]) lastText = "\n".join(theLines[nTitle-1:nLine-1])
cC, wC, pC = countWords(lastText) cC, wC, pC = countWords(lastText)
theCounts[str(nTitle)] = [cC, wC, pC] theCounts["T%d" % nTitle] = [cC, wC, pC]
nTitle = nLine nTitle = nLine
elif aLine.startswith(r"@"): elif aLine.startswith(r"@"):
@@ -317,13 +318,13 @@ class NWIndex():
self.indexTag(tHandle, aLine, nLine, itemClass) self.indexTag(tHandle, aLine, nLine, itemClass)
elif aLine.startswith(r"%synopsis:"): elif aLine.startswith(r"%synopsis:"):
theSynopsis[str(nTitle)] = aLine[10:].strip() theSynopsis["T%d" % nTitle] = aLine[10:].strip()
# Count words for remaining text after last heading # Count words for remaining text after last heading
if nTitle > 0: if nTitle > 0:
lastText = "\n".join(theLines[nTitle-1:]) lastText = "\n".join(theLines[nTitle-1:])
cC, wC, pC = countWords(lastText) cC, wC, pC = countWords(lastText)
theCounts[str(nTitle)] = [cC, wC, pC] theCounts["T%d" % nTitle] = [cC, wC, pC]
if theSynopsis: if theSynopsis:
self.textSynopsis[tHandle] = theSynopsis self.textSynopsis[tHandle] = theSynopsis
@@ -342,27 +343,30 @@ class NWIndex():
""" """
if aLine.startswith("# "): if aLine.startswith("# "):
hDepth = 1 hDepth = "H1"
hText = aLine[2:].strip() hText = aLine[2:].strip()
elif aLine.startswith("## "): elif aLine.startswith("## "):
hDepth = 2 hDepth = "H2"
hText = aLine[3:].strip() hText = aLine[3:].strip()
elif aLine.startswith("### "): elif aLine.startswith("### "):
hDepth = 3 hDepth = "H3"
hText = aLine[4:].strip() hText = aLine[4:].strip()
elif aLine.startswith("#### "): elif aLine.startswith("#### "):
hDepth = 4 hDepth = "H4"
hText = aLine[5:].strip() hText = aLine[5:].strip()
else: else:
return False return False
sTitle = "T%d" % nLine
self.refIndex[tHandle][sTitle] = []
if hText != "": if hText != "":
if isNovel: if isNovel:
if tHandle in self.novelIndex: if tHandle in self.novelIndex:
self.novelIndex[tHandle].append([nLine, hDepth, hText, itemLayout.name]) self.novelIndex[tHandle][sTitle] = [hDepth, hText, itemLayout.name]
else: else:
if tHandle in self.noteIndex: if tHandle in self.noteIndex:
self.noteIndex[tHandle].append([nLine, hDepth, hText, itemLayout.name]) self.noteIndex[tHandle][sTitle] = [hDepth, hText, itemLayout.name]
return True return True
@@ -375,9 +379,11 @@ class NWIndex():
if not isValid or len(theBits) == 0: if not isValid or len(theBits) == 0:
return False return False
sTitle = "T%d" % nTitle
if theBits[0] != nwKeyWords.TAG_KEY: if theBits[0] != nwKeyWords.TAG_KEY:
for aVal in theBits[1:]: for aVal in theBits[1:]:
self.refIndex[tHandle].append([nLine, theBits[0], aVal, nTitle]) self.refIndex[tHandle][sTitle].append([nLine, theBits[0], aVal])
return True return True
@@ -470,7 +476,7 @@ class NWIndex():
# If we're still here, we better check that the references exist # If we're still here, we better check that the references exist
for n in range(1,nBits): for n in range(1,nBits):
if theBits[n] in self.tagIndex: if theBits[n] in self.tagIndex:
isGood[n] = self.TAG_CLASS[theBits[0]][0].name == self.tagIndex[theBits[n]][2] isGood[n] = self.TAG_CLASS[theBits[0]].name == self.tagIndex[theBits[n]][2]
return isGood return isGood
@@ -510,9 +516,10 @@ class NWIndex():
if tHandle not in self.refIndex: if tHandle not in self.refIndex:
return theRefs return theRefs
for nLine, tKey, tTag, nTitle in self.refIndex[tHandle]: for sTitle in self.refIndex[tHandle]:
if tTitle is None or tTitle == str(nTitle): for nLine, tKey, tTag in self.refIndex[tHandle][sTitle]:
theRefs.append((tKey, tTag)) if tTitle is None or tTitle == sTitle:
theRefs.append((tKey, tTag))
return theRefs return theRefs
@@ -535,9 +542,10 @@ class NWIndex():
if theTag is not None: if theTag is not None:
for tHandle in self.refIndex: for tHandle in self.refIndex:
for nLine, tKey, tTag, nTitle in self.refIndex[tHandle]: for sTitle in self.refIndex[tHandle]:
if tTag == theTag: for nLine, tKey, tTag in self.refIndex[tHandle][sTitle]:
theRefs[tHandle] = nLine if tTag == theTag:
theRefs[tHandle] = nLine
return theRefs return theRefs