Added large size handling to build tool

This commit is contained in:
Veronica K. B. Olsen
2020-10-06 14:29:33 +02:00
parent 3893892927
commit 23d5e33864
2 changed files with 57 additions and 5 deletions
+17 -1
View File
@@ -33,7 +33,7 @@ from PyQt5.QtCore import QRegularExpression
from nw.core.document import NWDoc from nw.core.document import NWDoc
from nw.core.tools import numberToWord, numberToRoman from nw.core.tools import numberToWord, numberToRoman
from nw.constants import nwItemLayout, nwItemType, nwRegEx from nw.constants import nwConst, nwItemLayout, nwItemType, nwRegEx
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -120,6 +120,9 @@ class Tokenizer():
self.isNote = False self.isNote = False
self.isNovel = False self.isNovel = False
# Error Handling
self.errData = []
return return
## ##
@@ -212,6 +215,14 @@ class Tokenizer():
theDocument = NWDoc(self.theProject, self.theParent) theDocument = NWDoc(self.theProject, self.theParent)
self.theText = theDocument.openDocument(theHandle) self.theText = theDocument.openDocument(theHandle)
docSize = len(self.theText)
if docSize > nwConst.maxDocSize:
errVal = "Document '%s' is too big (%.2f MB). Skipping." % (
self.theItem.itemName, docSize/1.0e6
)
self.theText = "# ERROR\n\n%s\n\n" % errVal
self.errData.append(errVal)
self.isNone = self.theItem.itemLayout == nwItemLayout.NO_LAYOUT self.isNone = self.theItem.itemLayout == nwItemLayout.NO_LAYOUT
self.isTitle = self.theItem.itemLayout == nwItemLayout.TITLE self.isTitle = self.theItem.itemLayout == nwItemLayout.TITLE
self.isBook = self.theItem.itemLayout == nwItemLayout.BOOK self.isBook = self.theItem.itemLayout == nwItemLayout.BOOK
@@ -230,6 +241,11 @@ class Tokenizer():
""" """
return self.theResult return self.theResult
def getResultSize(self):
"""Return the size of the result from the conversion.
"""
return len(self.theResult)
def getFilteredMarkdown(self): def getFilteredMarkdown(self):
"""Return the novelWriter markdown after the filters have been applied. """Return the novelWriter markdown after the filters have been applied.
""" """
+40 -4
View File
@@ -49,7 +49,7 @@ from nw.common import fuzzyTime, makeFileNameSafe
from nw.gui.custom import QSwitch from nw.gui.custom import QSwitch
from nw.core import ToHtml from nw.core import ToHtml
from nw.constants import ( from nw.constants import (
nwAlert, nwFiles, nwItemType, nwItemLayout, nwItemClass nwConst, nwAlert, nwFiles, nwItemType, nwItemLayout, nwItemClass
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -77,7 +77,7 @@ class GuiBuildNovel(QDialog):
self.theTheme = theParent.theTheme self.theTheme = theParent.theTheme
self.optState = self.theProject.optState self.optState = self.theProject.optState
self.htmlText = [] # List of html document self.htmlText = [] # List of html documents
self.htmlStyle = [] # List of html styles self.htmlStyle = [] # List of html styles
self.nwdText = [] # List of markdown documents self.nwdText = [] # List of markdown documents
self.buildTime = 0 # The timestamp of the last build self.buildTime = 0 # The timestamp of the last build
@@ -494,7 +494,15 @@ class GuiBuildNovel(QDialog):
self.docView.clearStyleSheet() self.docView.clearStyleSheet()
else: else:
self.docView.setStyleSheet(self.htmlStyle) self.docView.setStyleSheet(self.htmlStyle)
self.docView.setContent(self.htmlText, self.buildTime)
htmlSize = sum([len(x) for x in self.htmlText])
if htmlSize < nwConst.maxBuildSize:
self.docView.setContent(self.htmlText, self.buildTime)
else:
self.docView.setText(
"Failed to generate preview. The result is too big."
)
self._enableQtSave(False)
else: else:
self.htmlText = [] self.htmlText = []
self.htmlStyle = [] self.htmlStyle = []
@@ -554,6 +562,8 @@ class GuiBuildNovel(QDialog):
self.htmlStyle = [] self.htmlStyle = []
self.nwdText = [] self.nwdText = []
htmlSize = 0
for nItt, tItem in enumerate(self.theProject.projTree): for nItt, tItem in enumerate(self.theProject.projTree):
noteRoot = noteFiles noteRoot = noteFiles
@@ -578,6 +588,7 @@ class GuiBuildNovel(QDialog):
makeHtml.doPostProcessing() makeHtml.doPostProcessing()
self.htmlText.append(makeHtml.getResult()) self.htmlText.append(makeHtml.getResult())
self.nwdText.append(makeHtml.getFilteredMarkdown()) self.nwdText.append(makeHtml.getFilteredMarkdown())
htmlSize += makeHtml.getResultSize()
except Exception as e: except Exception as e:
logger.error("Failed to generate html of document '%s'" % tItem.itemHandle) logger.error("Failed to generate html of document '%s'" % tItem.itemHandle)
@@ -591,6 +602,12 @@ class GuiBuildNovel(QDialog):
# Update progress bar, also for skipped items # Update progress bar, also for skipped items
self.buildProgress.setValue(nItt+1) self.buildProgress.setValue(nItt+1)
if makeHtml.errData:
self.theParent.makeAlert((
"There were problems when building the project:"
"<br>-&nbsp;%s"
) % "<br>-&nbsp;".join(makeHtml.errData), nwAlert.ERROR)
if replaceTabs: if replaceTabs:
htmlText = [] htmlText = []
eightSpace = "&nbsp;"*8 eightSpace = "&nbsp;"*8
@@ -615,7 +632,16 @@ class GuiBuildNovel(QDialog):
self.docView.clearStyleSheet() self.docView.clearStyleSheet()
else: else:
self.docView.setStyleSheet(self.htmlStyle) self.docView.setStyleSheet(self.htmlStyle)
self.docView.setContent(self.htmlText, self.buildTime)
if htmlSize < nwConst.maxBuildSize:
self.docView.setContent(self.htmlText, self.buildTime)
self._enableQtSave(True)
else:
self.docView.setText(
"Failed to generate preview. The result is too big."
)
allowQtSave = False
self._enableQtSave(False)
self._saveCache() self._saveCache()
@@ -962,6 +988,16 @@ class GuiBuildNovel(QDialog):
# Internal Functions # Internal Functions
## ##
def _enableQtSave(self, theState):
"""Set the enabled status of Save menu entries that depend on
the QTextDocument.
"""
self.saveODT.setEnabled(theState)
self.savePDF.setEnabled(theState)
self.saveMD.setEnabled(theState)
self.saveTXT.setEnabled(theState)
return
def _saveSettings(self): def _saveSettings(self):
"""Save the various user settings. """Save the various user settings.
""" """