Generalise ODT build settings

This commit is contained in:
Veronica Berglyd Olsen
2024-10-15 20:34:41 +02:00
parent b56095224a
commit b2c7da319e
10 changed files with 81 additions and 84 deletions
+7 -7
View File
@@ -96,11 +96,11 @@ SETTINGS_TEMPLATE: dict[str, tuple[type, T_Basic]] = {
"format.bottomMargin": (float, 2.0),
"format.leftMargin": (float, 2.0),
"format.rightMargin": (float, 2.0),
"odt.addColours": (bool, True),
"odt.scaleHeadings": (bool, True),
"odt.boldHeadings": (bool, True),
"odt.pageHeader": (str, nwHeadFmt.ODT_AUTO),
"odt.pageCountOffset": (int, 0),
"odt.colorHeadings": (bool, True),
"odt.scaleHeadings": (bool, True),
"odt.boldHeadings": (bool, True),
"html.addStyles": (bool, True),
"html.preserveTabs": (bool, False),
}
@@ -155,12 +155,12 @@ SETTINGS_LABELS = {
"format.leftMargin": QT_TRANSLATE_NOOP("Builds", "Left Margin"),
"format.rightMargin": QT_TRANSLATE_NOOP("Builds", "Right Margin"),
"odt": QT_TRANSLATE_NOOP("Builds", "ODT Options"),
"odt.addColours": QT_TRANSLATE_NOOP("Builds", "Add Highlight Colours"),
"odt.scaleHeadings": QT_TRANSLATE_NOOP("Builds", "Increase Size of Headings"),
"odt.boldHeadings": QT_TRANSLATE_NOOP("Builds", "Bold Headings"),
"odt": QT_TRANSLATE_NOOP("Builds", "Document Options"),
"odt.pageHeader": QT_TRANSLATE_NOOP("Builds", "Page Header"),
"odt.pageCountOffset": QT_TRANSLATE_NOOP("Builds", "Page Counter Offset"),
"odt.colorHeadings": QT_TRANSLATE_NOOP("Builds", "Add Colours to Headings"),
"odt.scaleHeadings": QT_TRANSLATE_NOOP("Builds", "Increase Size of Headings"),
"odt.boldHeadings": QT_TRANSLATE_NOOP("Builds", "Bold Headings"),
"html": QT_TRANSLATE_NOOP("Builds", "HTML Options"),
"html.addStyles": QT_TRANSLATE_NOOP("Builds", "Add CSS Styles"),
+5 -5
View File
@@ -347,6 +347,11 @@ class NWBuildDocument:
self._build.getFloat("format.firstIndentWidth"),
self._build.getBool("format.indentFirstPar"),
)
bldObj.setHeadingStyles(
self._build.getBool("odt.colorHeadings"),
self._build.getBool("odt.scaleHeadings"),
self._build.getBool("odt.boldHeadings"),
)
bldObj.setBodyText(self._build.getBool("text.includeBodyText"))
bldObj.setSynopsis(self._build.getBool("text.includeSynopsis"))
@@ -359,11 +364,6 @@ class NWBuildDocument:
bldObj.setReplaceUnicode(self._build.getBool("format.stripUnicode"))
if isinstance(bldObj, ToOdt):
bldObj.setColoursEnabled(self._build.getBool("odt.addColours"))
bldObj.setHeadingStyles(
self._build.getBool("odt.scaleHeadings"),
self._build.getBool("odt.boldHeadings"),
)
bldObj.setLanguage(self._project.data.language)
bldObj.setHeaderFormat(
self._build.getStr("odt.pageHeader"),
+22 -12
View File
@@ -150,18 +150,21 @@ class Tokenizer(ABC):
# User Settings
self._textFont = QFont("Serif", 11) # Output text font
self._lineHeight = 1.15 # Line height in units of em
self._blockIndent = 4.00 # Block indent in units of em
self._firstIndent = False # Enable first line indent
self._firstWidth = 1.40 # First line indent in units of em
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._doKeywords = False # Also process keywords like tags and references
self._skipKeywords = set() # Keywords to ignore
self._keepBreaks = True # Keep line breaks in paragraphs
self._lineHeight = 1.15 # Line height in units of em
self._colorHeads = True # Colourise headings
self._scaleHeads = True # Scale headings to larger font size
self._boldHeads = True # Bold headings
self._blockIndent = 4.00 # Block indent in units of em
self._firstIndent = False # Enable first line indent
self._firstWidth = 1.40 # First line indent in units of em
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._doKeywords = False # Also process keywords like tags and references
self._skipKeywords = set() # Keywords to ignore
self._keepBreaks = True # Keep line breaks in paragraphs
# Margins
self._marginTitle = (1.417, 0.500)
@@ -346,6 +349,13 @@ class Tokenizer(ABC):
self._lineHeight = min(max(float(height), 0.5), 5.0)
return
def setHeadingStyles(self, color: bool, scale: bool, bold: bool) -> None:
"""Set text style for headings."""
self._colorHeads = color
self._scaleHeads = scale
self._boldHeads = bold
return
def setBlockIndent(self, indent: float) -> None:
"""Set the block indent between 0.0 and 10.0."""
self._blockIndent = min(max(float(indent), 0.0), 10.0)
+4 -18
View File
@@ -161,8 +161,6 @@ class ToOdt(Tokenizer):
# Properties
self._textFont = QFont("Liberation Serif", 12)
self._addColours = False
self._scaleHeads = True
self._headWeight = "bold"
self._headerFormat = ""
self._pageOffset = 0
@@ -226,10 +224,10 @@ class ToOdt(Tokenizer):
self._opaHead12 = None
self._colHead34 = None
self._opaHead34 = None
self._colMetaTx = None
self._opaMetaTx = None
self._colDialogM = None
self._colDialogA = None
self._colMetaTx = "#813709"
self._opaMetaTx = "100%"
self._markText = "#ffffa6"
return
@@ -246,17 +244,6 @@ class ToOdt(Tokenizer):
self._dCountry = country or self._dCountry
return
def setHeadingStyles(self, scale: bool, bold: bool) -> None:
"""Set plain text style for headings."""
self._scaleHeads = scale
self._headWeight = self._fontBold if bold else None
return
def setColoursEnabled(self, state: bool) -> None:
"""Enable/disable coloured headings and comments."""
self._addColours = state
return
def setPageLayout(
self, width: int | float, height: int | float,
top: int | float, bottom: int | float, left: int | float, right: int | float
@@ -295,6 +282,7 @@ class ToOdt(Tokenizer):
self._fontStyle = FONT_STYLE.get(self._textFont.style(), "normal")
self._fontPitch = "fixed" if self._textFont.fixedPitch() else "variable"
self._fontBold = FONT_WEIGHT_MAP.get(fontBold, fontBold)
self._headWeight = self._fontBold if self._boldHeads else None
hScale = self._scaleHeads
self._fSizeTitle = f"{round((2.50 if hScale else 1.0) * self._fontSize):d}pt"
@@ -331,13 +319,11 @@ class ToOdt(Tokenizer):
self._mLeftFoot = self._emToCm(self._marginFoot[0])
self._mBotFoot = self._emToCm(self._marginFoot[1])
if self._addColours:
if self._colorHeads:
self._colHead12 = "#2a6099"
self._opaHead12 = "100%"
self._colHead34 = "#444444"
self._opaHead34 = "100%"
self._colMetaTx = "#813709"
self._opaMetaTx = "100%"
if self._showDialog:
self._colDialogM = "#2a6099"
+8 -7
View File
@@ -114,12 +114,13 @@ class ToQTextDocument(Tokenizer):
self.T_HEAD4: (mPx * self._marginHead4[0], mPx * self._marginHead4[1]),
}
hScale = self._scaleHeads
self._sHead = {
self.T_TITLE: nwHeaders.H_SIZES.get(0, 1.0) * fPt,
self.T_HEAD1: nwHeaders.H_SIZES.get(1, 1.0) * fPt,
self.T_HEAD2: nwHeaders.H_SIZES.get(2, 1.0) * fPt,
self.T_HEAD3: nwHeaders.H_SIZES.get(3, 1.0) * fPt,
self.T_HEAD4: nwHeaders.H_SIZES.get(4, 1.0) * fPt,
self.T_TITLE: (nwHeaders.H_SIZES.get(0, 1.0) * fPt) if hScale else fPt,
self.T_HEAD1: (nwHeaders.H_SIZES.get(1, 1.0) * fPt) if hScale else fPt,
self.T_HEAD2: (nwHeaders.H_SIZES.get(2, 1.0) * fPt) if hScale else fPt,
self.T_HEAD3: (nwHeaders.H_SIZES.get(3, 1.0) * fPt) if hScale else fPt,
self.T_HEAD4: (nwHeaders.H_SIZES.get(4, 1.0) * fPt) if hScale else fPt,
}
self._mText = (mPx * self._marginText[0], mPx * self._marginText[1])
@@ -148,7 +149,8 @@ class ToQTextDocument(Tokenizer):
self._cText.setForeground(self._theme.text)
self._cHead = QTextCharFormat(self._cText)
self._cHead.setForeground(self._theme.head)
self._cHead.setForeground(self._theme.head if self._colorHeads else self._theme.text)
self._cHead.setFontWeight(self._bold if self._boldHeads else self._normal)
self._cComment = QTextCharFormat(self._cText)
self._cComment.setForeground(self._theme.comment)
@@ -410,7 +412,6 @@ class ToQTextDocument(Tokenizer):
bFmt.setBottomMargin(mBottom)
cFmt = QTextCharFormat(self._cText if hType == self.T_TITLE else self._cHead)
cFmt.setFontWeight(self._bold)
cFmt.setFontPointSize(self._sHead.get(hType, 1.0))
if nHead >= 0:
cFmt.setAnchorNames([f"{self._handle}:T{nHead:04d}"])
+12 -10
View File
@@ -1122,14 +1122,7 @@ class _FormattingTab(NScrollableForm):
self._sidebar.addButton(title, section)
self.addGroupLabel(title, section)
self.odtAddColours = NSwitch(self, height=iPx)
self.scaleHeadings = NSwitch(self, height=iPx)
self.boldHeadings = NSwitch(self, height=iPx)
self.addRow(self._build.getLabel("odt.addColours"), self.odtAddColours)
self.addRow(self._build.getLabel("odt.scaleHeadings"), self.scaleHeadings)
self.addRow(self._build.getLabel("odt.boldHeadings"), self.boldHeadings)
# Header
self.odtPageHeader = QLineEdit(self)
self.odtPageHeader.setMinimumWidth(CONFIG.pxInt(200))
self.btnPageHeader = NIconToolButton(self, iSz, "revert")
@@ -1146,6 +1139,15 @@ class _FormattingTab(NScrollableForm):
self.odtPageCountOffset.setMinimumWidth(spW)
self.addRow(self._build.getLabel("odt.pageCountOffset"), self.odtPageCountOffset)
# Headings
self.colorHeadings = NSwitch(self, height=iPx)
self.scaleHeadings = NSwitch(self, height=iPx)
self.boldHeadings = NSwitch(self, height=iPx)
self.addRow(self._build.getLabel("odt.colorHeadings"), self.colorHeadings)
self.addRow(self._build.getLabel("odt.scaleHeadings"), self.scaleHeadings)
self.addRow(self._build.getLabel("odt.boldHeadings"), self.boldHeadings)
# HTML Document
# =============
@@ -1230,7 +1232,7 @@ class _FormattingTab(NScrollableForm):
# ODT Document
# ============
self.odtAddColours.setChecked(self._build.getBool("odt.addColours"))
self.colorHeadings.setChecked(self._build.getBool("odt.colorHeadings"))
self.scaleHeadings.setChecked(self._build.getBool("odt.scaleHeadings"))
self.boldHeadings.setChecked(self._build.getBool("odt.boldHeadings"))
self.odtPageHeader.setText(self._build.getStr("odt.pageHeader"))
@@ -1282,7 +1284,7 @@ class _FormattingTab(NScrollableForm):
self._build.setValue("format.rightMargin", self.rightMargin.value())
# ODT Document
self._build.setValue("odt.addColours", self.odtAddColours.isChecked())
self._build.setValue("odt.colorHeadings", self.colorHeadings.isChecked())
self._build.setValue("odt.scaleHeadings", self.scaleHeadings.isChecked())
self._build.setValue("odt.boldHeadings", self.boldHeadings.isChecked())
self._build.setValue("odt.pageHeader", self.odtPageHeader.text())
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<office:document-styles xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3">
<office:document-styles xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3">
<office:font-face-decls>
<style:font-face style:name="Liberation Serif" style:font-pitch="variable" />
</office:font-face-decls>
@@ -25,7 +25,7 @@
</style:style>
<style:style style:name="Text_20_Meta" style:family="paragraph" style:display-name="Text Meta" style:parent-style-name="Standard" style:class="text">
<style:paragraph-properties fo:margin-top="0.000cm" fo:margin-bottom="0.247cm" fo:line-height="115%" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="12pt" fo:font-weight="normal" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="12pt" fo:font-weight="normal" fo:color="#813709" loext:opacity="100%" />
</style:style>
<style:style style:name="Title" style:family="paragraph" style:display-name="Title" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:class="chapter">
<style:paragraph-properties fo:margin-top="0.600cm" fo:margin-bottom="0.212cm" fo:text-align="center" />
@@ -37,19 +37,19 @@
</style:style>
<style:style style:name="Heading_20_1" style:family="paragraph" style:display-name="Heading 1" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="1" style:class="text">
<style:paragraph-properties fo:margin-top="0.600cm" fo:margin-bottom="0.212cm" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="24pt" fo:font-weight="bold" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="24pt" fo:font-weight="bold" fo:color="#2a6099" loext:opacity="100%" />
</style:style>
<style:style style:name="Heading_20_2" style:family="paragraph" style:display-name="Heading 2" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="2" style:class="text">
<style:paragraph-properties fo:margin-top="0.706cm" fo:margin-bottom="0.212cm" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="19pt" fo:font-weight="bold" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="19pt" fo:font-weight="bold" fo:color="#2a6099" loext:opacity="100%" />
</style:style>
<style:style style:name="Heading_20_3" style:family="paragraph" style:display-name="Heading 3" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="3" style:class="text">
<style:paragraph-properties fo:margin-top="0.494cm" fo:margin-bottom="0.212cm" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="16pt" fo:font-weight="bold" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="16pt" fo:font-weight="bold" fo:color="#444444" loext:opacity="100%" />
</style:style>
<style:style style:name="Heading_20_4" style:family="paragraph" style:display-name="Heading 4" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="4" style:class="text">
<style:paragraph-properties fo:margin-top="0.494cm" fo:margin-bottom="0.212cm" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="14pt" fo:font-weight="bold" />
<style:text-properties style:font-name="Liberation Serif" fo:font-family="Liberation Serif" fo:font-size="14pt" fo:font-weight="bold" fo:color="#444444" loext:opacity="100%" />
</style:style>
<style:style style:name="Header" style:family="paragraph" style:display-name="Header" style:parent-style-name="Header_20_and_20_Footer">
<style:paragraph-properties fo:text-align="right" />
+1 -1
View File
@@ -64,7 +64,7 @@ BUILD_CONF = {
"format.stripUnicode": False,
"format.replaceTabs": True,
"format.firstLineIndent": True,
"odt.addColours": True,
"odt.colorHeadings": True,
"html.addStyles": True,
},
"content": {
-2
View File
@@ -772,8 +772,6 @@ def testFmtToOdt_SaveFlat(mockGUI, fncPath, tstPaths):
assert odt._dLanguage == ""
odt.setLanguage("nb_NO")
assert odt._dLanguage == "nb"
odt.setColoursEnabled(True)
assert odt._addColours is True
odt.setHeaderFormat(nwHeadFmt.ODT_AUTO, 1)
assert odt._headerFormat == nwHeadFmt.ODT_AUTO
odt.setFirstLineIndent(True, 1.4, False)
+16 -16
View File
@@ -707,11 +707,11 @@ def testToolBuildSettings_FormatOutput(qtbot, nwGUI):
"""Test the format-specific settings."""
build = BuildSettings()
build.setValue("odt.addColours", True)
build.setValue("odt.scaleHeadings", True)
build.setValue("odt.boldHeadings", True)
build.setValue("odt.pageHeader", nwHeadFmt.ODT_AUTO)
build.setValue("odt.pageCountOffset", 0)
build.setValue("odt.colorHeadings", True)
build.setValue("odt.scaleHeadings", True)
build.setValue("odt.boldHeadings", True)
build.setValue("html.addStyles", False)
build.setValue("html.preserveTabs", False)
@@ -726,34 +726,34 @@ def testToolBuildSettings_FormatOutput(qtbot, nwGUI):
assert bSettings.toolStack.currentWidget() is fmtTab
# Check initial values
assert fmtTab.odtAddColours.isChecked() is True
assert fmtTab.scaleHeadings.isChecked() is True
assert fmtTab.boldHeadings.isChecked() is True
assert fmtTab.odtPageHeader.text() == nwHeadFmt.ODT_AUTO
assert fmtTab.odtPageCountOffset.value() == 0
assert fmtTab.colorHeadings.isChecked() is True
assert fmtTab.scaleHeadings.isChecked() is True
assert fmtTab.boldHeadings.isChecked() is True
assert fmtTab.htmlAddStyles.isChecked() is False
assert fmtTab.htmlPreserveTabs.isChecked() is False
# Toggle all
fmtTab.odtAddColours.setChecked(False)
fmtTab.scaleHeadings.setChecked(False)
fmtTab.boldHeadings.setChecked(False)
fmtTab.htmlAddStyles.setChecked(True)
fmtTab.htmlPreserveTabs.setChecked(True)
# Change Values
fmtTab.odtPageCountOffset.setValue(1)
fmtTab.odtPageHeader.setText("Stuff")
# Toggle all
fmtTab.colorHeadings.setChecked(False)
fmtTab.scaleHeadings.setChecked(False)
fmtTab.boldHeadings.setChecked(False)
fmtTab.htmlAddStyles.setChecked(True)
fmtTab.htmlPreserveTabs.setChecked(True)
# Save values
fmtTab.saveContent()
assert build.getBool("odt.addColours") is False
assert build.getBool("odt.scaleHeadings") is False
assert build.getBool("odt.boldHeadings") is False
assert build.getStr("odt.pageHeader") == "Stuff"
assert build.getInt("odt.pageCountOffset") == 1
assert build.getBool("odt.colorHeadings") is False
assert build.getBool("odt.scaleHeadings") is False
assert build.getBool("odt.boldHeadings") is False
assert build.getBool("html.addStyles") is True
assert build.getBool("html.preserveTabs") is True