Connect markdown exporter to build tool

This commit is contained in:
Veronica K. B. Olsen
2021-02-06 15:20:03 +01:00
parent c7a42a1641
commit 9861f294b7
2 changed files with 59 additions and 45 deletions
+2
View File
@@ -6,6 +6,7 @@ from nw.core.project import NWProject
from nw.core.spellcheck import NWSpellCheck, NWSpellEnchant, NWSpellSimple from nw.core.spellcheck import NWSpellCheck, NWSpellEnchant, NWSpellSimple
from nw.core.tohtml import ToHtml from nw.core.tohtml import ToHtml
from nw.core.toodt import ToOdt from nw.core.toodt import ToOdt
from nw.core.tomd import ToMarkdown
from nw.core.tools import countWords, numberToRoman, numberToWord from nw.core.tools import countWords, numberToRoman, numberToWord
__all__ = [ __all__ = [
@@ -20,4 +21,5 @@ __all__ = [
"NWSpellSimple", "NWSpellSimple",
"ToHtml", "ToHtml",
"ToOdt", "ToOdt",
"ToMarkdown",
] ]
+57 -45
View File
@@ -46,7 +46,7 @@ from PyQt5.QtWidgets import (
from nw.common import fuzzyTime, makeFileNameSafe from nw.common import fuzzyTime, makeFileNameSafe
from nw.gui.custom import QSwitch from nw.gui.custom import QSwitch
from nw.core import ToHtml, ToOdt from nw.core import ToHtml, ToOdt, ToMarkdown
from nw.constants import ( from nw.constants import (
nwConst, nwAlert, nwFiles, nwItemType, nwItemLayout, nwItemClass nwConst, nwAlert, nwFiles, nwItemType, nwItemLayout, nwItemClass
) )
@@ -60,10 +60,11 @@ class GuiBuildNovel(QDialog):
FMT_PDF = 3 FMT_PDF = 3
FMT_HTM = 4 FMT_HTM = 4
FMT_MD = 5 FMT_MD = 5
FMT_NWD = 6 FMT_GH = 6
FMT_TXT = 7 FMT_NWD = 7
FMT_JSON_H = 8 FMT_TXT = 8
FMT_JSON_M = 9 FMT_JSON_H = 9
FMT_JSON_M = 10
def __init__(self, theParent, theProject): def __init__(self, theParent, theProject):
QDialog.__init__(self, theParent) QDialog.__init__(self, theParent)
@@ -403,10 +404,13 @@ class GuiBuildNovel(QDialog):
self.saveNWD.triggered.connect(lambda: self._saveDocument(self.FMT_NWD)) self.saveNWD.triggered.connect(lambda: self._saveDocument(self.FMT_NWD))
self.saveMenu.addAction(self.saveNWD) self.saveMenu.addAction(self.saveNWD)
if self.mainConf.verQtValue >= 51400: self.saveMD = QAction("Standard Markdown (.md)", self)
self.saveMD = QAction("Markdown (.md)", self) self.saveMD.triggered.connect(lambda: self._saveDocument(self.FMT_MD))
self.saveMD.triggered.connect(lambda: self._saveDocument(self.FMT_MD)) self.saveMenu.addAction(self.saveMD)
self.saveMenu.addAction(self.saveMD)
self.saveGH = QAction("GitHub Markdown (.md)", self)
self.saveGH.triggered.connect(lambda: self._saveDocument(self.FMT_GH))
self.saveMenu.addAction(self.saveGH)
self.saveTXT = QAction("Plain Text (.txt)", self) self.saveTXT = QAction("Plain Text (.txt)", self)
self.saveTXT.triggered.connect(lambda: self._saveDocument(self.FMT_TXT)) self.saveTXT.triggered.connect(lambda: self._saveDocument(self.FMT_TXT))
@@ -619,6 +623,7 @@ class GuiBuildNovel(QDialog):
isHtml = isinstance(bldObj, ToHtml) isHtml = isinstance(bldObj, ToHtml)
isOdt = isinstance(bldObj, ToOdt) isOdt = isinstance(bldObj, ToOdt)
# isMd = isinstance(bldObj, ToMarkdown)
bldObj.setTitleFormat(fmtTitle) bldObj.setTitleFormat(fmtTitle)
bldObj.setChapterFormat(fmtChapter) bldObj.setChapterFormat(fmtChapter)
@@ -739,7 +744,7 @@ class GuiBuildNovel(QDialog):
return True return True
def _saveDocument(self, theFormat): def _saveDocument(self, theFmt):
"""Save the document to various formats. """Save the document to various formats.
""" """
replaceTabs = self.replaceTabs.isChecked() replaceTabs = self.replaceTabs.isChecked()
@@ -751,43 +756,46 @@ class GuiBuildNovel(QDialog):
# Settings # Settings
# ======== # ========
if theFormat == self.FMT_ODT: if theFmt == self.FMT_ODT:
fileExt = "odt" fileExt = "odt"
textFmt = "Open Document" textFmt = "Open Document"
elif theFormat == self.FMT_FODT: elif theFmt == self.FMT_FODT:
fileExt = "fodt" fileExt = "fodt"
textFmt = "Flat Open Document" textFmt = "Flat Open Document"
elif theFormat == self.FMT_PDF: elif theFmt == self.FMT_PDF:
fileExt = "pdf" fileExt = "pdf"
textFmt = "PDF" textFmt = "PDF"
elif theFormat == self.FMT_HTM: elif theFmt == self.FMT_HTM:
fileExt = "htm" fileExt = "htm"
textFmt = "Plain HTML" textFmt = "Plain HTML"
elif theFormat == self.FMT_MD: elif theFmt == self.FMT_MD:
byteFmt.append("markdown")
fileExt = "md" fileExt = "md"
textFmt = "Markdown" textFmt = "Standard Markdown"
elif theFormat == self.FMT_NWD: elif theFmt == self.FMT_GH:
fileExt = "md"
textFmt = "GitHub Markdown"
elif theFmt == self.FMT_NWD:
fileExt = "nwd" fileExt = "nwd"
textFmt = "%s Markdown" % nw.__package__ textFmt = "novelWriter Markdown"
elif theFormat == self.FMT_TXT: elif theFmt == self.FMT_TXT:
byteFmt.append("plaintext") byteFmt.append("plaintext")
fileExt = "txt" fileExt = "txt"
textFmt = "Plain Text" textFmt = "Plain Text"
elif theFormat == self.FMT_JSON_H: elif theFmt == self.FMT_JSON_H:
fileExt = "json" fileExt = "json"
textFmt = "JSON + %s HTML" % nw.__package__ textFmt = "JSON + novelWriter HTML"
elif theFormat == self.FMT_JSON_M: elif theFmt == self.FMT_JSON_M:
fileExt = "json" fileExt = "json"
textFmt = "JSON + %s Markdown" % nw.__package__ textFmt = "JSON + novelWriter Markdown"
else: else:
return False return False
@@ -823,13 +831,13 @@ class GuiBuildNovel(QDialog):
errMsg = "" errMsg = ""
wSuccess = False wSuccess = False
if theFormat == self.FMT_MD or theFormat == self.FMT_TXT: if theFmt == self.FMT_TXT:
docWriter = QTextDocumentWriter() docWriter = QTextDocumentWriter()
docWriter.setFileName(savePath) docWriter.setFileName(savePath)
docWriter.setFormat(byteFmt) docWriter.setFormat(byteFmt)
wSuccess = docWriter.write(self.docView.qDocument) wSuccess = docWriter.write(self.docView.qDocument)
elif theFormat == self.FMT_HTM: elif theFmt == self.FMT_HTM:
makeHtml = ToHtml(self.theProject, self.theParent) makeHtml = ToHtml(self.theProject, self.theParent)
self._doBuild(makeHtml) self._doBuild(makeHtml)
if replaceTabs: if replaceTabs:
@@ -841,22 +849,26 @@ class GuiBuildNovel(QDialog):
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e)
elif theFormat == self.FMT_NWD: elif theFmt in (self.FMT_NWD, self.FMT_MD, self.FMT_GH):
makeNwd = ToHtml(self.theProject, self.theParent) makeMd = ToMarkdown(self.theProject, self.theParent)
makeNwd.setKeepMarkdown(True) if theFmt == self.FMT_NWD:
self._doBuild(makeNwd, doConvert=False) makeMd.setNovelWriterMarkdown()
elif theFmt == self.FMT_GH:
makeMd.setGitHubMarkdown
else:
makeMd.setStandardMarkdown()
self._doBuild(makeMd)
if replaceTabs: if replaceTabs:
makeNwd.replaceTabs(spaceChar=" ") makeMd.replaceTabs(spaceChar=" ")
try: try:
with open(savePath, mode="w", encoding="utf8") as outFile: makeMd.saveMarkdown(savePath)
for nwdPage in makeNwd.theMarkdown:
outFile.write(nwdPage)
wSuccess = True wSuccess = True
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e)
elif theFormat == self.FMT_FODT: elif theFmt == self.FMT_FODT:
makeOdt = ToOdt(self.theProject, self.theParent, isFlat=True) makeOdt = ToOdt(self.theProject, self.theParent, isFlat=True)
self._doBuild(makeOdt) self._doBuild(makeOdt)
try: try:
@@ -865,7 +877,7 @@ class GuiBuildNovel(QDialog):
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e)
elif theFormat == self.FMT_ODT: elif theFmt == self.FMT_ODT:
makeOdt = ToOdt(self.theProject, self.theParent, isFlat=False) makeOdt = ToOdt(self.theProject, self.theParent, isFlat=False)
self._doBuild(makeOdt) self._doBuild(makeOdt)
try: try:
@@ -874,7 +886,7 @@ class GuiBuildNovel(QDialog):
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e)
elif theFormat == self.FMT_JSON_H or theFormat == self.FMT_JSON_M: elif theFmt == self.FMT_JSON_H or theFmt == self.FMT_JSON_M:
jsonData = { jsonData = {
"meta" : { "meta" : {
"workingTitle" : self.theProject.projName, "workingTitle" : self.theProject.projName,
@@ -884,7 +896,7 @@ class GuiBuildNovel(QDialog):
} }
} }
if theFormat == self.FMT_JSON_H: if theFmt == self.FMT_JSON_H:
makeHtml = ToHtml(self.theProject, self.theParent) makeHtml = ToHtml(self.theProject, self.theParent)
self._doBuild(makeHtml) self._doBuild(makeHtml)
if replaceTabs: if replaceTabs:
@@ -898,15 +910,15 @@ class GuiBuildNovel(QDialog):
"html" : theBody, "html" : theBody,
} }
elif theFormat == self.FMT_JSON_M: elif theFmt == self.FMT_JSON_M:
makeNwd = ToHtml(self.theProject, self.theParent) makeMd = ToHtml(self.theProject, self.theParent)
makeNwd.setKeepMarkdown(True) makeMd.setKeepMarkdown(True)
self._doBuild(makeNwd, doConvert=False) self._doBuild(makeMd, doConvert=False)
if replaceTabs: if replaceTabs:
makeNwd.replaceTabs(spaceChar=" ") makeMd.replaceTabs(spaceChar=" ")
theBody = [] theBody = []
for nwdPage in makeNwd.theMarkdown: for nwdPage in makeMd.theMarkdown:
theBody.append(nwdPage.split("\n")) theBody.append(nwdPage.split("\n"))
jsonData["text"] = { jsonData["text"] = {
"nwd" : theBody, "nwd" : theBody,
@@ -919,7 +931,7 @@ class GuiBuildNovel(QDialog):
except Exception as e: except Exception as e:
errMsg = str(e) errMsg = str(e)
elif theFormat == self.FMT_PDF: elif theFmt == self.FMT_PDF:
try: try:
thePrinter = QPrinter() thePrinter = QPrinter()
thePrinter.setOutputFormat(QPrinter.PdfFormat) thePrinter.setOutputFormat(QPrinter.PdfFormat)