From 73951757bb0a5bb13958d9b23f869d72aa4ed465 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:05:47 +0100 Subject: [PATCH 1/3] Added call to escape unicode in LaTeX export. Fails with a flag if unsuccessful --- nw/convert/text/tolatex.py | 34 +++++++++++++++------------------- requirements.txt | 1 + 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/nw/convert/text/tolatex.py b/nw/convert/text/tolatex.py index 2002f8d9..345a988e 100644 --- a/nw/convert/text/tolatex.py +++ b/nw/convert/text/tolatex.py @@ -12,6 +12,7 @@ import textwrap import logging +import codecs import re import nw @@ -23,20 +24,7 @@ class ToLaTeX(Tokenizer): def __init__(self, theProject, theParent): Tokenizer.__init__(self, theProject, theParent) - return - - def doAutoReplace(self): - Tokenizer.doAutoReplace(self) - - repDict = { - "\u2013" : "--", - "\u2014" : "---", - "\u2500" : "---", - "\u2026" : "...", - } - xRep = re.compile("|".join([re.escape(k) for k in repDict.keys()]), flags=re.DOTALL) - self.theText = xRep.sub(lambda x: repDict[x.group(0)], self.theText) - + self.texCodecFail = False return def doConvert(self): @@ -122,17 +110,17 @@ class ToLaTeX(Tokenizer): elif tType == self.T_HEAD1: self.theResult += begText - self.theResult += "{\\Huge %s}\n" % tText + self.theResult += "{\\Huge %s}\n" % self._escapeUnicode(tText) self.theResult += endText elif tType == self.T_HEAD2: - self.theResult += "\\chapter*{%s}\n\n" % tText + self.theResult += "\\chapter*{%s}\n\n" % self._escapeUnicode(tText) elif tType == self.T_HEAD3: - self.theResult += "\\section*{%s}\n\n" % tText + self.theResult += "\\section*{%s}\n\n" % self._escapeUnicode(tText) elif tType == self.T_HEAD4: - self.theResult += "\\subsection*{%s}\n\n" % tText + self.theResult += "\\subsection*{%s}\n\n" % self._escapeUnicode(tText) elif tType == self.T_SEP: self.theResult += begText @@ -140,7 +128,7 @@ class ToLaTeX(Tokenizer): self.theResult += endText elif tType == self.T_TEXT: - thisPar.append(tText) + thisPar.append(self._escapeUnicode(tText)) elif tType == self.T_PBREAK: self.theResult += "\\newpage\n\n" @@ -153,4 +141,12 @@ class ToLaTeX(Tokenizer): return + def _escapeUnicode(self, theText): + try: + import latexcodec + return codecs.encode(theText, "ulatex") + except: + self.texCodecFail = True + return theText + # END Class ToLaTeX diff --git a/requirements.txt b/requirements.txt index a83517dd..14cd2a0c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ appdirs lxml pyenchant pycountry +latexcodec From 7eb7fafcbd61226f1293053be01455a7e00e8454 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:18:26 +0100 Subject: [PATCH 2/3] LaTeX export now supports escaping, with a warning if it fails --- nw/convert/file/latex.py | 3 +++ nw/convert/text/tolatex.py | 39 +------------------------------------- nw/gui/export.py | 12 ++++++++++-- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/nw/convert/file/latex.py b/nw/convert/file/latex.py index 334e9583..ebb052c8 100644 --- a/nw/convert/file/latex.py +++ b/nw/convert/file/latex.py @@ -24,6 +24,7 @@ class LaTeXFile(TextFile): def __init__(self, theProject, theParent): TextFile.__init__(self, theProject, theParent) self.theConv = ToLaTeX(self.theProject, self.theParent) + self.texCodecFail = False return ## @@ -50,6 +51,8 @@ class LaTeXFile(TextFile): self.outFile.write("\\end{document}\n") self.outFile.close() + self.texCodecFail = self.theConv.texCodecFail + return True # END Class LaTeXFile diff --git a/nw/convert/text/tolatex.py b/nw/convert/text/tolatex.py index 345a988e..a006cc01 100644 --- a/nw/convert/text/tolatex.py +++ b/nw/convert/text/tolatex.py @@ -10,7 +10,6 @@ """ -import textwrap import logging import codecs import re @@ -38,34 +37,6 @@ class ToLaTeX(Tokenizer): self.FMT_U_E : r"}", } - if self.wordWrap > 0: - tWrap = textwrap.TextWrapper( - width = self.wordWrap, - initial_indent = "", - subsequent_indent = "", - expand_tabs = True, - replace_whitespace = True, - fix_sentence_endings = False, - break_long_words = True, - drop_whitespace = True, - break_on_hyphens = True, - tabsize = 8, - max_lines = None - ) - tComm = textwrap.TextWrapper( - width = self.wordWrap-2, - initial_indent = "", - subsequent_indent = "", - expand_tabs = True, - replace_whitespace = True, - fix_sentence_endings = False, - break_long_words = True, - drop_whitespace = True, - break_on_hyphens = True, - tabsize = 8, - max_lines = None - ) - self.theResult = "" thisPar = [] for tType, tText, tFormat, tAlign in self.theTokens: @@ -89,14 +60,6 @@ class ToLaTeX(Tokenizer): tLen = len(tText) - # The text can now be word wrapped, if we have requested this and it's needed. - if self.wordWrap > 0 and tLen > self.wordWrap: - if tType == self.T_COMMENT: - aText = tComm.wrap(tText) - tText = "\n% ".join(aText) - else: - tText = tWrap.fill(tText) - # Then the text can receive final formatting before we append it to the results. # We also store text lines in a buffer and merge them only when we find an empty line # indicating a new paragraph. @@ -124,7 +87,7 @@ class ToLaTeX(Tokenizer): elif tType == self.T_SEP: self.theResult += begText - self.theResult += "%s\n" % tText + self.theResult += "%s\n" % self._escapeUnicode(tText) self.theResult += endText elif tType == self.T_TEXT: diff --git a/nw/gui/export.py b/nw/gui/export.py index 85b42f37..3b26861c 100644 --- a/nw/gui/export.py +++ b/nw/gui/export.py @@ -32,7 +32,7 @@ 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 +from nw.enum import nwItemType, nwAlert logger = logging.getLogger(__name__) @@ -167,11 +167,19 @@ class GuiExport(QDialog): nDone += 1 outFile.closeFile() - self.exportProgress.setValue(nDone) self.exportStatus.setText("Export to %s complete" % outFile.fileName) logger.verbose("Export to %s complete" % outFile.fileName) + if eFormat == GuiExportMain.FMT_TEX: + # Check that encoding was successful + if outFile.texCodecFail: + self.theParent.makeAlert(( + "Failed to escape unicode characters while writing LaTeX file. " + "The genrated .tex file may not build properly. " + "Make sure the python package 'latexcodec' is installed and working." + ), nwAlert.WARN) + return def _doClose(self): From d030243b5c2b09a3f39cc355ef0fc7e51bcf79c0 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:21:27 +0100 Subject: [PATCH 3/3] Updated tooltip on fixed width exports --- nw/gui/export.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nw/gui/export.py b/nw/gui/export.py index 3b26861c..1477f174 100644 --- a/nw/gui/export.py +++ b/nw/gui/export.py @@ -392,7 +392,7 @@ class GuiExportMain(QWidget): self.fixedWidth.setMaximum(999) self.fixedWidth.setSingleStep(1) self.fixedWidth.setValue(self.optState.getSetting("fixWidth")) - self.fixedWidth.setToolTip("Applies to .txt, .md and .tex files. 0 disables the feature.") + self.fixedWidth.setToolTip("Applies to .txt and .md files. A value of '0' disables the feature.") self.addSettingsForm.addWidget(QLabel("Fixed width"), 0, 0) self.addSettingsForm.addWidget(self.fixedWidth, 0, 1)