Preserve markdown during build, and use lists instead of strings

This commit is contained in:
Veronica K. B. Olsen
2020-05-12 18:41:08 +02:00
parent 2a67b69d32
commit aa4778d4cd
3 changed files with 69 additions and 19 deletions
+15 -10
View File
@@ -105,8 +105,10 @@ class ToHtml(Tokenizer):
} }
self.theResult = "" self.theResult = ""
thisPar = [] thisPar = []
parStyle = "" parStyle = ""
tmpResult = []
for tType, tText, tFormat, tStyle in self.theTokens: for tType, tText, tFormat, tStyle in self.theTokens:
# Styles # Styles
@@ -146,31 +148,31 @@ class ToHtml(Tokenizer):
if tType == self.T_EMPTY: if tType == self.T_EMPTY:
if len(thisPar) > 0: if len(thisPar) > 0:
tTemp = "".join(thisPar) tTemp = "".join(thisPar)
self.theResult += "<p%s>%s</p>\n" % (parStyle,tTemp.rstrip()) tmpResult.append("<p%s>%s</p>\n" % (parStyle, tTemp.rstrip()))
thisPar = [] thisPar = []
parStyle = "" parStyle = ""
elif tType == self.T_HEAD1: elif tType == self.T_HEAD1:
tHead = tText.replace(r"\\", "<br/>") tHead = tText.replace(r"\\", "<br/>")
self.theResult += "<h1%s>%s</h1>\n" % (hStyle, tHead) tmpResult.append("<h1%s>%s</h1>\n" % (hStyle, tHead))
elif tType == self.T_HEAD2: elif tType == self.T_HEAD2:
tHead = tText.replace(r"\\", "<br/>") tHead = tText.replace(r"\\", "<br/>")
self.theResult += "<h2%s>%s</h2>\n" % (hStyle, tHead) tmpResult.append("<h2%s>%s</h2>\n" % (hStyle, tHead))
elif tType == self.T_HEAD3: elif tType == self.T_HEAD3:
tHead = tText.replace(r"\\", "<br/>") tHead = tText.replace(r"\\", "<br/>")
self.theResult += "<h3%s>%s</h3>\n" % (hStyle, tHead) tmpResult.append("<h3%s>%s</h3>\n" % (hStyle, tHead))
elif tType == self.T_HEAD4: elif tType == self.T_HEAD4:
tHead = tText.replace(r"\\", "<br/>") tHead = tText.replace(r"\\", "<br/>")
self.theResult += "<h4%s>%s</h4>\n" % (hStyle, tHead) tmpResult.append("<h4%s>%s</h4>\n" % (hStyle, tHead))
elif tType == self.T_SEP: elif tType == self.T_SEP:
self.theResult += "<p%s>%s</p>\n" % (hStyle, tText) tmpResult.append("<p%s>%s</p>\n" % (hStyle, tText))
elif tType == self.T_SKIP: elif tType == self.T_SKIP:
self.theResult += "<p%s>&nbsp;</p>\n" % hStyle tmpResult.append("<p%s>&nbsp;</p>\n" % hStyle)
elif tType == self.T_TEXT: elif tType == self.T_TEXT:
tTemp = tText tTemp = tText
@@ -183,13 +185,16 @@ class ToHtml(Tokenizer):
thisPar.append(tTemp.rstrip()+" ") thisPar.append(tTemp.rstrip()+" ")
elif tType == self.T_SYNOPSIS and self.doSynopsis: elif tType == self.T_SYNOPSIS and self.doSynopsis:
self.theResult += self._formatSynopsis(tText) tmpResult.append(self._formatSynopsis(tText))
elif tType == self.T_COMMENT and self.doComments: elif tType == self.T_COMMENT and self.doComments:
self.theResult += self._formatComments(tText) tmpResult.append(self._formatComments(tText))
elif tType == self.T_KEYWORD and self.doKeywords: elif tType == self.T_KEYWORD and self.doKeywords:
self.theResult += self._formatKeywords(tText) tmpResult.append(self._formatKeywords(tText))
self.theResult = "".join(tmpResult)
tmpResult = []
return return
+39 -7
View File
@@ -84,6 +84,7 @@ class Tokenizer():
self.theItem = None # The NWItem associated with the handle self.theItem = None # The NWItem associated with the handle
self.theTokens = None # The list of the processed tokens self.theTokens = None # The list of the processed tokens
self.theResult = None # The result text after conversion self.theResult = None # The result text after conversion
self.theMarkdown = None # The result text in novelWriter markdown
# User Settings # User Settings
self.doBodyText = True # Include body text self.doBodyText = True # Include body text
@@ -125,13 +126,14 @@ class Tokenizer():
"""Clear the data arrays and variables, but not settings, so the class """Clear the data arrays and variables, but not settings, so the class
can be reused for multiple documents. can be reused for multiple documents.
""" """
self.theText = None self.theText = None
self.theHandle = None self.theHandle = None
self.theItem = None self.theItem = None
self.theTokens = None self.theTokens = None
self.theResult = None self.theResult = None
self.numChapter = 0 self.theMarkdown = None
self.firstScene = False self.numChapter = 0
self.firstScene = False
self.isNone = False self.isNone = False
self.isTitle = False self.isTitle = False
@@ -230,6 +232,11 @@ class Tokenizer():
""" """
return self.theResult return self.theResult
def getFilteredMarkdown(self):
"""Return the novelWriter markdown after the filters have been applied.
"""
return self.theMarkdown
def doAutoReplace(self): def doAutoReplace(self):
"""Run through the user's auto-replace dictionary. """Run through the user's auto-replace dictionary.
""" """
@@ -280,6 +287,8 @@ class Tokenizer():
defAlign = self.A_LEFT defAlign = self.A_LEFT
self.theTokens = [] self.theTokens = []
self.theMarkdown = ""
tmpMarkdown = []
for aLine in self.theText.splitlines(): for aLine in self.theText.splitlines():
# Tag lines starting with specific characters # Tag lines starting with specific characters
@@ -287,36 +296,54 @@ class Tokenizer():
self.theTokens.append(( self.theTokens.append((
self.T_EMPTY, "", None, None self.T_EMPTY, "", None, None
)) ))
tmpMarkdown.append("\n")
elif aLine[0] == "%": elif aLine[0] == "%":
cLine = aLine[1:].strip() cLine = aLine[1:].strip()
if cLine.lower().startswith("synopsis:"): if cLine.lower().startswith("synopsis:"):
self.theTokens.append(( self.theTokens.append((
self.T_SYNOPSIS, cLine[9:].strip(), None, defAlign self.T_SYNOPSIS, cLine[9:].strip(), None, defAlign
)) ))
if self.doSynopsis:
tmpMarkdown.append("%s\n" % aLine)
else: else:
self.theTokens.append(( self.theTokens.append((
self.T_COMMENT, aLine[1:].strip(), None, defAlign self.T_COMMENT, aLine[1:].strip(), None, defAlign
)) ))
if self.doComments:
tmpMarkdown.append("%s\n" % aLine)
elif aLine[0] == "@": elif aLine[0] == "@":
self.theTokens.append(( self.theTokens.append((
self.T_KEYWORD, aLine[1:].strip(), None, self.A_LEFT self.T_KEYWORD, aLine[1:].strip(), None, self.A_LEFT
)) ))
if self.doKeywords:
tmpMarkdown.append("%s\n" % aLine)
elif aLine[:2] == "# ": elif aLine[:2] == "# ":
self.theTokens.append(( self.theTokens.append((
self.T_HEAD1, aLine[2:].strip(), None, self.A_LEFT | self.A_PBB self.T_HEAD1, aLine[2:].strip(), None, self.A_LEFT | self.A_PBB
)) ))
tmpMarkdown.append("%s\n" % aLine)
elif aLine[:3] == "## ": elif aLine[:3] == "## ":
self.theTokens.append(( self.theTokens.append((
self.T_HEAD2, aLine[3:].strip(), None, self.A_LEFT | self.A_PBA_AV self.T_HEAD2, aLine[3:].strip(), None, self.A_LEFT | self.A_PBA_AV
)) ))
tmpMarkdown.append("%s\n" % aLine)
elif aLine[:4] == "### ": elif aLine[:4] == "### ":
self.theTokens.append(( self.theTokens.append((
self.T_HEAD3, aLine[4:].strip(), None, self.A_LEFT | self.A_PBA_AV self.T_HEAD3, aLine[4:].strip(), None, self.A_LEFT | self.A_PBA_AV
)) ))
tmpMarkdown.append("%s\n" % aLine)
elif aLine[:5] == "#### ": elif aLine[:5] == "#### ":
self.theTokens.append(( self.theTokens.append((
self.T_HEAD4, aLine[5:].strip(), None, self.A_LEFT | self.A_PBA_AV self.T_HEAD4, aLine[5:].strip(), None, self.A_LEFT | self.A_PBA_AV
)) ))
tmpMarkdown.append("%s\n" % aLine)
else: else:
if not self.doBodyText: if not self.doBodyText:
# Skip all body text # Skip all body text
@@ -340,11 +367,16 @@ class Tokenizer():
self.theTokens.append(( self.theTokens.append((
self.T_TEXT, aLine, fmtPos, defAlign self.T_TEXT, aLine, fmtPos, defAlign
)) ))
tmpMarkdown.append("%s\n" % aLine)
# Always add an empty line at the end # Always add an empty line at the end
self.theTokens.append(( self.theTokens.append((
self.T_EMPTY, "", None, None self.T_EMPTY, "", None, None
)) ))
tmpMarkdown.append("\n")
self.theMarkdown = "".join(tmpMarkdown)
tmpMarkdown = []
return return
+15 -2
View File
@@ -29,6 +29,7 @@ import logging
import nw import nw
from os import path from os import path
from time import time
from PyQt5.QtCore import Qt, QByteArray from PyQt5.QtCore import Qt, QByteArray
from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog
@@ -69,6 +70,7 @@ class GuiBuildNovel(QDialog):
self.optState = self.theProject.optState self.optState = self.theProject.optState
self.htmlText = "" self.htmlText = ""
self.nwdText = ""
self.setWindowTitle("Build Novel Project") self.setWindowTitle("Build Novel Project")
self.setMinimumWidth(800) self.setMinimumWidth(800)
@@ -311,9 +313,13 @@ class GuiBuildNovel(QDialog):
makeHtml.setKeywords(incKeywords) makeHtml.setKeywords(incKeywords)
makeHtml.setJustify(justifyText) makeHtml.setJustify(justifyText)
self.htmlText = ""
self.buildProgress.setMaximum(len(self.theProject.projTree)) self.buildProgress.setMaximum(len(self.theProject.projTree))
self.buildProgress.setValue(0) self.buildProgress.setValue(0)
tStart = time()
tmpHtml = []
tmpNwd = []
for nItt, tItem in enumerate(self.theProject.projTree): for nItt, tItem in enumerate(self.theProject.projTree):
if self._checkInclude(tItem, noteFiles, novelFiles, ignoreFlag): if self._checkInclude(tItem, noteFiles, novelFiles, ignoreFlag):
makeHtml.setText(tItem.itemHandle) makeHtml.setText(tItem.itemHandle)
@@ -322,9 +328,16 @@ class GuiBuildNovel(QDialog):
makeHtml.doHeaders() makeHtml.doHeaders()
makeHtml.doConvert() makeHtml.doConvert()
makeHtml.doPostProcessing() makeHtml.doPostProcessing()
self.htmlText += makeHtml.getResult() tmpHtml.append(makeHtml.getResult())
tmpNwd.append(makeHtml.getFilteredMarkdown())
self.buildProgress.setValue(nItt+1) self.buildProgress.setValue(nItt+1)
self.htmlText = "".join(tmpHtml)
self.nwdText = "".join(tmpNwd)
tEnd = time()
logger.debug("Built project in %.3f ms" % (1000*(tEnd-tStart)))
self.docView.setHtml(self.htmlText) self.docView.setHtml(self.htmlText)
return return