Split up block types of comments in build formats

This commit is contained in:
Veronica Berglyd Olsen
2025-06-19 20:47:34 +02:00
parent c598fdd009
commit ba4fdd1f92
8 changed files with 37 additions and 20 deletions
+2 -2
View File
@@ -38,7 +38,7 @@ from novelwriter import __version__
from novelwriter.common import firstFloat, xmlElement, xmlSubElem from novelwriter.common import firstFloat, xmlElement, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwStyles from novelwriter.constants import nwHeadFmt, nwStyles
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import COMMENT_BLOCKS, Tokenizer
from novelwriter.types import QtHexRgb from novelwriter.types import QtHexRgb
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -296,7 +296,7 @@ class ToDocX(Tokenizer):
elif tType == BlockTyp.SKIP: elif tType == BlockTyp.SKIP:
self._processFragments(par, S_NORM, "") self._processFragments(par, S_NORM, "")
elif tType == BlockTyp.COMMENT: elif tType in COMMENT_BLOCKS:
self._processFragments(par, S_META, tText, tFormat) self._processFragments(par, S_META, tText, tFormat)
elif tType == BlockTyp.KEYWORD: elif tType == BlockTyp.KEYWORD:
+2 -2
View File
@@ -32,7 +32,7 @@ from typing import TYPE_CHECKING
from novelwriter.common import formatTimeStamp from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHtmlUnicode, nwStyles from novelwriter.constants import nwHtmlUnicode, nwStyles
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import COMMENT_BLOCKS, Tokenizer
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS, QtHexRgb from novelwriter.types import FONT_STYLE, FONT_WEIGHTS, QtHexRgb
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -228,7 +228,7 @@ class ToHtml(Tokenizer):
elif tType == BlockTyp.SKIP: elif tType == BlockTyp.SKIP:
lines.append(f"<p{hStyle}>&nbsp;</p>\n") lines.append(f"<p{hStyle}>&nbsp;</p>\n")
elif tType == BlockTyp.COMMENT: elif tType in COMMENT_BLOCKS:
lines.append(f"<p class='comment'{hStyle}>{self._formatText(tText, tFmt)}</p>\n") lines.append(f"<p class='comment'{hStyle}>{self._formatText(tText, tFmt)}</p>\n")
elif tType == BlockTyp.KEYWORD: elif tType == BlockTyp.KEYWORD:
+17 -5
View File
@@ -71,11 +71,23 @@ COMMENT_STYLE = {
nwComment.COMMENT: ComStyle(), nwComment.COMMENT: ComStyle(),
nwComment.STORY: ComStyle("Story Structure", "modifier", "note"), nwComment.STORY: ComStyle("Story Structure", "modifier", "note"),
} }
HEADINGS = [ COMMENT_TYPE = {
nwComment.PLAIN: BlockTyp.COMMENT,
nwComment.IGNORE: BlockTyp.COMMENT,
nwComment.SYNOPSIS: BlockTyp.SUMMARY,
nwComment.SHORT: BlockTyp.SUMMARY,
nwComment.NOTE: BlockTyp.NOTE,
nwComment.FOOTNOTE: BlockTyp.COMMENT,
nwComment.COMMENT: BlockTyp.COMMENT,
nwComment.STORY: BlockTyp.NOTE,
}
HEADING_BLOCKS = [
BlockTyp.TITLE, BlockTyp.PART, BlockTyp.HEAD1, BlockTyp.TITLE, BlockTyp.PART, BlockTyp.HEAD1,
BlockTyp.HEAD2, BlockTyp.HEAD3, BlockTyp.HEAD4, BlockTyp.HEAD2, BlockTyp.HEAD3, BlockTyp.HEAD4,
] ]
SKIP_INDENT = [*HEADINGS, BlockTyp.SEP, BlockTyp.SKIP] COMMENT_BLOCKS = (BlockTyp.COMMENT, BlockTyp.SUMMARY, BlockTyp.NOTE)
META_BLOCKS = (BlockTyp.COMMENT, BlockTyp.SUMMARY, BlockTyp.NOTE, BlockTyp.KEYWORD)
SKIP_INDENT = [*HEADING_BLOCKS, BlockTyp.SEP, BlockTyp.SKIP]
B_EMPTY: T_Block = (BlockTyp.EMPTY, "", "", [], BlockFmt.NONE) B_EMPTY: T_Block = (BlockTyp.EMPTY, "", "", [], BlockFmt.NONE)
@@ -619,7 +631,7 @@ class Tokenizer(ABC):
bStyle = COMMENT_STYLE[cStyle] bStyle = COMMENT_STYLE[cStyle]
tLine, tFmt = self._formatComment(bStyle, cKey, cText) tLine, tFmt = self._formatComment(bStyle, cKey, cText)
tBlocks.append(( tBlocks.append((
BlockTyp.COMMENT, "", tLine, tFmt, tStyle COMMENT_TYPE[cStyle], "", tLine, tFmt, tStyle
)) ))
elif cStyle == nwComment.FOOTNOTE: elif cStyle == nwComment.FOOTNOTE:
@@ -990,7 +1002,7 @@ class Tokenizer(ABC):
allWordChars += nPWChars allWordChars += nPWChars
textWordChars += nPWChars textWordChars += nPWChars
elif tType in HEADINGS: elif tType in HEADING_BLOCKS:
titleCount += 1 titleCount += 1
allWords += nWords allWords += nWords
titleWords += nWords titleWords += nWords
@@ -1004,7 +1016,7 @@ class Tokenizer(ABC):
allChars += nChars allChars += nChars
allWordChars += nWChars allWordChars += nWChars
elif tType in (BlockTyp.COMMENT, BlockTyp.KEYWORD): elif tType in META_BLOCKS:
words = tText.split() words = tText.split()
allWords += len(words) allWords += len(words)
allChars += len(tText) allChars += len(tText)
+2 -2
View File
@@ -29,7 +29,7 @@ from typing import TYPE_CHECKING
from novelwriter.constants import nwUnicode from novelwriter.constants import nwUnicode
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import COMMENT_BLOCKS, Tokenizer
if TYPE_CHECKING: if TYPE_CHECKING:
from pathlib import Path from pathlib import Path
@@ -144,7 +144,7 @@ class ToMarkdown(Tokenizer):
elif tType == BlockTyp.SKIP: elif tType == BlockTyp.SKIP:
lines.append(f"{cSkip}\n\n") lines.append(f"{cSkip}\n\n")
elif tType == BlockTyp.COMMENT: elif tType in COMMENT_BLOCKS:
lines.append(f"{self._formatText(tText, tFormat, mTags)}\n\n") lines.append(f"{self._formatText(tText, tFormat, mTags)}\n\n")
elif tType == BlockTyp.KEYWORD: elif tType == BlockTyp.KEYWORD:
+2 -2
View File
@@ -40,7 +40,7 @@ from novelwriter import __version__
from novelwriter.common import xmlElement, xmlIndent, xmlSubElem from novelwriter.common import xmlElement, xmlIndent, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwStyles from novelwriter.constants import nwHeadFmt, nwStyles
from novelwriter.formats.shared import BlockFmt, BlockTyp, TextFmt, stripEscape from novelwriter.formats.shared import BlockFmt, BlockTyp, TextFmt, stripEscape
from novelwriter.formats.tokenizer import Tokenizer from novelwriter.formats.tokenizer import COMMENT_BLOCKS, Tokenizer
from novelwriter.types import FONT_STYLE, QtHexRgb from novelwriter.types import FONT_STYLE, QtHexRgb
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -389,7 +389,7 @@ class ToOdt(Tokenizer):
elif tType == BlockTyp.SKIP: elif tType == BlockTyp.SKIP:
self._addTextPar(xText, S_TEXT, oStyle, "") self._addTextPar(xText, S_TEXT, oStyle, "")
elif tType == BlockTyp.COMMENT: elif tType in COMMENT_BLOCKS:
self._addTextPar(xText, S_META, oStyle, tText, tFmt=tFormat) self._addTextPar(xText, S_META, oStyle, tText, tFmt=tFormat)
elif tType == BlockTyp.KEYWORD: elif tType == BlockTyp.KEYWORD:
+8 -4
View File
@@ -37,7 +37,7 @@ from PyQt6.QtPrintSupport import QPrinter
from novelwriter import __version__ from novelwriter import __version__
from novelwriter.constants import nwStyles, nwUnicode from novelwriter.constants import nwStyles, nwUnicode
from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape from novelwriter.formats.shared import BlockFmt, BlockTyp, T_Formats, TextFmt, stripEscape
from novelwriter.formats.tokenizer import HEADINGS, Tokenizer from novelwriter.formats.tokenizer import HEADING_BLOCKS, META_BLOCKS, Tokenizer
from novelwriter.types import ( from novelwriter.types import (
QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight, QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight,
QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakAuto, QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakAuto,
@@ -214,7 +214,7 @@ class ToQTextDocument(Tokenizer):
for tType, tMeta, tText, tFormat, tStyle in self._blocks: for tType, tMeta, tText, tFormat, tStyle in self._blocks:
bFmt = QTextBlockFormat(self._blockFmt) bFmt = QTextBlockFormat(self._blockFmt)
if tType in (BlockTyp.COMMENT, BlockTyp.KEYWORD): if tType in META_BLOCKS:
bFmt.setTopMargin(self._mMeta[0]) bFmt.setTopMargin(self._mMeta[0])
bFmt.setBottomMargin(self._mMeta[1]) bFmt.setBottomMargin(self._mMeta[1])
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
@@ -248,17 +248,21 @@ class ToQTextDocument(Tokenizer):
if tStyle & BlockFmt.IND_T: if tStyle & BlockFmt.IND_T:
bFmt.setTextIndent(self._tIndent) bFmt.setTextIndent(self._tIndent)
if tType in (BlockTyp.TEXT, BlockTyp.COMMENT, BlockTyp.KEYWORD): if tType == BlockTyp.TEXT:
newBlock(cursor, bFmt) newBlock(cursor, bFmt)
self._insertFragments(tText, tFormat, cursor, self._charFmt) self._insertFragments(tText, tFormat, cursor, self._charFmt)
elif tType in HEADINGS: elif tType in HEADING_BLOCKS:
bFmt, cFmt = self._genHeadStyle(tType, tMeta, bFmt) bFmt, cFmt = self._genHeadStyle(tType, tMeta, bFmt)
for tPart in tText.split("\n"): for tPart in tText.split("\n"):
newBlock(cursor, bFmt) newBlock(cursor, bFmt)
cursor.insertText(tPart, cFmt) cursor.insertText(tPart, cFmt)
bFmt.setPageBreakPolicy(QtPageBreakAuto) bFmt.setPageBreakPolicy(QtPageBreakAuto)
elif tType in META_BLOCKS:
newBlock(cursor, bFmt)
self._insertFragments(tText, tFormat, cursor, self._charFmt)
elif tType == BlockTyp.SEP: elif tType == BlockTyp.SEP:
newBlock(cursor, bFmt) newBlock(cursor, bFmt)
cursor.insertText(tText, self._charFmt) cursor.insertText(tText, self._charFmt)
@@ -83,6 +83,7 @@ showedittoolbar = False
showsessiontime = True showsessiontime = True
viewcomments = True viewcomments = True
viewsynopsis = True viewsynopsis = True
viewnotes = True
searchcase = False searchcase = False
searchword = False searchword = False
searchregex = False searchregex = False
+3 -3
View File
@@ -783,7 +783,7 @@ def testFmtToken_MetaFormat(mockGUI):
tokens._text = "% synopsis: The synopsis\n" tokens._text = "% synopsis: The synopsis\n"
tokens.tokenizeText() tokens.tokenizeText()
assert tokens._blocks == [( assert tokens._blocks == [(
BlockTyp.COMMENT, "", "Synopsis: The synopsis", [ BlockTyp.SUMMARY, "", "Synopsis: The synopsis", [
(0, TextFmt.B_B, ""), (0, TextFmt.COL_B, "modifier"), (0, TextFmt.B_B, ""), (0, TextFmt.COL_B, "modifier"),
(9, TextFmt.COL_E, ""), (9, TextFmt.B_E, ""), (9, TextFmt.COL_E, ""), (9, TextFmt.B_E, ""),
(10, TextFmt.COL_B, "note"), (22, TextFmt.COL_E, "") (10, TextFmt.COL_B, "note"), (22, TextFmt.COL_E, "")
@@ -800,7 +800,7 @@ def testFmtToken_MetaFormat(mockGUI):
tokens._text = "% short: A short description\n" tokens._text = "% short: A short description\n"
tokens.tokenizeText() tokens.tokenizeText()
assert tokens._blocks == [( assert tokens._blocks == [(
BlockTyp.COMMENT, "", "Short Description: A short description", [ BlockTyp.SUMMARY, "", "Short Description: A short description", [
(0, TextFmt.B_B, ""), (0, TextFmt.COL_B, "modifier"), (0, TextFmt.B_B, ""), (0, TextFmt.COL_B, "modifier"),
(18, TextFmt.COL_E, ""), (18, TextFmt.B_E, ""), (18, TextFmt.COL_E, ""), (18, TextFmt.B_E, ""),
(19, TextFmt.COL_B, "note"), (38, TextFmt.COL_E, ""), (19, TextFmt.COL_B, "note"), (38, TextFmt.COL_E, ""),
@@ -1563,7 +1563,7 @@ def testFmtToken_TextIndent(mockGUI):
] ]
assert tokens._blocks == [ assert tokens._blocks == [
(BlockTyp.HEAD3, TM1, "Scene Two", [], BlockFmt.NONE), (BlockTyp.HEAD3, TM1, "Scene Two", [], BlockFmt.NONE),
(BlockTyp.COMMENT, "", "Synopsis: Stuff happens.", tFmt, BlockFmt.NONE), (BlockTyp.SUMMARY, "", "Synopsis: Stuff happens.", tFmt, BlockFmt.NONE),
] ]
assert tokens._noIndent is True assert tokens._noIndent is True