From 09e0d281159312dffd728608f0b6f7475c1303d0 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Mon, 6 Nov 2023 18:19:03 +0100 Subject: [PATCH] Add tags get function to index class --- novelwriter/core/index.py | 10 ++++++++++ tests/test_core/test_core_index.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/novelwriter/core/index.py b/novelwriter/core/index.py index 62f3b4d4..e4210dd2 100644 --- a/novelwriter/core/index.py +++ b/novelwriter/core/index.py @@ -621,6 +621,10 @@ class NWIndex: sTitle = self._tagsIndex.tagHeading(tagKey) return tHandle, sTitle + def getTags(self, itemClass: nwItemClass) -> list[str]: + """Return all tags based on itemClass.""" + return self._tagsIndex.filterTagNames(itemClass.name) + # END Class NWIndex @@ -684,6 +688,12 @@ class TagsIndex: """Get the class of a given tag.""" return self._tags.get(tagKey.lower(), {}).get("class", None) + def filterTagNames(self, className: str) -> list[str]: + """Get a list of tag names for a given class.""" + return [ + x.get("name", "") for x in self._tags.values() if x.get("class", "") == className + ] + ## # Pack/Unpack ## diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 03b497da..773b0e79 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -196,6 +196,16 @@ def testCoreIndex_ScanThis(mockGUI): assert theBits == ["@tag", "this", "and this"] assert thePos == [0, 6, 12] + isValid, theBits, thePos = index.scanThis("@tag: this,, and this") + assert isValid is True + assert theBits == ["@tag", "this", "", "and this"] + assert thePos == [0, 6, 11, 13] + + isValid, theBits, thePos = index.scanThis("@tag: this, , and this") + assert isValid is True + assert theBits == ["@tag", "this", "", "and this"] + assert thePos == [0, 6, 12, 14] + project.closeProject() # END Test testCoreIndex_ScanThis