Added novelWriter flavour markdown as an export format, and cleaned up windows/unix line endings code

This commit is contained in:
Veronica K. B. Olsen
2019-10-26 22:58:16 +02:00
parent 753b248b9f
commit f38e770402
5 changed files with 126 additions and 41 deletions
+81
View File
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""novelWriter Concatenated File
novelWriter Concatenated File
=================================
Concatenate the standard novelWriter files to a single file
File History:
Created: 2019-10-26 [0.3.1]
"""
import logging
import nw
from os import path
from PyQt5.QtWidgets import QMessageBox
from nw.convert.file.text import TextFile
from nw.convert.tokenizer import Tokenizer
from nw.enum import nwAlert, nwItemLayout
logger = logging.getLogger(__name__)
class ConcatFile(TextFile):
def __init__(self, theProject, theParent):
TextFile.__init__(self, theProject, theParent)
self.theConv = Tokenizer(self.theProject, self.theParent)
return
def addText(self, tHandle):
logger.verbose("Parsing content of item '%s'" % tHandle)
theItem = self.theProject.getItem(tHandle)
isNone = theItem.itemLayout == nwItemLayout.NO_LAYOUT
isNote = theItem.itemLayout == nwItemLayout.NOTE
isNovel = not isNone and not isNote
if isNone:
return False
if isNote and not self.expNotes:
return False
if isNovel and not self.expNovel:
return False
self.theConv.setText(tHandle)
theResult = self.theConv.theText
if self.winEnding:
self.theConv.windowsEndings()
if theResult is not None and self.outFile is not None:
self.outFile.write(theResult.rstrip())
self.outFile.write(self.endLine)
self.outFile.write(self.endLine)
return True
##
# Internal Functions
##
def _doOpenFile(self, filePath):
try:
self.outFile = open(filePath,mode="w+")
except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False
return True
def _doCloseFile(self):
if self.outFile is not None:
self.outFile.close()
return True
# END Class ConcatFile
+10 -10
View File
@@ -35,14 +35,14 @@ class HtmlFile(TextFile):
def _doOpenFile(self, filePath):
try:
self.outFile = open(filePath,mode="w+")
self.outFile.write("<!DOCTYPE html>\n")
self.outFile.write("<html>\n")
self.outFile.write("<head>\n")
self.outFile.write("<style>\n")
self.outFile.write(" pre {background-color: #ffff99;}\n")
self.outFile.write("</style>\n")
self.outFile.write("</head>\n")
self.outFile.write("<body>\n")
self.outFile.write("<!DOCTYPE html>"+self.endLine)
self.outFile.write("<html>"+self.endLine)
self.outFile.write("<head>"+self.endLine)
self.outFile.write("<style>"+self.endLine)
self.outFile.write(" pre {background-color: #ffff99;}"+self.endLine)
self.outFile.write("</style>"+self.endLine)
self.outFile.write("</head>"+self.endLine)
self.outFile.write("<body>"+self.endLine)
except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False
@@ -50,8 +50,8 @@ class HtmlFile(TextFile):
def _doCloseFile(self):
if self.outFile is not None:
self.outFile.write("</body>\n")
self.outFile.write("</html>\n")
self.outFile.write("</body>"+self.endLine)
self.outFile.write("</html>"+self.endLine)
self.outFile.close()
return True
+4 -14
View File
@@ -34,16 +34,11 @@ class LaTeXFile(TextFile):
def _doOpenFile(self, filePath):
if self.winEnding:
tN = "\r\n"
else:
tN = "\n"
try:
self.outFile = open(filePath,mode="w+")
self.outFile.write(r"\documentclass[12pt]{report}"+tN+tN)
self.outFile.write(r"\usepackage[utf8]{inputenc}"+tN+tN)
self.outFile.write(r"\begin{document}"+tN+tN)
self.outFile.write(r"\documentclass[12pt]{report}"+self.endLine)
self.outFile.write(r"\usepackage[utf8]{inputenc}"+self.endLine)
self.outFile.write(r"\begin{document}"+self.endLine)
except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False
@@ -52,13 +47,8 @@ class LaTeXFile(TextFile):
def _doCloseFile(self):
if self.winEnding:
tN = "\r\n"
else:
tN = "\n"
if self.outFile is not None:
self.outFile.write(r"\end{document}"+tN)
self.outFile.write(r"\end{document}"+self.endLine)
self.outFile.close()
return True
+8 -5
View File
@@ -34,7 +34,7 @@ class TextFile():
self.theText = ""
self.expNovel = True
self.expNotes = False
self.winEnding = False
self.winEnding = self.mainConf.osWindows
self.theConv = ToText(self.theProject, self.theParent)
self.makeAlert = self.theParent.makeAlert
@@ -43,6 +43,11 @@ class TextFile():
self.setMeta(False)
self.setWordWrap(80)
if self.winEnding:
self.endLine = "\r\n"
else:
self.endLine = "\n"
return
##
@@ -158,10 +163,8 @@ class TextFile():
"""
try:
self.outFile = open(filePath,mode="w+")
if self.winEnding:
self.outFile.write("\r\n\r\n")
else:
self.outFile.write("\n\n")
self.outFile.write(self.endLine)
self.outFile.write(self.endLine)
except Exception as e:
self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR)
return False
+23 -12
View File
@@ -30,6 +30,7 @@ from nw.convert.file.text import TextFile
from nw.convert.file.html import HtmlFile
from nw.convert.file.markdown import MarkdownFile
from nw.convert.file.latex import LaTeXFile
from nw.convert.file.concat import ConcatFile
from nw.constants import nwFiles
from nw.enum import nwItemType
@@ -130,6 +131,8 @@ class GuiExport(QDialog):
outFile = HtmlFile(self.theProject, self.theParent)
elif eFormat == GuiExportMain.FMT_TEX:
outFile = LaTeXFile(self.theProject, self.theParent)
elif eFormat == GuiExportMain.FMT_NWD:
outFile = ConcatFile(self.theProject, self.theParent)
if outFile is None:
return False
@@ -206,12 +209,13 @@ class GuiExport(QDialog):
class GuiExportMain(QWidget):
FMT_TXT = 1
FMT_MD = 2
FMT_HTML = 3
FMT_EBOOK = 4
FMT_ODT = 5
FMT_TEX = 6
FMT_TXT = 1 # Plain text file
FMT_MD = 2 # Markdown file
FMT_HTML = 3 # HTML file
FMT_EBOOK = 4 # E-book friendly HTML
FMT_ODT = 5 # Open document
FMT_TEX = 6 # LaTeX file
FMT_NWD = 7 # novelWriter markdown
FMT_EXT = {
FMT_TXT : ".txt",
FMT_MD : ".md",
@@ -219,6 +223,7 @@ class GuiExportMain(QWidget):
FMT_EBOOK : ".htm",
FMT_ODT : ".odt",
FMT_TEX : ".tex",
FMT_NWD : ".nwd",
}
FMT_HELP = {
FMT_TXT : (
@@ -245,6 +250,10 @@ class GuiExportMain(QWidget):
"Exports a LaTeX file that can be compiled to PDF using for instance PDFLaTeX. "
"Comments are exported as LaTeX comments."
),
FMT_NWD : (
"Exports a document using the novelWriter markdown format. "
"The files selected by the filters are appended as-is."
),
}
def __init__(self, theParent, theProject, optState):
@@ -346,12 +355,13 @@ class GuiExportMain(QWidget):
self.outputHelp.setAlignment(Qt.AlignTop)
self.outputFormat = QComboBox(self)
self.outputFormat.addItem("Plain Text (.txt)", self.FMT_TXT)
self.outputFormat.addItem("Markdown (.md)", self.FMT_MD)
self.outputFormat.addItem("HTML5 (.htm)", self.FMT_HTML)
# self.outputFormat.addItem("HTML5 for eBook (.htm)", self.FMT_EBOOK)
# self.outputFormat.addItem("Open Document (.odt)", self.FMT_ODT)
self.outputFormat.addItem("LaTeX for PDF (.tex)", self.FMT_TEX)
self.outputFormat.addItem("Plain Text (.txt)", self.FMT_TXT)
self.outputFormat.addItem("Markdown (.md)", self.FMT_MD)
self.outputFormat.addItem("HTML5 (.htm)", self.FMT_HTML)
# self.outputFormat.addItem("HTML5 for eBook (.htm)", self.FMT_EBOOK)
# self.outputFormat.addItem("Open Document (.odt)", self.FMT_ODT)
self.outputFormat.addItem("LaTeX for PDF (.tex)", self.FMT_TEX)
self.outputFormat.addItem("novelWriter Markdown (.nwd)", self.FMT_NWD)
self.outputFormat.currentIndexChanged.connect(self._updateFormat)
optIdx = self.outputFormat.findData(self.optState.getSetting("eFormat"))
@@ -421,6 +431,7 @@ class GuiExportMain(QWidget):
"HTML files (*.htm *.html)",
# "Open document files (*.odt)",
"LaTeX files (*.tex)",
"novelWriter document files (*.nwd)",
"All files (*.*)",
]