Move custom XML sub element function to common

This commit is contained in:
Veronica Berglyd Olsen
2024-10-19 17:08:08 +02:00
parent ca33f488f1
commit 34127d78bf
2 changed files with 36 additions and 23 deletions
+26
View File
@@ -204,6 +204,14 @@ def checkIntTuple(value: int, valid: tuple | list | set, default: int) -> int:
return default return default
def firstFloat(*args: Any) -> float:
"""Return the first value that is a float."""
for arg in args:
if isinstance(arg, float):
return arg
return 0.0
## ##
# Formatting Functions # Formatting Functions
## ##
@@ -515,6 +523,24 @@ def xmlIndent(tree: ET.Element | ET.ElementTree) -> None:
return return
def xmlSubElem(
parent: ET.Element,
tag: str,
text: str | int | float | bool | None = None,
attrib: dict | None = None
) -> ET.Element:
"""A custom implementation of SubElement that takes text as an
argument.
"""
xSub = ET.SubElement(parent, tag, attrib=attrib or {})
if text is not None:
if isinstance(text, bool):
xSub.text = str(text).lower()
else:
xSub.text = str(text)
return xSub
## ##
# File and File System Functions # File and File System Functions
## ##
+10 -23
View File
@@ -38,7 +38,7 @@ from zipfile import ZipFile
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from novelwriter import __version__ from novelwriter import __version__
from novelwriter.common import xmlIndent from novelwriter.common import xmlIndent, xmlSubElem
from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels, nwStyles from novelwriter.constants import nwHeadFmt, nwKeyWords, nwLabels, nwStyles
from novelwriter.core.project import NWProject from novelwriter.core.project import NWProject
from novelwriter.formats.tokenizer import T_Formats, Tokenizer, stripEscape from novelwriter.formats.tokenizer import T_Formats, Tokenizer, stripEscape
@@ -69,19 +69,6 @@ def _mkTag(ns: str, tag: str) -> str:
return tag return tag
def _addSingle(
parent: ET.Element,
tag: str,
text: str | int | None = None,
attrib: dict | None = None
) -> None:
"""Add a single value to a parent element."""
xSub = ET.SubElement(parent, tag, attrib=attrib or {})
if text is not None:
xSub.text = str(text)
return
# Mimetype and Version # Mimetype and Version
X_MIME = "application/vnd.oasis.opendocument.text" X_MIME = "application/vnd.oasis.opendocument.text"
X_VERS = "1.3" X_VERS = "1.3"
@@ -411,21 +398,21 @@ class ToOdt(Tokenizer):
timeStamp = datetime.now().isoformat(sep="T", timespec="seconds") timeStamp = datetime.now().isoformat(sep="T", timespec="seconds")
# Office Meta Data # Office Meta Data
_addSingle(self._xMeta, _mkTag("meta", "creation-date"), timeStamp) xmlSubElem(self._xMeta, _mkTag("meta", "creation-date"), timeStamp)
_addSingle(self._xMeta, _mkTag("meta", "generator"), f"novelWriter/{__version__}") xmlSubElem(self._xMeta, _mkTag("meta", "generator"), f"novelWriter/{__version__}")
_addSingle(self._xMeta, _mkTag("meta", "initial-creator"), self._project.data.author) xmlSubElem(self._xMeta, _mkTag("meta", "initial-creator"), self._project.data.author)
_addSingle(self._xMeta, _mkTag("meta", "editing-cycles"), self._project.data.saveCount) xmlSubElem(self._xMeta, _mkTag("meta", "editing-cycles"), self._project.data.saveCount)
# Format is: PnYnMnDTnHnMnS # Format is: PnYnMnDTnHnMnS
# https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#duration # https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#duration
eT = self._project.data.editTime eT = self._project.data.editTime
fT = f"P{eT//86400:d}DT{eT%86400//3600:d}H{eT%3600//60:d}M{eT%60:d}S" fT = f"P{eT//86400:d}DT{eT%86400//3600:d}H{eT%3600//60:d}M{eT%60:d}S"
_addSingle(self._xMeta, _mkTag("meta", "editing-duration"), fT) xmlSubElem(self._xMeta, _mkTag("meta", "editing-duration"), fT)
# Dublin Core Meta Data # Dublin Core Meta Data
_addSingle(self._xMeta, _mkTag("dc", "title"), self._project.data.name) xmlSubElem(self._xMeta, _mkTag("dc", "title"), self._project.data.name)
_addSingle(self._xMeta, _mkTag("dc", "date"), timeStamp) xmlSubElem(self._xMeta, _mkTag("dc", "date"), timeStamp)
_addSingle(self._xMeta, _mkTag("dc", "creator"), self._project.data.author) xmlSubElem(self._xMeta, _mkTag("dc", "creator"), self._project.data.author)
self._pageStyles() self._pageStyles()
self._defaultStyles() self._defaultStyles()
@@ -795,7 +782,7 @@ class ToOdt(Tokenizer):
_mkTag("text", "id"): f"ftn{self._nNote}", _mkTag("text", "id"): f"ftn{self._nNote}",
_mkTag("text", "note-class"): "footnote", _mkTag("text", "note-class"): "footnote",
}) })
_addSingle(xNote, _mkTag("text", "note-citation"), self._nNote) xmlSubElem(xNote, _mkTag("text", "note-citation"), self._nNote)
xBody = ET.SubElement(xNote, _mkTag("text", "note-body")) xBody = ET.SubElement(xNote, _mkTag("text", "note-body"))
self._addTextPar(xBody, "Footnote", nStyle, content[0], tFmt=content[1]) self._addTextPar(xBody, "Footnote", nStyle, content[0], tFmt=content[1])
return xNote return xNote