Add POV or Focus char field to manuscript header format

This commit is contained in:
Veronica Berglyd Olsen
2024-01-29 17:32:43 +01:00
parent a48a4e5674
commit cbf5f1ead3
4 changed files with 53 additions and 19 deletions
+34 -9
View File
@@ -37,7 +37,9 @@ from PyQt5.QtCore import QCoreApplication, QRegularExpression
from novelwriter.enum import nwComment, nwItemLayout
from novelwriter.common import formatTimeStamp, numberToRoman, checkInt
from novelwriter.constants import nwHeadFmt, nwRegEx, nwShortcode, nwUnicode
from novelwriter.constants import (
nwHeadFmt, nwKeyWords, nwLabels, nwRegEx, nwShortcode, nwUnicode, trConst
)
from novelwriter.core.index import processComment
from novelwriter.core.project import NWProject
@@ -112,13 +114,15 @@ class Tokenizer(ABC):
# Data Variables
self._text = "" # The raw text to be tokenized
self._nwItem = None # The NWItem associated with the handle
self._tokens = [] # The list of the processed tokens
self._nwItem = None # The NWItem currently being processed
self._result = "" # The result of the last document
self._keepMarkdown = False # Whether to keep the markdown text
self._allMarkdown = [] # The result novelWriter markdown of all documents
# Processed Tokens
self._tokens: list[tuple[int, int, str, list[tuple[int, int]] | None, int]] = []
# User Settings
self._textFont = "Serif" # Output text font
self._textSize = 11 # Output text size
@@ -655,6 +659,8 @@ class Tokenizer(ABC):
if not self._isNovel:
return False
self._hFormatter.setHandle(self._nwItem.itemHandle if self._nwItem else None)
for n, tToken in enumerate(self._tokens):
# In case we see text before a scene, we reset the flag
@@ -664,7 +670,7 @@ class Tokenizer(ABC):
elif tToken[0] == self.T_HEAD1:
# Partition
tTemp = self._hFormatter.apply(self._fmtTitle, tToken[2])
tTemp = self._hFormatter.apply(self._fmtTitle, tToken[2], tToken[1])
self._tokens[n] = (
tToken[0], tToken[1], tTemp, None, tToken[4]
)
@@ -674,10 +680,10 @@ class Tokenizer(ABC):
# Numbered or Unnumbered
if tToken[0] == self.T_UNNUM:
tTemp = self._hFormatter.apply(self._fmtUnNum, tToken[2])
tTemp = self._hFormatter.apply(self._fmtUnNum, tToken[2], tToken[1])
else:
self._hFormatter.incChapter()
tTemp = self._hFormatter.apply(self._fmtChapter, tToken[2])
tTemp = self._hFormatter.apply(self._fmtChapter, tToken[2], tToken[1])
# Format the chapter header
self._tokens[n] = (
@@ -693,7 +699,7 @@ class Tokenizer(ABC):
self._hFormatter.incScene()
tTemp = self._hFormatter.apply(self._fmtScene, tToken[2])
tTemp = self._hFormatter.apply(self._fmtScene, tToken[2], tToken[1])
if tTemp == "" and self._hideScene:
self._tokens[n] = (
self.T_EMPTY, tToken[1], "", None, self.A_NONE
@@ -726,7 +732,7 @@ class Tokenizer(ABC):
elif tToken[0] == self.T_HEAD4:
# Section
tTemp = self._hFormatter.apply(self._fmtSection, tToken[2])
tTemp = self._hFormatter.apply(self._fmtSection, tToken[2], tToken[1])
if tTemp == "" and self._hideSection:
self._tokens[n] = (
self.T_EMPTY, tToken[1], "", None, self.A_NONE
@@ -817,11 +823,17 @@ class HeadingFormatter:
def __init__(self, project: NWProject) -> None:
self._project = project
self._handle = None
self._chCount = 0
self._scChCount = 0
self._scAbsCount = 0
return
def setHandle(self, tHandle: str | None) -> None:
"""Set the handle currently being processed."""
self._handle = tHandle
return
def incChapter(self) -> None:
"""Increment the chapter counter."""
self._chCount += 1
@@ -838,7 +850,7 @@ class HeadingFormatter:
self._scChCount = 0
return
def apply(self, hFormat: str, text: str) -> str:
def apply(self, hFormat: str, text: str, nHead: int) -> str:
"""Apply formatting to a specific heading."""
hFormat = hFormat.replace(nwHeadFmt.TITLE, text)
hFormat = hFormat.replace(nwHeadFmt.CH_NUM, str(self._chCount))
@@ -854,6 +866,19 @@ class HeadingFormatter:
chRom = numberToRoman(self._chCount, toLower=False)
hFormat = hFormat.replace(nwHeadFmt.CH_ROMU, chRom)
if nwHeadFmt.CHAR_POV in hFormat or nwHeadFmt.CHAR_FOCUS in hFormat:
if self._handle and nHead > 0:
refs = self._project.index.getReferences(self._handle, f"T{nHead:04d}")
povData = refs[nwKeyWords.POV_KEY]
focData = refs[nwKeyWords.FOCUS_KEY]
povText = povData[0] if povData else nwUnicode.U_ENDASH
focText = focData[0] if focData else nwUnicode.U_ENDASH
else:
povText = trConst(nwLabels.KEY_NAME[nwKeyWords.POV_KEY])
focText = trConst(nwLabels.KEY_NAME[nwKeyWords.FOCUS_KEY])
hFormat = hFormat.replace(nwHeadFmt.CHAR_POV, povText)
hFormat = hFormat.replace(nwHeadFmt.CHAR_FOCUS, focText)
return hFormat
# END Class HeadingFormatter