Use a set to track comment styles to include in build

This commit is contained in:
Veronica Berglyd Olsen
2025-04-17 16:27:06 +02:00
parent 21431058be
commit 312ad2a21b
9 changed files with 66 additions and 58 deletions
+4 -3
View File
@@ -32,7 +32,7 @@ from PyQt6.QtGui import QFont
from novelwriter import CONFIG
from novelwriter.constants import nwLabels
from novelwriter.core.item import NWItem
from novelwriter.enum import nwBuildFmt
from novelwriter.enum import nwBuildFmt, nwComment
from novelwriter.error import formatException, logException
from novelwriter.formats.todocx import ToDocX
from novelwriter.formats.tohtml import ToHtml
@@ -311,10 +311,11 @@ class NWBuildDocument:
)
bldObj.setBodyText(self._build.getBool("text.includeBodyText"))
bldObj.setSynopsis(self._build.getBool("text.includeSynopsis"))
bldObj.setComments(self._build.getBool("text.includeComments"))
bldObj.setKeywords(self._build.getBool("text.includeKeywords"))
bldObj.setIgnoredKeywords(self._build.getStr("text.ignoredKeywords"))
bldObj.setCommentType(nwComment.PLAIN, self._build.getBool("text.includeComments"))
bldObj.setCommentType(nwComment.SYNOPSIS, self._build.getBool("text.includeSynopsis"))
bldObj.setCommentType(nwComment.SHORT, self._build.getBool("text.includeSynopsis"))
if isinstance(bldObj, ToHtml):
bldObj.setStyles(self._build.getBool("html.addStyles"))
+13 -14
View File
@@ -121,14 +121,16 @@ class Tokenizer(ABC):
self._indentFirst = False # Indent first paragraph
self._doJustify = False # Justify text
self._doBodyText = True # Include body text
self._doSynopsis = False # Also process synopsis comments
self._doComments = False # Also process comments
self._doComments = set() # Comment styles to allow
self._doKeywords = False # Also process keywords like tags and references
self._keepBreaks = True # Keep line breaks in paragraphs
self._defaultAlign = "left" # The default text alignment
self._skipKeywords: set[str] = set() # Keywords to ignore
# Defaults
self._doComments.add(nwComment.FOOTNOTE)
# Other Setting
self._theme = TextDocumentTheme()
self._classes: dict[str, QColor] = {}
@@ -396,14 +398,12 @@ class Tokenizer(ABC):
self._doBodyText = state
return
def setSynopsis(self, state: bool) -> None:
"""Include synopsis comments in build."""
self._doSynopsis = state
return
def setComments(self, state: bool) -> None:
"""Include comments in build."""
self._doComments = state
def setCommentType(self, comment: nwComment, state: bool) -> None:
"""Toggle the inclusion og certain comment types."""
if state:
self._doComments.add(comment)
else:
self._doComments.discard(comment)
return
def setKeywords(self, state: bool) -> None:
@@ -607,9 +607,7 @@ class Tokenizer(ABC):
continue
cStyle, cKey, cText, _, _ = processComment(aLine)
if cStyle in (nwComment.SYNOPSIS, nwComment.SHORT) and not self._doSynopsis:
continue
if cStyle == nwComment.PLAIN and not self._doComments:
if cStyle not in self._doComments:
continue
if doJustify and not tStyle & BlockFmt.ALIGNED:
@@ -1043,7 +1041,8 @@ class Tokenizer(ABC):
tTxt, tFmt = self._extractFormats(text)
tFmt.insert(0, (0, TextFmt.COL_B, style.textClass))
tFmt.append((len(tTxt), TextFmt.COL_E, ""))
if label := (self._localLookup(style.label) + (f" ({key})" if key else "")).strip():
term = f" ({key.title()})" if key else ""
if label := f"{self._localLookup(style.label)}{term}".strip():
shift = len(label) + 2
tTxt = f"{label}: {tTxt}"
rFmt = [(0, TextFmt.B_B, ""), (shift - 1, TextFmt.B_E, "")]
+4 -3
View File
@@ -43,7 +43,7 @@ from PyQt6.QtWidgets import (
from novelwriter import CONFIG, SHARED
from novelwriter.common import decodeMimeHandles, qtAddAction, qtLambda
from novelwriter.constants import nwConst, nwStyles, nwUnicode
from novelwriter.enum import nwChange, nwDocAction, nwDocMode, nwItemType
from novelwriter.enum import nwChange, nwComment, nwDocAction, nwDocMode, nwItemType
from novelwriter.error import logException
from novelwriter.extensions.configlayout import NColorLabel
from novelwriter.extensions.eventfilters import WheelEventFilter
@@ -228,8 +228,9 @@ class GuiDocViewer(QTextBrowser):
qDoc.setTheme(self._docTheme)
qDoc.initDocument()
qDoc.setKeywords(True)
qDoc.setComments(CONFIG.viewComments)
qDoc.setSynopsis(CONFIG.viewSynopsis)
qDoc.setCommentType(nwComment.PLAIN, CONFIG.viewComments)
qDoc.setCommentType(nwComment.SYNOPSIS, CONFIG.viewSynopsis)
qDoc.setCommentType(nwComment.SHORT, CONFIG.viewSynopsis)
# Be extra careful here to prevent crashes when first opening a
# project as a crash here leaves no way of recovering.