Standardise the save document call for all document classes
This commit is contained in:
@@ -178,10 +178,7 @@ class NWBuildDocument:
|
||||
self._cache = makeObj
|
||||
|
||||
try:
|
||||
if isFlat:
|
||||
makeObj.saveFlatXML(path)
|
||||
else:
|
||||
makeObj.saveOpenDocText(path)
|
||||
makeObj.saveDocument(path)
|
||||
except Exception as exc:
|
||||
logException()
|
||||
self._error = formatException(exc)
|
||||
@@ -213,10 +210,7 @@ class NWBuildDocument:
|
||||
|
||||
if isinstance(path, Path):
|
||||
try:
|
||||
if asJson:
|
||||
makeObj.saveHtmlJson(path)
|
||||
else:
|
||||
makeObj.saveHtml5(path)
|
||||
makeObj.saveDocument(path, asJson=asJson)
|
||||
except Exception as exc:
|
||||
logException()
|
||||
self._error = formatException(exc)
|
||||
@@ -246,7 +240,7 @@ class NWBuildDocument:
|
||||
self._cache = makeObj
|
||||
|
||||
try:
|
||||
makeObj.saveMarkdown(path)
|
||||
makeObj.saveDocument(path)
|
||||
except Exception as exc:
|
||||
logException()
|
||||
self._error = formatException(exc)
|
||||
@@ -276,10 +270,7 @@ class NWBuildDocument:
|
||||
|
||||
if isinstance(path, Path):
|
||||
try:
|
||||
if asJson:
|
||||
makeObj.saveRawMarkdownJSON(path)
|
||||
else:
|
||||
makeObj.saveRawMarkdown(path)
|
||||
makeObj.saveRawDocument(path, asJson=asJson)
|
||||
except Exception as exc:
|
||||
logException()
|
||||
self._error = formatException(exc)
|
||||
|
||||
@@ -290,51 +290,51 @@ class ToHtml(Tokenizer):
|
||||
|
||||
return
|
||||
|
||||
def saveHtml5(self, path: str | Path) -> None:
|
||||
def saveDocument(self, path: str | Path, asJson: bool = False) -> None:
|
||||
"""Save the data to an HTML file."""
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
fObj.write((
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html>\n"
|
||||
"<head>\n"
|
||||
"<meta charset='utf-8'>\n"
|
||||
"<title>{title:s}</title>\n"
|
||||
"</head>\n"
|
||||
"<style>\n"
|
||||
"{style:s}\n"
|
||||
"</style>\n"
|
||||
"<body>\n"
|
||||
"<article>\n"
|
||||
"{body:s}\n"
|
||||
"</article>\n"
|
||||
"</body>\n"
|
||||
"</html>\n"
|
||||
).format(
|
||||
title=self._project.data.name,
|
||||
style="\n".join(self.getStyleSheet()),
|
||||
body=("".join(self._fullHTML)).replace("\t", "	").rstrip(),
|
||||
))
|
||||
logger.info("Wrote file: %s", path)
|
||||
return
|
||||
|
||||
def saveHtmlJson(self, path: str | Path) -> None:
|
||||
"""Save the data to a JSON file."""
|
||||
timeStamp = time()
|
||||
data = {
|
||||
"meta": {
|
||||
"projectName": self._project.data.name,
|
||||
"novelAuthor": self._project.data.author,
|
||||
"buildTime": int(timeStamp),
|
||||
"buildTimeStr": formatTimeStamp(timeStamp),
|
||||
},
|
||||
"text": {
|
||||
"css": self.getStyleSheet(),
|
||||
"html": [t.replace("\t", "	").rstrip().split("\n") for t in self.fullHTML],
|
||||
if asJson:
|
||||
ts = time()
|
||||
data = {
|
||||
"meta": {
|
||||
"projectName": self._project.data.name,
|
||||
"novelAuthor": self._project.data.author,
|
||||
"buildTime": int(ts),
|
||||
"buildTimeStr": formatTimeStamp(ts),
|
||||
},
|
||||
"text": {
|
||||
"css": self.getStyleSheet(),
|
||||
"html": [t.replace("\t", "	").rstrip().split("\n") for t in self.fullHTML],
|
||||
}
|
||||
}
|
||||
}
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
json.dump(data, fObj, indent=2)
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
json.dump(data, fObj, indent=2)
|
||||
|
||||
else:
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
fObj.write((
|
||||
"<!DOCTYPE html>\n"
|
||||
"<html>\n"
|
||||
"<head>\n"
|
||||
"<meta charset='utf-8'>\n"
|
||||
"<title>{title:s}</title>\n"
|
||||
"</head>\n"
|
||||
"<style>\n"
|
||||
"{style:s}\n"
|
||||
"</style>\n"
|
||||
"<body>\n"
|
||||
"<article>\n"
|
||||
"{body:s}\n"
|
||||
"</article>\n"
|
||||
"</body>\n"
|
||||
"</html>\n"
|
||||
).format(
|
||||
title=self._project.data.name,
|
||||
style="\n".join(self.getStyleSheet()),
|
||||
body=("".join(self._fullHTML)).replace("\t", "	").rstrip(),
|
||||
))
|
||||
|
||||
logger.info("Wrote file: %s", path)
|
||||
|
||||
return
|
||||
|
||||
def replaceTabs(self, nSpaces: int = 8, spaceChar: str = " ") -> None:
|
||||
|
||||
@@ -486,6 +486,10 @@ class Tokenizer(ABC):
|
||||
def doConvert(self) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def saveDocument(self, path: str | Path) -> None:
|
||||
raise NotImplementedError
|
||||
|
||||
def addRootHeading(self, tHandle: str) -> None:
|
||||
"""Add a heading at the start of a new root folder."""
|
||||
self._text = ""
|
||||
@@ -1087,29 +1091,31 @@ class Tokenizer(ABC):
|
||||
|
||||
return
|
||||
|
||||
def saveRawMarkdown(self, path: str | Path) -> None:
|
||||
def saveRawDocument(self, path: str | Path, asJson: bool = False) -> None:
|
||||
"""Save the raw text to a plain text file."""
|
||||
with open(path, mode="w", encoding="utf-8") as outFile:
|
||||
for nwdPage in self._markdown:
|
||||
outFile.write(nwdPage)
|
||||
return
|
||||
|
||||
def saveRawMarkdownJSON(self, path: str | Path) -> None:
|
||||
"""Save the raw text to a JSON file."""
|
||||
timeStamp = time()
|
||||
data = {
|
||||
"meta": {
|
||||
"projectName": self._project.data.name,
|
||||
"novelAuthor": self._project.data.author,
|
||||
"buildTime": int(timeStamp),
|
||||
"buildTimeStr": formatTimeStamp(timeStamp),
|
||||
},
|
||||
"text": {
|
||||
"nwd": [page.rstrip("\n").split("\n") for page in self._markdown],
|
||||
if asJson:
|
||||
ts = time()
|
||||
data = {
|
||||
"meta": {
|
||||
"projectName": self._project.data.name,
|
||||
"novelAuthor": self._project.data.author,
|
||||
"buildTime": int(ts),
|
||||
"buildTimeStr": formatTimeStamp(ts),
|
||||
},
|
||||
"text": {
|
||||
"nwd": [page.rstrip("\n").split("\n") for page in self._markdown],
|
||||
}
|
||||
}
|
||||
}
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
json.dump(data, fObj, indent=2)
|
||||
with open(path, mode="w", encoding="utf-8") as fObj:
|
||||
json.dump(data, fObj, indent=2)
|
||||
|
||||
else:
|
||||
with open(path, mode="w", encoding="utf-8") as outFile:
|
||||
for nwdPage in self._markdown:
|
||||
outFile.write(nwdPage)
|
||||
|
||||
logger.info("Wrote file: %s", path)
|
||||
|
||||
return
|
||||
|
||||
##
|
||||
|
||||
@@ -199,7 +199,7 @@ class ToMarkdown(Tokenizer):
|
||||
|
||||
return
|
||||
|
||||
def saveMarkdown(self, path: str | Path) -> None:
|
||||
def saveDocument(self, path: str | Path) -> None:
|
||||
"""Save the data to a plain text file."""
|
||||
with open(path, mode="w", encoding="utf-8") as outFile:
|
||||
outFile.write("".join(self._fullMD))
|
||||
|
||||
@@ -543,46 +543,44 @@ class ToOdt(Tokenizer):
|
||||
self._xText.insert(0, xFields)
|
||||
return
|
||||
|
||||
def saveFlatXML(self, path: str | Path) -> None:
|
||||
"""Save the data to an .fodt file."""
|
||||
with open(path, mode="wb") as fObj:
|
||||
xml = ET.ElementTree(self._dFlat)
|
||||
xmlIndent(xml)
|
||||
xml.write(fObj, encoding="utf-8", xml_declaration=True)
|
||||
logger.info("Wrote file: %s", path)
|
||||
return
|
||||
|
||||
def saveOpenDocText(self, path: str | Path) -> None:
|
||||
"""Save the data to an .odt file."""
|
||||
mMani = _mkTag("manifest", "manifest")
|
||||
mVers = _mkTag("manifest", "version")
|
||||
mPath = _mkTag("manifest", "full-path")
|
||||
mType = _mkTag("manifest", "media-type")
|
||||
mFile = _mkTag("manifest", "file-entry")
|
||||
|
||||
xMani = ET.Element(mMani, attrib={mVers: X_VERS})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "/", mVers: X_VERS, mType: X_MIME})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "settings.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "content.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "meta.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "styles.xml", mType: "text/xml"})
|
||||
|
||||
oRoot = _mkTag("office", "document-settings")
|
||||
oVers = _mkTag("office", "version")
|
||||
xSett = ET.Element(oRoot, attrib={oVers: X_VERS})
|
||||
|
||||
def putInZip(name: str, xObj: ET.Element, zipObj: ZipFile) -> None:
|
||||
with zipObj.open(name, mode="w") as fObj:
|
||||
xml = ET.ElementTree(xObj)
|
||||
def saveDocument(self, path: str | Path) -> None:
|
||||
"""Save the data to an .fodt or .odt file."""
|
||||
if self._isFlat:
|
||||
with open(path, mode="wb") as fObj:
|
||||
xml = ET.ElementTree(self._dFlat)
|
||||
xmlIndent(xml)
|
||||
xml.write(fObj, encoding="utf-8", xml_declaration=True)
|
||||
|
||||
with ZipFile(path, mode="w") as outZip:
|
||||
outZip.writestr("mimetype", X_MIME)
|
||||
putInZip("META-INF/manifest.xml", xMani, outZip)
|
||||
putInZip("settings.xml", xSett, outZip)
|
||||
putInZip("content.xml", self._dCont, outZip)
|
||||
putInZip("meta.xml", self._dMeta, outZip)
|
||||
putInZip("styles.xml", self._dStyl, outZip)
|
||||
else:
|
||||
mMani = _mkTag("manifest", "manifest")
|
||||
mVers = _mkTag("manifest", "version")
|
||||
mPath = _mkTag("manifest", "full-path")
|
||||
mType = _mkTag("manifest", "media-type")
|
||||
mFile = _mkTag("manifest", "file-entry")
|
||||
|
||||
xMani = ET.Element(mMani, attrib={mVers: X_VERS})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "/", mVers: X_VERS, mType: X_MIME})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "settings.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "content.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "meta.xml", mType: "text/xml"})
|
||||
ET.SubElement(xMani, mFile, attrib={mPath: "styles.xml", mType: "text/xml"})
|
||||
|
||||
oRoot = _mkTag("office", "document-settings")
|
||||
oVers = _mkTag("office", "version")
|
||||
xSett = ET.Element(oRoot, attrib={oVers: X_VERS})
|
||||
|
||||
def putInZip(name: str, xObj: ET.Element, zipObj: ZipFile) -> None:
|
||||
with zipObj.open(name, mode="w") as fObj:
|
||||
xml = ET.ElementTree(xObj)
|
||||
xml.write(fObj, encoding="utf-8", xml_declaration=True)
|
||||
|
||||
with ZipFile(path, mode="w") as outZip:
|
||||
outZip.writestr("mimetype", X_MIME)
|
||||
putInZip("META-INF/manifest.xml", xMani, outZip)
|
||||
putInZip("settings.xml", xSett, outZip)
|
||||
putInZip("content.xml", self._dCont, outZip)
|
||||
putInZip("meta.xml", self._dMeta, outZip)
|
||||
putInZip("styles.xml", self._dStyl, outZip)
|
||||
|
||||
logger.info("Wrote file: %s", path)
|
||||
|
||||
|
||||
@@ -25,6 +25,8 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from PyQt5.QtGui import (
|
||||
QColor, QFont, QFontMetricsF, QTextBlockFormat, QTextCharFormat,
|
||||
QTextCursor, QTextDocument
|
||||
@@ -273,6 +275,10 @@ class ToQTextDocument(Tokenizer):
|
||||
|
||||
return
|
||||
|
||||
def saveDocument(self, path: str | Path) -> None:
|
||||
"""Not implemented."""
|
||||
return
|
||||
|
||||
def appendFootnotes(self) -> None:
|
||||
"""Append the footnotes in the buffer."""
|
||||
if self._usedNotes:
|
||||
|
||||
Reference in New Issue
Block a user