Add comment style processing to the index class
This commit is contained in:
+22
-10
@@ -36,7 +36,7 @@ from typing import TYPE_CHECKING, ItemsView, Iterable, Iterator
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from novelwriter import SHARED
|
from novelwriter import SHARED
|
||||||
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
from novelwriter.enum import nwComment, nwItemClass, nwItemType, nwItemLayout
|
||||||
from novelwriter.error import logException
|
from novelwriter.error import logException
|
||||||
from novelwriter.common import checkInt, isHandle, isItemClass, isTitleTag, jsonEncode
|
from novelwriter.common import checkInt, isHandle, isItemClass, isTitleTag, jsonEncode
|
||||||
from novelwriter.constants import nwFiles, nwKeyWords, nwRegEx, nwUnicode, nwHeaders
|
from novelwriter.constants import nwFiles, nwKeyWords, nwRegEx, nwUnicode, nwHeaders
|
||||||
@@ -338,14 +338,9 @@ class NWIndex:
|
|||||||
|
|
||||||
elif line.startswith("%"):
|
elif line.startswith("%"):
|
||||||
if cTitle != TT_NONE:
|
if cTitle != TT_NONE:
|
||||||
toCheck = line[1:].lstrip()
|
cStyle, cText, _ = processComment(line)
|
||||||
synTag = toCheck[:9].lower()
|
if cStyle in (nwComment.BRIEF, nwComment.SYNOPSIS):
|
||||||
tLen = len(line)
|
self._itemIndex.setHeadingSynopsis(tHandle, cTitle, cText)
|
||||||
cLen = len(toCheck)
|
|
||||||
cOff = tLen - cLen
|
|
||||||
if synTag == "synopsis:":
|
|
||||||
sText = line[cOff+9:].strip()
|
|
||||||
self._itemIndex.setHeadingSynopsis(tHandle, cTitle, sText)
|
|
||||||
|
|
||||||
# Count words for remaining text after last heading
|
# Count words for remaining text after last heading
|
||||||
if pTitle != TT_NONE:
|
if pTitle != TT_NONE:
|
||||||
@@ -1269,9 +1264,26 @@ class IndexHeading:
|
|||||||
|
|
||||||
|
|
||||||
# =============================================================================================== #
|
# =============================================================================================== #
|
||||||
# Simple Word Counter
|
# text Processing Functions
|
||||||
# =============================================================================================== #
|
# =============================================================================================== #
|
||||||
|
|
||||||
|
CLASSIFIERS = {
|
||||||
|
"brief": nwComment.BRIEF,
|
||||||
|
"synopsis": nwComment.SYNOPSIS,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def processComment(text: str) -> tuple[nwComment, str, int]:
|
||||||
|
"""Extract comment style and text. Should only be called on text
|
||||||
|
starting with a %.
|
||||||
|
"""
|
||||||
|
check = text[1:].lstrip()
|
||||||
|
classifier, _, content = check.partition(":")
|
||||||
|
if content and (clean := classifier.strip().lower()) in CLASSIFIERS:
|
||||||
|
return CLASSIFIERS[clean], content.strip(), text.find(":") + 1
|
||||||
|
return nwComment.PLAIN, check, 0
|
||||||
|
|
||||||
|
|
||||||
def countWords(text: str) -> tuple[int, int, int]:
|
def countWords(text: str) -> tuple[int, int, int]:
|
||||||
"""Count words in a piece of text, skipping special syntax and
|
"""Count words in a piece of text, skipping special syntax and
|
||||||
comments.
|
comments.
|
||||||
|
|||||||
@@ -61,6 +61,15 @@ class nwItemLayout(Enum):
|
|||||||
# END Enum nwItemLayout
|
# END Enum nwItemLayout
|
||||||
|
|
||||||
|
|
||||||
|
class nwComment(Enum):
|
||||||
|
|
||||||
|
PLAIN = 0
|
||||||
|
SYNOPSIS = 1
|
||||||
|
BRIEF = 2
|
||||||
|
|
||||||
|
# END Enum nwComment
|
||||||
|
|
||||||
|
|
||||||
class nwTrinary(Enum):
|
class nwTrinary(Enum):
|
||||||
|
|
||||||
NEGATIVE = -1
|
NEGATIVE = -1
|
||||||
|
|||||||
Reference in New Issue
Block a user