From ce19de31f5428326b4f7f2f8de037bd37e4bc962 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 31 May 2020 14:11:35 +0200 Subject: [PATCH] Added build files to JSON with HTML or markdown formatting --- nw/gui/build.py | 63 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/nw/gui/build.py b/nw/gui/build.py index 6c424ddb..2616ff83 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -53,12 +53,14 @@ logger = logging.getLogger(__name__) class GuiBuildNovel(QDialog): - FMT_ODT = 1 - FMT_PDF = 2 - FMT_HTM = 3 - FMT_MD = 4 - FMT_NWD = 5 - FMT_TXT = 6 + FMT_ODT = 1 + FMT_PDF = 2 + FMT_HTM = 3 + FMT_MD = 4 + FMT_NWD = 5 + FMT_TXT = 6 + FMT_JSON_H = 7 + FMT_JSON_M = 8 def __init__(self, theParent, theProject): QDialog.__init__(self, theParent) @@ -334,7 +336,7 @@ class GuiBuildNovel(QDialog): self.saveMD.triggered.connect(lambda: self._saveDocument(self.FMT_MD)) self.saveMenu.addAction(self.saveMD) - self.saveNWD = QAction("novelWriter Markdown (.nwd)", self) + self.saveNWD = QAction("%s Markdown (.nwd)" % nw.__package__, self) self.saveNWD.triggered.connect(lambda: self._saveDocument(self.FMT_NWD)) self.saveMenu.addAction(self.saveNWD) @@ -342,6 +344,14 @@ class GuiBuildNovel(QDialog): self.saveTXT.triggered.connect(lambda: self._saveDocument(self.FMT_TXT)) self.saveMenu.addAction(self.saveTXT) + self.saveJsonH = QAction("JSON + %s HTML (.json)" % nw.__package__, self) + self.saveJsonH.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_H)) + self.saveMenu.addAction(self.saveJsonH) + + self.saveJsonM = QAction("JSON + %s Markdown (.json)" % nw.__package__, self) + self.saveJsonM.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_M)) + self.saveMenu.addAction(self.saveJsonM) + self.btnClose = QPushButton("Close") self.btnClose.clicked.connect(self._doClose) @@ -546,7 +556,7 @@ class GuiBuildNovel(QDialog): elif theFormat == self.FMT_NWD: fileExt = "nwd" - textFmt = "%s markdown" % nw.__package__ + textFmt = "%s Markdown" % nw.__package__ outTool = "NW" elif theFormat == self.FMT_TXT: @@ -555,6 +565,16 @@ class GuiBuildNovel(QDialog): textFmt = "Plain Text" outTool = "Qt" + elif theFormat == self.FMT_JSON_H: + fileExt = "json" + textFmt = "JSON + %s HTML" % nw.__package__ + outTool = "NW" + + elif theFormat == self.FMT_JSON_M: + fileExt = "json" + textFmt = "JSON + %s Markdown" % nw.__package__ + outTool = "NW" + else: return False @@ -628,6 +648,33 @@ class GuiBuildNovel(QDialog): for aLine in self.nwdText: outFile.write(aLine) + elif theFormat == self.FMT_JSON_H or theFormat == self.FMT_JSON_M: + jsonData = { + "meta" : { + "workingTitle" : self.theProject.projName, + "novelTitle" : self.theProject.bookTitle, + "authors" : self.theProject.bookAuthors, + } + } + + if theFormat == self.FMT_JSON_H: + theBody = [] + for htmlPage in self.htmlText: + theBody.append(htmlPage.rstrip("\n").split("\n")) + jsonData["text"] = { + "css" : self.htmlStyle, + "html" : theBody, + } + elif theFormat == self.FMT_JSON_M: + theBody = [] + for nwdPage in self.nwdText: + theBody.append(nwdPage.split("\n")) + jsonData["text"] = { + "nwd" : theBody, + } + + outFile.write(json.dumps(jsonData, indent=2)) + wSuccess = True except Exception as e: