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.constants import nwHeadFmt, nwStyles
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
if TYPE_CHECKING:
@@ -296,7 +296,7 @@ class ToDocX(Tokenizer):
elif tType == BlockTyp.SKIP:
self._processFragments(par, S_NORM, "")
elif tType == BlockTyp.COMMENT:
elif tType in COMMENT_BLOCKS:
self._processFragments(par, S_META, tText, tFormat)
elif tType == BlockTyp.KEYWORD:
+2 -2
View File
@@ -32,7 +32,7 @@ from typing import TYPE_CHECKING
from novelwriter.common import formatTimeStamp
from novelwriter.constants import nwHtmlUnicode, nwStyles
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
if TYPE_CHECKING:
@@ -228,7 +228,7 @@ class ToHtml(Tokenizer):
elif tType == BlockTyp.SKIP:
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")
elif tType == BlockTyp.KEYWORD:
+17 -5
View File
@@ -71,11 +71,23 @@ COMMENT_STYLE = {
nwComment.COMMENT: ComStyle(),
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.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)
@@ -619,7 +631,7 @@ class Tokenizer(ABC):
bStyle = COMMENT_STYLE[cStyle]
tLine, tFmt = self._formatComment(bStyle, cKey, cText)
tBlocks.append((
BlockTyp.COMMENT, "", tLine, tFmt, tStyle
COMMENT_TYPE[cStyle], "", tLine, tFmt, tStyle
))
elif cStyle == nwComment.FOOTNOTE:
@@ -990,7 +1002,7 @@ class Tokenizer(ABC):
allWordChars += nPWChars
textWordChars += nPWChars
elif tType in HEADINGS:
elif tType in HEADING_BLOCKS:
titleCount += 1
allWords += nWords
titleWords += nWords
@@ -1004,7 +1016,7 @@ class Tokenizer(ABC):
allChars += nChars
allWordChars += nWChars
elif tType in (BlockTyp.COMMENT, BlockTyp.KEYWORD):
elif tType in META_BLOCKS:
words = tText.split()
allWords += len(words)
allChars += len(tText)
+2 -2
View File
@@ -29,7 +29,7 @@ from typing import TYPE_CHECKING
from novelwriter.constants import nwUnicode
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:
from pathlib import Path
@@ -144,7 +144,7 @@ class ToMarkdown(Tokenizer):
elif tType == BlockTyp.SKIP:
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")
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.constants import nwHeadFmt, nwStyles
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
if TYPE_CHECKING:
@@ -389,7 +389,7 @@ class ToOdt(Tokenizer):
elif tType == BlockTyp.SKIP:
self._addTextPar(xText, S_TEXT, oStyle, "")
elif tType == BlockTyp.COMMENT:
elif tType in COMMENT_BLOCKS:
self._addTextPar(xText, S_META, oStyle, tText, tFmt=tFormat)
elif tType == BlockTyp.KEYWORD:
+8 -4
View File
@@ -37,7 +37,7 @@ from PyQt6.QtPrintSupport import QPrinter
from novelwriter import __version__
from novelwriter.constants import nwStyles, nwUnicode
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 (
QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight,
QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakAuto,
@@ -214,7 +214,7 @@ class ToQTextDocument(Tokenizer):
for tType, tMeta, tText, tFormat, tStyle in self._blocks:
bFmt = QTextBlockFormat(self._blockFmt)
if tType in (BlockTyp.COMMENT, BlockTyp.KEYWORD):
if tType in META_BLOCKS:
bFmt.setTopMargin(self._mMeta[0])
bFmt.setBottomMargin(self._mMeta[1])
elif tType == BlockTyp.SEP:
@@ -248,17 +248,21 @@ class ToQTextDocument(Tokenizer):
if tStyle & BlockFmt.IND_T:
bFmt.setTextIndent(self._tIndent)
if tType in (BlockTyp.TEXT, BlockTyp.COMMENT, BlockTyp.KEYWORD):
if tType == BlockTyp.TEXT:
newBlock(cursor, bFmt)
self._insertFragments(tText, tFormat, cursor, self._charFmt)
elif tType in HEADINGS:
elif tType in HEADING_BLOCKS:
bFmt, cFmt = self._genHeadStyle(tType, tMeta, bFmt)
for tPart in tText.split("\n"):
newBlock(cursor, bFmt)
cursor.insertText(tPart, cFmt)
bFmt.setPageBreakPolicy(QtPageBreakAuto)
elif tType in META_BLOCKS:
newBlock(cursor, bFmt)
self._insertFragments(tText, tFormat, cursor, self._charFmt)
elif tType == BlockTyp.SEP:
newBlock(cursor, bFmt)
cursor.insertText(tText, self._charFmt)