Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add word counts to ODT output #2035

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions novelwriter/core/docbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ def lastBuild(self) -> Tokenizer | None:
# Setters
##

def setCountEnabled(self, state: bool) -> None:
"""Turn on/off stats for builds."""
self._count = state
return

def setBuildOutline(self, state: bool) -> None:
"""Turn on/off outline for builds."""
self._outline = state
Expand Down Expand Up @@ -130,8 +125,8 @@ def iterBuildPreview(self, theme: TextDocumentTheme) -> Iterable[tuple[int, bool
makeObj = ToQTextDocument(self._project)
filtered = self._setupBuild(makeObj)

self._outline = True
self._count = True
self._outline = True

font = QFont()
font.fromString(self._build.getStr("format.textFont"))
Expand Down Expand Up @@ -169,6 +164,7 @@ def iterBuildOpenDocument(self, path: Path, isFlat: bool) -> Iterable[tuple[int,
filtered = self._setupBuild(makeObj)
makeObj.initDocument()

self._count = True
for i, tHandle in enumerate(self._queue):
self._error = None
if filtered.get(tHandle, (False, 0))[0]:
Expand Down Expand Up @@ -199,6 +195,7 @@ def iterBuildHTML(self, path: Path | None, asJson: bool = False) -> Iterable[tup
makeObj = ToHtml(self._project)
filtered = self._setupBuild(makeObj)

self._count = False
for i, tHandle in enumerate(self._queue):
self._error = None
if filtered.get(tHandle, (False, 0))[0]:
Expand Down Expand Up @@ -235,6 +232,7 @@ def iterBuildMarkdown(self, path: Path, extendedMd: bool) -> Iterable[tuple[int,
if self._build.getBool("format.replaceTabs"):
makeObj.replaceTabs(nSpaces=4, spaceChar=" ")

self._count = False
for i, tHandle in enumerate(self._queue):
self._error = None
if filtered.get(tHandle, (False, 0))[0]:
Expand Down Expand Up @@ -262,6 +260,7 @@ def iterBuildNWD(self, path: Path | None, asJson: bool = False) -> Iterable[tupl

makeObj.setKeepMarkdown(True)

self._count = False
for i, tHandle in enumerate(self._queue):
self._error = None
if filtered.get(tHandle, (False, 0))[0]:
Expand Down
11 changes: 10 additions & 1 deletion novelwriter/core/toodt.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,20 @@ def doConvert(self) -> None:
return

def closeDocument(self) -> None:
"""Pack the automatic styles of the XML document."""
"""Add additional collected information to the XML."""
for style in self._autoPara.values():
style.packXML(self._xAuto)
for style in self._autoText.values():
style.packXML(self._xAuto)
if self._counts:
xFields = ET.Element(_mkTag("text", "user-field-decls"))
for key, value in self._counts.items():
ET.SubElement(xFields, _mkTag("text", "user-field-decl"), attrib={
_mkTag("office", "value-type"): "float",
_mkTag("office", "value"): str(value),
_mkTag("text", "name"): f"Manuscript{key[:1].upper()}{key[1:]}",
})
self._xText.insert(0, xFields)
return

def saveFlatXML(self, path: str | Path) -> None:
Expand Down
19 changes: 16 additions & 3 deletions tests/reference/mBuildDocBuild_OpenDocument_Lorem_Ipsum.fodt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version='1.0' encoding='utf-8'?>
<office:document xmlns:dc="http://purl.org/dc/elements/1.1/" 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:meta="urn:oasis:names:tc:opendocument:xmlns:meta: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:mimetype="application/vnd.oasis.opendocument.text">
<office:meta>
<meta:creation-date>2024-05-24T21:31:12</meta:creation-date>
<meta:generator>novelWriter/2.5a4</meta:generator>
<meta:creation-date>2024-09-30T14:08:16</meta:creation-date>
<meta:generator>novelWriter/2.6a0</meta:generator>
<meta:initial-creator>lipsum.com</meta:initial-creator>
<meta:editing-cycles>45</meta:editing-cycles>
<meta:editing-duration>P0DT0H36M8S</meta:editing-duration>
<dc:title>Lorem Ipsum</dc:title>
<dc:date>2024-05-24T21:31:12</dc:date>
<dc:date>2024-09-30T14:08:16</dc:date>
<dc:creator>lipsum.com</dc:creator>
</office:meta>
<office:font-face-decls>
Expand Down Expand Up @@ -116,6 +116,19 @@
</office:master-styles>
<office:body>
<office:text>
<text:user-field-decls>
<text:user-field-decl office:value-type="float" office:value="19" text:name="ManuscriptTitleCount" />
<text:user-field-decl office:value-type="float" office:value="45" text:name="ManuscriptParagraphCount" />
<text:user-field-decl office:value-type="float" office:value="4163" text:name="ManuscriptAllWords" />
<text:user-field-decl office:value-type="float" office:value="3809" text:name="ManuscriptTextWords" />
<text:user-field-decl office:value-type="float" office:value="54" text:name="ManuscriptTitleWords" />
<text:user-field-decl office:value-type="float" office:value="27849" text:name="ManuscriptAllChars" />
<text:user-field-decl office:value-type="float" office:value="25528" text:name="ManuscriptTextChars" />
<text:user-field-decl office:value-type="float" office:value="311" text:name="ManuscriptTitleChars" />
<text:user-field-decl office:value-type="float" office:value="23782" text:name="ManuscriptAllWordChars" />
<text:user-field-decl office:value-type="float" office:value="21761" text:name="ManuscriptTextWordChars" />
<text:user-field-decl office:value-type="float" office:value="276" text:name="ManuscriptTitleWordChars" />
</text:user-field-decls>
<text:p text:style-name="Title">Lorem Ipsum</text:p>
<text:p text:style-name="P1"><text:span text:style-name="T1">By lipsum.com</text:span></text:p>
<text:p text:style-name="P1">“Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit…”</text:p>
Expand Down
2 changes: 0 additions & 2 deletions tests/test_core/test_core_docbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ def testCoreDocBuild_OpenDocument(monkeypatch, mockGUI, prjLipsum, fncPath, tstP
build.unpack(BUILD_CONF)

docBuild = NWBuildDocument(project, build)
docBuild.setCountEnabled(True)
docBuild.setBuildOutline(True)
docBuild.queueAll()

assert docBuild._count is True
assert docBuild._outline is True

assert len(docBuild) == 21
Expand Down