Make tags in the index case insensitive

This commit is contained in:
Veronica Berglyd Olsen
2023-09-15 22:38:39 +02:00
parent e08790e889
commit cba1a2c062
+34 -22
View File
@@ -151,11 +151,13 @@ class NWIndex:
"""Check if the index has changed since a given time.""" """Check if the index has changed since a given time."""
return self._indexChange > float(checkTime) return self._indexChange > float(checkTime)
def rootChangedSince(self, rootHandle: str, checkTime: int | float) -> bool: def rootChangedSince(self, rootHandle: str | None, checkTime: int | float) -> bool:
"""Check if the index has changed since a given time for a """Check if the index has changed since a given time for a
given root item. given root item.
""" """
return self._rootChange.get(rootHandle, self._indexChange) > float(checkTime) if isinstance(rootHandle, str):
return self._rootChange.get(rootHandle, self._indexChange) > float(checkTime)
return False
## ##
# Load and Save Index to/from File # Load and Save Index to/from File
@@ -475,10 +477,11 @@ class NWIndex:
return isGood return isGood
# If we're still here, we check that the references exist # If we're still here, we check that the references exist
theKey = nwKeyWords.KEY_CLASS[tBits[0]].name refKey = nwKeyWords.KEY_CLASS[tBits[0]].name
for n in range(1, nBits): for n in range(1, nBits):
if tBits[n] in self._tagsIndex: tagKey = tBits[n].lower()
isGood[n] = self._tagsIndex.tagClass(tBits[n]) == theKey if tagKey in self._tagsIndex:
isGood[n] = self._tagsIndex.tagClass(tagKey) == refKey
return isGood return isGood
@@ -586,15 +589,15 @@ class NWIndex:
"""Extract all references made in a file, and optionally title """Extract all references made in a file, and optionally title
section. section.
""" """
theRefs = {x: [] for x in nwKeyWords.KEY_CLASS} tRefs = {x: [] for x in nwKeyWords.KEY_CLASS}
for rTitle, hItem in self._itemIndex.iterItemHeaders(tHandle): for rTitle, hItem in self._itemIndex.iterItemHeaders(tHandle):
if sTitle is None or sTitle == rTitle: if sTitle is None or sTitle == rTitle:
for aTag, refTypes in hItem.references.items(): for aTag, refTypes in hItem.references.items():
for refType in refTypes: for refType in refTypes:
if refType in theRefs: if refType in tRefs:
theRefs[refType].append(aTag) tRefs[refType].append(self._tagsIndex.tagName(aTag))
return theRefs return tRefs
def getBackReferenceList(self, tHandle: str) -> dict[str, str]: def getBackReferenceList(self, tHandle: str) -> dict[str, str]:
"""Build a list of files referring back to our file, specified """Build a list of files referring back to our file, specified
@@ -603,17 +606,17 @@ class NWIndex:
if tHandle is None or tHandle not in self._itemIndex: if tHandle is None or tHandle not in self._itemIndex:
return {} return {}
theRefs = {} tRefs = {}
theTags = self._itemIndex.allItemTags(tHandle) tTags = self._itemIndex.allItemTags(tHandle)
if not theTags: if not tTags:
return theRefs return tRefs
for aHandle, sTitle, hItem in self._itemIndex.iterAllHeaders(): for aHandle, sTitle, hItem in self._itemIndex.iterAllHeaders():
for aTag in hItem.references: for aTag in hItem.references:
if aTag in theTags and aHandle not in theRefs: if aTag in tTags and aHandle not in tRefs:
theRefs[aHandle] = sTitle tRefs[aHandle] = sTitle
return theRefs return tRefs
def getTagSource(self, tagKey: str) -> tuple[str, str]: def getTagSource(self, tagKey: str) -> tuple[str, str]:
"""Return the source location of a given tag.""" """Return the source location of a given tag."""
@@ -663,22 +666,26 @@ class TagsIndex:
def add(self, tagKey: str, tHandle: str, sTitle: str, itemClass: nwItemClass): def add(self, tagKey: str, tHandle: str, sTitle: str, itemClass: nwItemClass):
"""Add a key to the index and set all values.""" """Add a key to the index and set all values."""
self._tags[tagKey] = { self._tags[tagKey.lower()] = {
"handle": tHandle, "heading": sTitle, "class": itemClass.name "name": tagKey, "handle": tHandle, "heading": sTitle, "class": itemClass.name
} }
return return
def tagName(self, tagKey: str) -> str:
"""Get the display name of a given tag."""
return self._tags.get(tagKey.lower(), {}).get("name", None)
def tagHandle(self, tagKey: str) -> str: def tagHandle(self, tagKey: str) -> str:
"""Get the handle of a given tag.""" """Get the handle of a given tag."""
return self._tags.get(tagKey, {}).get("handle", None) return self._tags.get(tagKey.lower(), {}).get("handle", None)
def tagHeading(self, tagKey: str) -> str: def tagHeading(self, tagKey: str) -> str:
"""Get the heading of a given tag.""" """Get the heading of a given tag."""
return self._tags.get(tagKey, {}).get("heading", TT_NONE) return self._tags.get(tagKey.lower(), {}).get("heading", TT_NONE)
def tagClass(self, tagKey: str) -> str | None: def tagClass(self, tagKey: str) -> str | None:
"""Get the class of a given tag.""" """Get the class of a given tag."""
return self._tags.get(tagKey, {}).get("class", None) return self._tags.get(tagKey.lower(), {}).get("class", None)
## ##
# Pack/Unpack # Pack/Unpack
@@ -699,12 +706,16 @@ class TagsIndex:
for tagKey, tagData in data.items(): for tagKey, tagData in data.items():
if not isinstance(tagKey, str): if not isinstance(tagKey, str):
raise ValueError("tagsIndex keys must be a strings") raise ValueError("tagsIndex keys must be a strings")
if "name" not in tagData:
raise KeyError("A tagIndex item is missing a name entry")
if "handle" not in tagData: if "handle" not in tagData:
raise KeyError("A tagIndex item is missing a handle entry") raise KeyError("A tagIndex item is missing a handle entry")
if "heading" not in tagData: if "heading" not in tagData:
raise KeyError("A tagIndex item is missing a heading entry") raise KeyError("A tagIndex item is missing a heading entry")
if "class" not in tagData: if "class" not in tagData:
raise KeyError("A tagIndex item is missing a class entry") raise KeyError("A tagIndex item is missing a class entry")
if tagData["name"].lower() != tagKey.lower():
raise ValueError("tagsIndex name must match key")
if not isHandle(tagData["handle"]): if not isHandle(tagData["handle"]):
raise ValueError("tagsIndex handle must be a handle") raise ValueError("tagsIndex handle must be a handle")
if not isTitleTag(tagData["heading"]): if not isTitleTag(tagData["heading"]):
@@ -1127,7 +1138,7 @@ class IndexHeading:
def setTag(self, tagKey: str): def setTag(self, tagKey: str):
"""Set the tag for references, and make sure it is a string.""" """Set the tag for references, and make sure it is a string."""
self._tag = str(tagKey) self._tag = str(tagKey).lower()
return return
def addReference(self, tagKey: str, refType: str): def addReference(self, tagKey: str, refType: str):
@@ -1135,6 +1146,7 @@ class IndexHeading:
associated with. associated with.
""" """
if refType in nwKeyWords.VALID_KEYS: if refType in nwKeyWords.VALID_KEYS:
tagKey = tagKey.lower()
if tagKey not in self._refs: if tagKey not in self._refs:
self._refs[tagKey] = set() self._refs[tagKey] = set()
self._refs[tagKey].add(refType) self._refs[tagKey].add(refType)