Convert format codes to enums

This commit is contained in:
Veronica Berglyd Olsen
2024-10-21 23:37:27 +02:00
parent f91006631d
commit 38ca447c8a
12 changed files with 817 additions and 789 deletions
+49 -47
View File
@@ -41,7 +41,9 @@ from novelwriter import __version__
from novelwriter.common import xmlIndent, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels, nwStyles
from novelwriter.core.project import NWProject
from novelwriter.formats.tokenizer import T_Formats, Tokenizer, stripEscape
from novelwriter.formats.tokenizer import (
BlockFmt, BlockTyp, T_Formats, TextFmt, Tokenizer, stripEscape
)
from novelwriter.types import FONT_STYLE, FONT_WEIGHTS
logger = logging.getLogger(__name__)
@@ -429,79 +431,79 @@ class ToOdt(Tokenizer):
# Styles
oStyle = ODTParagraphStyle("New")
if tStyle is not None:
if tStyle & self.A_LEFT:
if tStyle & BlockFmt.LEFT:
oStyle.setTextAlign("left")
elif tStyle & self.A_RIGHT:
elif tStyle & BlockFmt.RIGHT:
oStyle.setTextAlign("right")
elif tStyle & self.A_CENTRE:
elif tStyle & BlockFmt.CENTRE:
oStyle.setTextAlign("center")
elif tStyle & self.A_JUSTIFY:
elif tStyle & BlockFmt.JUSTIFY:
oStyle.setTextAlign("justify")
if tStyle & self.A_PBB:
if tStyle & BlockFmt.PBB:
oStyle.setBreakBefore("page")
if tStyle & self.A_PBA:
if tStyle & BlockFmt.PBA:
oStyle.setBreakAfter("page")
if tStyle & self.A_Z_BTMMRG:
if tStyle & BlockFmt.Z_BTMMRG:
oStyle.setMarginBottom("0.000cm")
if tStyle & self.A_Z_TOPMRG:
if tStyle & BlockFmt.Z_TOPMRG:
oStyle.setMarginTop("0.000cm")
if tStyle & self.A_IND_L:
if tStyle & BlockFmt.IND_L:
oStyle.setMarginLeft(self._fBlockIndent)
if tStyle & self.A_IND_R:
if tStyle & BlockFmt.IND_R:
oStyle.setMarginRight(self._fBlockIndent)
# Process Text Types
if tType == self.T_TEXT:
if tType == BlockTyp.TEXT:
# Text indentation is processed here because there is a
# dedicated pre-defined style for it
if tStyle & self.A_IND_T:
if tStyle & BlockFmt.IND_T:
self._addTextPar(xText, S_FIND, oStyle, tText, tFmt=tFormat)
else:
self._addTextPar(xText, S_TEXT, oStyle, tText, tFmt=tFormat)
elif tType == self.T_TITLE:
elif tType == BlockTyp.TITLE:
# Title must be text:p
tHead = tText.replace(nwHeadFmt.BR, "\n")
self._addTextPar(xText, S_TITLE, oStyle, tHead, isHead=False)
elif tType == self.T_HEAD1:
elif tType == BlockTyp.HEAD1:
tHead = tText.replace(nwHeadFmt.BR, "\n")
self._addTextPar(xText, S_HEAD1, oStyle, tHead, isHead=True, oLevel="1")
elif tType == self.T_HEAD2:
elif tType == BlockTyp.HEAD2:
tHead = tText.replace(nwHeadFmt.BR, "\n")
self._addTextPar(xText, S_HEAD2, oStyle, tHead, isHead=True, oLevel="2")
elif tType == self.T_HEAD3:
elif tType == BlockTyp.HEAD3:
tHead = tText.replace(nwHeadFmt.BR, "\n")
self._addTextPar(xText, S_HEAD3, oStyle, tHead, isHead=True, oLevel="3")
elif tType == self.T_HEAD4:
elif tType == BlockTyp.HEAD4:
tHead = tText.replace(nwHeadFmt.BR, "\n")
self._addTextPar(xText, S_HEAD4, oStyle, tHead, isHead=True, oLevel="4")
elif tType == self.T_SEP:
elif tType == BlockTyp.SEP:
self._addTextPar(xText, S_SEP, oStyle, tText)
elif tType == self.T_SKIP:
elif tType == BlockTyp.SKIP:
self._addTextPar(xText, S_TEXT, oStyle, "")
elif tType == self.T_SYNOPSIS and self._doSynopsis:
elif tType == BlockTyp.SYNOPSIS and self._doSynopsis:
tTemp, tFmt = self._formatSynopsis(tText, tFormat, True)
self._addTextPar(xText, S_META, oStyle, tTemp, tFmt=tFmt)
elif tType == self.T_SHORT and self._doSynopsis:
elif tType == BlockTyp.SHORT and self._doSynopsis:
tTemp, tFmt = self._formatSynopsis(tText, tFormat, False)
self._addTextPar(xText, S_META, oStyle, tTemp, tFmt=tFmt)
elif tType == self.T_COMMENT and self._doComments:
elif tType == BlockTyp.COMMENT and self._doComments:
tTemp, tFmt = self._formatComments(tText, tFormat)
self._addTextPar(xText, S_META, oStyle, tTemp, tFmt=tFmt)
elif tType == self.T_KEYWORD and self._doKeywords:
elif tType == BlockTyp.KEYWORD and self._doKeywords:
tTemp, tFmt = self._formatKeywords(tText)
self._addTextPar(xText, S_META, oStyle, tTemp, tFmt=tFmt)
@@ -576,7 +578,7 @@ class ToOdt(Tokenizer):
name = self._localLookup("Synopsis" if synopsis else "Short Description")
shift = len(name) + 2
rTxt = f"{name}: {text}"
rFmt: T_Formats = [(0, self.FMT_B_B, ""), (len(name) + 1, self.FMT_B_E, "")]
rFmt: T_Formats = [(0, TextFmt.B_B, ""), (len(name) + 1, TextFmt.B_E, "")]
rFmt.extend((p + shift, f, d) for p, f, d in fmt)
return rTxt, rFmt
@@ -585,7 +587,7 @@ class ToOdt(Tokenizer):
name = self._localLookup("Comment")
shift = len(name) + 2
rTxt = f"{name}: {text}"
rFmt: T_Formats = [(0, self.FMT_B_B, ""), (len(name) + 1, self.FMT_B_E, "")]
rFmt: T_Formats = [(0, TextFmt.B_B, ""), (len(name) + 1, TextFmt.B_E, "")]
rFmt.extend((p + shift, f, d) for p, f, d in fmt)
return rTxt, rFmt
@@ -596,7 +598,7 @@ class ToOdt(Tokenizer):
return "", []
rTxt = f"{self._localLookup(nwLabels.KEY_NAME[bits[0]])}: "
rFmt: T_Formats = [(0, self.FMT_B_B, ""), (len(rTxt) - 1, self.FMT_B_E, "")]
rFmt: T_Formats = [(0, TextFmt.B_B, ""), (len(rTxt) - 1, TextFmt.B_E, "")]
if len(bits) > 1:
if bits[0] == nwKeyWords.TAG_KEY:
rTxt += bits[1]
@@ -654,45 +656,45 @@ class ToOdt(Tokenizer):
parProc.appendSpan(tFrag, self._textStyle(xFmt))
# Calculate the change of format
if fFmt == self.FMT_B_B:
if fFmt == TextFmt.B_B:
xFmt |= X_BLD
elif fFmt == self.FMT_B_E:
elif fFmt == TextFmt.B_E:
xFmt &= M_BLD
elif fFmt == self.FMT_I_B:
elif fFmt == TextFmt.I_B:
xFmt |= X_ITA
elif fFmt == self.FMT_I_E:
elif fFmt == TextFmt.I_E:
xFmt &= M_ITA
elif fFmt == self.FMT_D_B:
elif fFmt == TextFmt.D_B:
xFmt |= X_DEL
elif fFmt == self.FMT_D_E:
elif fFmt == TextFmt.D_E:
xFmt &= M_DEL
elif fFmt == self.FMT_U_B:
elif fFmt == TextFmt.U_B:
xFmt |= X_UND
elif fFmt == self.FMT_U_E:
elif fFmt == TextFmt.U_E:
xFmt &= M_UND
elif fFmt == self.FMT_M_B:
elif fFmt == TextFmt.M_B:
xFmt |= X_MRK
elif fFmt == self.FMT_M_E:
elif fFmt == TextFmt.M_E:
xFmt &= M_MRK
elif fFmt == self.FMT_SUP_B:
elif fFmt == TextFmt.SUP_B:
xFmt |= X_SUP
elif fFmt == self.FMT_SUP_E:
elif fFmt == TextFmt.SUP_E:
xFmt &= M_SUP
elif fFmt == self.FMT_SUB_B:
elif fFmt == TextFmt.SUB_B:
xFmt |= X_SUB
elif fFmt == self.FMT_SUB_E:
elif fFmt == TextFmt.SUB_E:
xFmt &= M_SUB
elif fFmt == self.FMT_DL_B:
elif fFmt == TextFmt.DL_B:
xFmt |= X_DLG
elif fFmt == self.FMT_DL_E:
elif fFmt == TextFmt.DL_E:
xFmt &= M_DLG
elif fFmt == self.FMT_ADL_B:
elif fFmt == TextFmt.ADL_B:
xFmt |= X_DLA
elif fFmt == self.FMT_ADL_E:
elif fFmt == TextFmt.ADL_E:
xFmt &= M_DLA
elif fFmt == self.FMT_FNOTE:
elif fFmt == TextFmt.FNOTE:
xNode = self._generateFootnote(fData)
elif fFmt == self.FMT_STRIP:
elif fFmt == TextFmt.STRIP:
pass
else:
pErr += 1