Generalise heading comments

This commit is contained in:
Veronica Berglyd Olsen
2025-04-06 19:13:37 +02:00
parent c5afeda726
commit edc19187f0
5 changed files with 24 additions and 16 deletions
+9 -6
View File
@@ -386,10 +386,10 @@ class Index:
elif line.startswith("%"):
cStyle, cKey, cText, _, _ = processComment(line)
if cStyle in (nwComment.SYNOPSIS, nwComment.SHORT):
self._itemIndex.setHeadingSynopsis(tHandle, cTitle, cText)
elif cStyle == nwComment.FOOTNOTE:
if cStyle == nwComment.FOOTNOTE:
self._itemIndex.addNoteKey(tHandle, "footnotes", cKey)
else:
self._itemIndex.setHeadingComment(tHandle, cTitle, cStyle, cKey, cText)
# Count words for remaining text after last heading
if pTitle != TT_NONE:
@@ -1007,10 +1007,13 @@ class ItemIndex:
self._items[tHandle].setHeadingCounts(sTitle, cC, wC, pC)
return
def setHeadingSynopsis(self, tHandle: str, sTitle: str, text: str) -> None:
"""Set the synopsis text for a heading on a given item."""
def setHeadingComment(
self, tHandle: str, sTitle: str,
comment: nwComment, key: str, text: str,
) -> None:
"""Set a story comment for a heading on a given item."""
if tHandle in self._items:
self._items[tHandle].setHeadingSynopsis(sTitle, text)
self._items[tHandle].setHeadingComment(sTitle, comment, key, text)
return
def setHeadingTag(self, tHandle: str, sTitle: str, tagKey: str) -> None:
+8 -6
View File
@@ -34,6 +34,7 @@ from typing import TYPE_CHECKING, Literal
from novelwriter import CONFIG
from novelwriter.common import checkInt, isListInstance, isTitleTag
from novelwriter.constants import nwKeyWords, nwStyles
from novelwriter.enum import nwComment
if TYPE_CHECKING: # pragma: no cover
from novelwriter.core.index import TagsIndex
@@ -113,10 +114,10 @@ class IndexNode:
self._headings[sTitle].setCounts([cCount, wCount, pCount])
return
def setHeadingSynopsis(self, sTitle: str, text: str) -> None:
"""Set the synopsis text of a heading."""
def setHeadingComment(self, sTitle: str, comment: nwComment, key: str, text: str) -> None:
"""Set the comment text of a heading."""
if sTitle in self._headings:
self._headings[sTitle].setSynopsis(text)
self._headings[sTitle].setComment(comment, key, text)
return
def setHeadingTag(self, sTitle: str, tag: str) -> None:
@@ -302,9 +303,10 @@ class IndexHeading:
)
return
def setSynopsis(self, text: str) -> None:
"""Set the synopsis text and make sure it is a string."""
self._comments["summary"] = str(text)
def setComment(self, comment: nwComment, key: str, text: str) -> None:
"""Set the text for a comment and make sure it is a string."""
if comment in (nwComment.SHORT, nwComment.SYNOPSIS):
self._comments["summary"] = str(text)
return
def setTag(self, tag: str) -> None:
+2
View File
@@ -31,12 +31,14 @@ MODIFIERS = {
"short": nwComment.SHORT,
"note": nwComment.NOTE,
"footnote": nwComment.FOOTNOTE,
"story": nwComment.STORY,
}
KEY_REQ = {
"synopsis": 0, # Key not allowed
"short": 0, # Key not allowed
"note": 1, # Key optional
"footnote": 2, # Key required
"story": 2, # Key required
}
+1 -1
View File
@@ -1196,7 +1196,7 @@ def testCoreIndex_ItemIndex(nwGUI, fncPath, mockRnd):
# Set the remaining data values
itemIndex.setHeadingCounts(cHandle, "T0001", 60, 10, 2)
itemIndex.setHeadingSynopsis(cHandle, "T0001", "In the beginning ...")
itemIndex.setHeadingComment(cHandle, "T0001", nwComment.SYNOPSIS, "", "In the beginning ...")
itemIndex.setHeadingTag(cHandle, "T0001", "One")
itemIndex.addHeadingRef(cHandle, "T0001", ["Jane"], "@pov")
itemIndex.addHeadingRef(cHandle, "T0001", ["Jane"], "@focus")
+4 -3
View File
@@ -27,6 +27,7 @@ from novelwriter.core.index import TagsIndex
from novelwriter.core.indexdata import IndexHeading, IndexNode
from novelwriter.core.item import NWItem
from novelwriter.core.project import NWProject
from novelwriter.enum import nwComment
@pytest.mark.core
@@ -74,8 +75,8 @@ def testCoreIndexData_IndexNode(mockGUI):
assert head2.paraCount == 6
# Set synopsis
node.setHeadingSynopsis("T0001", "The first")
node.setHeadingSynopsis("T0002", "The second")
node.setHeadingComment("T0001", nwComment.SYNOPSIS, "", "The first")
node.setHeadingComment("T0002", nwComment.SYNOPSIS, "", "The second")
assert head1.synopsis == "The first"
assert head2.synopsis == "The second"
@@ -213,7 +214,7 @@ def testCoreIndexData_IndexHeading():
assert head.mainCount == 42
# Set Summary
head.setSynopsis("In the beginning ...")
head.setComment(nwComment.SYNOPSIS, "", "In the beginning ...")
assert head.synopsis == "In the beginning ..."
# Set Tag