diff --git a/nw/convert/file/__init__.py b/nw/convert/file/__init__.py deleted file mode 100644 index da2afb33..00000000 --- a/nw/convert/file/__init__.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- - -from nw.convert.file.concat import ConcatFile -from nw.convert.file.html import HtmlFile -from nw.convert.file.latex import LaTeXFile -from nw.convert.file.markdown import MarkdownFile -from nw.convert.file.text import TextFile - -__all__ = [ - "ConcatFile", - "HtmlFile", - "LaTeXFile", - "MarkdownFile", - "TextFile", -] diff --git a/nw/convert/file/concat.py b/nw/convert/file/concat.py deleted file mode 100644 index 5bc76e89..00000000 --- a/nw/convert/file/concat.py +++ /dev/null @@ -1,80 +0,0 @@ -# -*- 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] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import nw - -from os import path - -from nw.convert.file.text import TextFile -from nw.convert.tokenizer import Tokenizer -from nw.constants import nwAlert - -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) - - if not self.checkInclude(tHandle): - return False - - self.theConv.setText(tHandle) - - theResult = self.theConv.theText - - if theResult is not None and self.outFile is not None: - self.outFile.write(theResult.rstrip()) - self.outFile.write("\n\n") - - return True - - ## - # Internal Functions - ## - - def _doOpenFile(self, filePath): - try: - self.outFile = open(filePath,mode="wt+",encoding="utf8") - 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 diff --git a/nw/convert/file/html.py b/nw/convert/file/html.py deleted file mode 100644 index f03e12b3..00000000 --- a/nw/convert/file/html.py +++ /dev/null @@ -1,83 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter HTML File - - novelWriter – HTML File -========================= - Writes the project to a html file - - File History: - Created: 2019-10-19 [0.3] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import nw - -from nw.convert.file.text import TextFile -from nw.convert.text.tohtml import ToHtml -from nw.constants import nwAlert - -logger = logging.getLogger(__name__) - -class HtmlFile(TextFile): - - def __init__(self, theProject, theParent): - TextFile.__init__(self, theProject, theParent) - self.theConv = ToHtml(self.theProject, self.theParent) - return - - ## - # Internal Functions - ## - - def _doOpenFile(self, filePath): - try: - self.outFile = open(filePath,mode="wt+",encoding="utf8") - self.outFile.write("\n") - self.outFile.write("\n") - self.outFile.write("\n") - self.outFile.write(" \n") - self.outFile.write(" \n") - self.outFile.write("\n") - self.outFile.write("\n") - self.outFile.write("
\n") - 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.write("
\n") - self.outFile.write("\n") - self.outFile.write("\n") - self.outFile.close() - return True - -# END Class HtmlFile diff --git a/nw/convert/file/latex.py b/nw/convert/file/latex.py deleted file mode 100644 index fd7c73c1..00000000 --- a/nw/convert/file/latex.py +++ /dev/null @@ -1,69 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter LaTeX File - - novelWriter – LaTeX File -========================== - Writes the project to a LaTeX file - - File History: - Created: 2019-10-24 [0.3.1] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import nw - -from nw.convert.file.text import TextFile -from nw.convert.text.tolatex import ToLaTeX -from nw.constants import nwAlert - -logger = logging.getLogger(__name__) - -class LaTeXFile(TextFile): - - def __init__(self, theProject, theParent): - TextFile.__init__(self, theProject, theParent) - self.theConv = ToLaTeX(self.theProject, self.theParent) - self.texCodecFail = False - return - - ## - # Internal Functions - ## - - def _doOpenFile(self, filePath): - try: - self.outFile = open(filePath,mode="wt+",encoding="utf8") - self.outFile.write("\\documentclass[12pt]{report}\n") - self.outFile.write("\\usepackage[utf8]{inputenc}\n") - self.outFile.write("\\usepackage[T1]{fontenc}\n") - self.outFile.write("\n") - self.outFile.write("\\begin{document}\n") - 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.write("\\end{document}\n") - self.outFile.close() - self.texCodecFail = self.theConv.texCodecFail - return True - -# END Class LaTeXFile diff --git a/nw/convert/file/markdown.py b/nw/convert/file/markdown.py deleted file mode 100644 index 24b516da..00000000 --- a/nw/convert/file/markdown.py +++ /dev/null @@ -1,61 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter Markdown File - - novelWriter – Markdown File -============================= - Writes the project to a markdown file - - File History: - Created: 2019-10-19 [0.3] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import nw - -from nw.convert.file.text import TextFile -from nw.convert.text.tomarkdown import ToMarkdown -from nw.constants import nwAlert - -logger = logging.getLogger(__name__) - -class MarkdownFile(TextFile): - - def __init__(self, theProject, theParent): - TextFile.__init__(self, theProject, theParent) - self.theConv = ToMarkdown(self.theProject, self.theParent) - return - - ## - # Internal Functions - ## - - def _doOpenFile(self, filePath): - try: - self.outFile = open(filePath,mode="wt+",encoding="utf8") - 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 MarkdownFile diff --git a/nw/convert/file/text.py b/nw/convert/file/text.py deleted file mode 100644 index afcadb10..00000000 --- a/nw/convert/file/text.py +++ /dev/null @@ -1,210 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter Text File - - novelWriter – Text File -========================= - Writes the project to a plain text file - - File History: - Created: 2019-10-18 [0.2.3] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import nw - -from os import path - -from PyQt5.QtWidgets import QMessageBox - -from nw.convert.text.totext import ToText -from nw.constants import nwAlert, nwItemType, nwItemLayout, nwItemClass - -logger = logging.getLogger(__name__) - -class TextFile(): - - def __init__(self, theProject, theParent): - - self.mainConf = nw.CONFIG - self.theProject = theProject - self.theParent = theParent - - self.outFile = None - self.fileName = "" - self.theText = "" - self.expNovel = True - self.expNotes = False - - self.theConv = ToText(self.theProject, self.theParent) - self.makeAlert = self.theParent.makeAlert - - self.setComments(False) - self.setKeywords(False) - self.setWordWrap(80) - - return - - ## - # Setters - ## - - def setExportNovel(self, doNovel): - self.expNovel = doNovel - return - - def setExportNotes(self, doNotes): - self.expNotes = doNotes - return - - def setComments(self, doComments): - self.theConv.setComments(doComments) - return - - def setKeywords(self, doKeywords): - self.theConv.setKeywords(doKeywords) - return - - def setWordWrap(self, wordWrap): - if wordWrap >= 0: - self.theConv.setWordWrap(wordWrap) - else: - self.theConv.setWordWrap(0) - return - - def setTitleFormat(self, fmtTitle): - self.theConv.setTitleFormat(fmtTitle) - return - - def setChapterFormat(self, fmtChapter): - self.theConv.setChapterFormat(fmtChapter) - return - - def setUnNumberedFormat(self, fmtUnNum): - self.theConv.setUnNumberedFormat(fmtUnNum) - return - - def setSceneFormat(self, fmtScene, hideScene): - self.theConv.setSceneFormat(fmtScene, hideScene) - return - - def setSectionFormat(self, fmtSection, hideSection): - self.theConv.setSectionFormat(fmtSection, hideSection) - return - - ## - # Core Methods - ## - - def openFile(self, filePath): - - self.fileName = path.basename(filePath) - if path.isfile(filePath) and self.mainConf.showGUI: - msgBox = QMessageBox() - msgRes = msgBox.question(self.theParent, "Overwrite", ( - "File '%s' already exists.
Do you want to overwrite it?" % self.fileName - )) - if msgRes != QMessageBox.Yes: - return False - - self._doOpenFile(filePath) - - if self.outFile is None: - return False - - return True - - def closeFile(self): - self._doCloseFile() - return True - - def addText(self, tHandle): - - logger.verbose("Parsing content of item '%s'" % tHandle) - - if not self.checkInclude(tHandle): - return False - - self.theConv.setText(tHandle) - self.theConv.doAutoReplace() - self.theConv.tokenizeText() - self.theConv.formatHeaders() - self.theConv.doConvert() - self.theConv.doPostProcessing() - - if self.theConv.theResult is not None and self.outFile is not None: - self.outFile.write(self.theConv.theResult) - - return True - - def checkInclude(self, tHandle): - """This function checks whether a file should be included in the - export or not. For standard note and novel files, this is - controlled by the options selected by the user. For other files - classified as non-exportable, a few checks must be made, and the - following are not: - * Items that are not actual files. - * Items that have been orphaned which are tagged as NO_LAYOUT - and NO_CLASS. - * Items that appear in the TRASH folder - """ - - theItem = self.theProject.projTree[tHandle] - isNone = theItem.itemType != nwItemType.FILE - isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT - isNone |= theItem.itemClass == nwItemClass.NO_CLASS - isNone |= theItem.itemClass == nwItemClass.TRASH - isNone |= theItem.parHandle == self.theProject.projTree.trashRoot() - 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 - - return True - - ## - # Internal Functions - ## - - def _doOpenFile(self, filePath): - """This function does the actual opening of the file, and can be - overloaded by a subclass that uses a different file format that - requires a different approach. - """ - try: - self.outFile = open(filePath,mode="wt+",encoding="utf8") - self.outFile.write("\n\n") - except Exception as e: - self.makeAlert(["Failed to open file.",str(e)], nwAlert.ERROR) - return False - return True - - def _doCloseFile(self): - """This function closes the file, and is meant to be overloaded - by the subclass for other file formats. - """ - if self.outFile is not None: - self.outFile.close() - return True - -# END Class OutFile diff --git a/nw/convert/text/__init__.py b/nw/convert/text/__init__.py deleted file mode 100644 index ef849320..00000000 --- a/nw/convert/text/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -# -*- coding: utf-8 -*- - -from nw.convert.text.tohtml import ToHtml -from nw.convert.text.tolatex import ToLaTeX -from nw.convert.text.tomarkdown import ToMarkdown -from nw.convert.text.totext import ToText - -__all__ = [ - "ToHtml", - "ToLaTeX", - "ToMarkdown", - "ToText", -] diff --git a/nw/convert/text/tolatex.py b/nw/convert/text/tolatex.py deleted file mode 100644 index f945b763..00000000 --- a/nw/convert/text/tolatex.py +++ /dev/null @@ -1,155 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter LaTeX Converter - - novelWriter – LaTeX Converter -=============================== - Extends the Tokenizer class to write LaTeX - - File History: - Created: 2019-10-24 [0.3.1] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import logging -import codecs -import re -import nw - -from nw.convert.tokenizer import Tokenizer -from nw.constants import nwUnicode - -logger = logging.getLogger(__name__) - -class ToLaTeX(Tokenizer): - - def __init__(self, theProject, theParent): - Tokenizer.__init__(self, theProject, theParent) - self.texCodecFail = False - return - - def doPostProcessing(self): - """The latexcodec misses dashes and non-breaking spaces, so we - do those here. - """ - - repDict = { - nwUnicode.U_ENDASH : "--", - nwUnicode.U_EMDASH : "---", - nwUnicode.U_NBSP : "~", - } - xRep = re.compile("|".join([re.escape(k) for k in repDict.keys()]), flags=re.DOTALL) - self.theResult = xRep.sub(lambda x: repDict[x.group(0)], self.theResult) - - return - - def doConvert(self): - - texTags = { - self.FMT_B_B : r"\textbf{", - self.FMT_B_E : r"}", - self.FMT_I_B : r"\textit{", - self.FMT_I_E : r"}", - self.FMT_U_B : r"\underline{", - self.FMT_U_E : r"}", - } - - self.theResult = "" - thisPar = [] - for tType, tText, tFormat, tAlign in self.theTokens: - - begText = "" - endText = "\n" - if tAlign == self.A_CENTRE: - begText = "\\begin{center}\n" - endText = "\\end{center}\n\n" - - # First check if we have a comment or plain text, as they - # need some extra replacing before we proceed to wrapping - # and final formatting. - if tType == self.T_COMMENT: - tText = "%% %s" % tText - - elif tType == self.T_TEXT: - tTemp = tText - for xPos, xLen, xFmt in reversed(tFormat): - tTemp = tTemp[:xPos]+texTags[xFmt]+tTemp[xPos+xLen:] - tText = tTemp - - tLen = len(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. - if tType == self.T_EMPTY: - if len(thisPar) > 0: - self.theResult += begText - for tTemp in thisPar: - self.theResult += "%s\n" % tTemp - self.theResult += endText - thisPar = [] - - elif tType == self.T_HEAD1: - self.theResult += begText - self.theResult += "{\\Huge %s}\n" % self._escapeUnicode(tText) - self.theResult += endText - - elif tType == self.T_HEAD2: - self.theResult += "\\chapter*{%s}\n\n" % self._escapeUnicode(tText) - - elif tType == self.T_HEAD3: - self.theResult += "\\section*{%s}\n\n" % self._escapeUnicode(tText) - - elif tType == self.T_HEAD4: - self.theResult += "\\subsection*{%s}\n\n" % self._escapeUnicode(tText) - - elif tType == self.T_SEP: - self.theResult += begText - self.theResult += "%s\n" % self._escapeUnicode(tText) - self.theResult += endText - - elif tType == self.T_SKIP: - self.theResult += "\\bigskip\n" - self.theResult += "\\bigskip\n\n" - - elif tType == self.T_TEXT: - if tText.endswith(" "): - thisPar.append(self._escapeUnicode(tText.rstrip())+"\\newline") - else: - thisPar.append(self._escapeUnicode(tText.rstrip())) - - elif tType == self.T_PBREAK: - self.theResult += "\\newpage\n\n" - - elif tType == self.T_COMMENT and self.doComments: - self.theResult += "%s\n\n" % tText - - elif tType == self.T_KEYWORD and self.doKeywords: - self.theResult += "%% @%s\n\n" % tText - - return - - def _escapeUnicode(self, theText): - try: - import latexcodec - return codecs.encode(theText, "ulatex+utf8") - except: - self.texCodecFail = True - return theText - -# END Class ToLaTeX diff --git a/nw/convert/text/tomarkdown.py b/nw/convert/text/tomarkdown.py deleted file mode 100644 index 97e5c7a4..00000000 --- a/nw/convert/text/tomarkdown.py +++ /dev/null @@ -1,136 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter Markdown Text Converter - - novelWriter – Markdown Text Converter -======================================= - Extends the Tokenizer class to write Markdown - - File History: - Created: 2019-10-19 [0.3] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import textwrap -import logging -import re -import nw - -from nw.convert.tokenizer import Tokenizer - -logger = logging.getLogger(__name__) - -class ToMarkdown(Tokenizer): - - def __init__(self, theProject, theParent): - Tokenizer.__init__(self, theProject, theParent) - return - - def doConvert(self): - - mdTags = { - self.FMT_B_B : "**", - self.FMT_B_E : "**", - self.FMT_I_B : "_", - self.FMT_I_E : "_", - self.FMT_U_B : "__", - self.FMT_U_E : "__", - } - - 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 - ) - - self.theResult = "" - thisPar = [] - for tType, tText, tFormat, tAlign in self.theTokens: - - # First check if we have a comment or plain text, as they - # need some extra replacing before we proceed to wrapping - # and final formatting. - if tType == self.T_COMMENT: - tText = " %s" % tText - - elif tType == self.T_TEXT: - tTemp = tText - for xPos, xLen, xFmt in reversed(tFormat): - tTemp = tTemp[:xPos]+mdTags[xFmt]+tTemp[xPos+xLen:] - tText = tTemp - - 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: - tText = textwrap.fill( - tText.strip(),initial_indent=" ",subsequent_indent=" " - ) - 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. - if tType == self.T_EMPTY: - if len(thisPar) > 0: - tTemp = "\n".join(thisPar) - self.theResult += "%s\n\n" % tTemp.rstrip() - thisPar = [] - - elif tType == self.T_HEAD1: - self.theResult += "# %s\n\n" % tText - - elif tType == self.T_HEAD2: - self.theResult += "## %s\n\n" % tText - - elif tType == self.T_HEAD3: - self.theResult += "### %s\n\n" % tText - - elif tType == self.T_HEAD4: - self.theResult += "#### %s\n\n" % tText - - elif tType == self.T_SEP: - self.theResult += "%s\n\n" % tText - - elif tType == self.T_SKIP: - self.theResult += "\n\n\n" - - elif tType == self.T_TEXT: - thisPar.append(tText) - - elif tType == self.T_COMMENT and self.doComments: - self.theResult += "%s\n\n" % tText - - elif tType == self.T_KEYWORD and self.doKeywords: - self.theResult += "%s\n\n" % tText - - return - -# END Class ToMarkdown diff --git a/nw/convert/text/totext.py b/nw/convert/text/totext.py deleted file mode 100644 index b4d3af64..00000000 --- a/nw/convert/text/totext.py +++ /dev/null @@ -1,154 +0,0 @@ -# -*- coding: utf-8 -*- -"""novelWriter Plain Text Converter - - novelWriter – Plain Text Converter -==================================== - Extends the Tokenizer class to convert to plain text - - File History: - Created: 2019-10-26 [0.3.1] - - This file is a part of novelWriter - Copyright 2020, Veronica Berglyd Olsen - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -""" - -import textwrap -import logging -import re -import nw - -from nw.convert.tokenizer import Tokenizer -from nw.constants import nwUnicode - -logger = logging.getLogger(__name__) - -class ToText(Tokenizer): - - def __init__(self, theProject, theParent): - Tokenizer.__init__(self, theProject, theParent) - return - - def doAutoReplace(self): - Tokenizer.doAutoReplace(self) - - repDict = { - "\t" : " ", - nwUnicode.U_NBSP : " ", - } - 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) - - return - - def doConvert(self): - """Converts the tokenized text into plain text. - """ - - 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 - ) - - self.theResult = "" - thisPar = [] - for tType, tText, tFormat, tAlign in self.theTokens: - - # First check if we have a comment or plain text, as they - # need some extra replacing before we proceed to wrapping - # and final formatting. - if tType == self.T_COMMENT: - tText = "[%s]" % tText - - elif tType == self.T_TEXT: - tTemp = tText - for xPos, xLen, xFmt in reversed(tFormat): - tTemp = tTemp[:xPos]+tTemp[xPos+xLen:] - tText = tTemp - - tLen = len(tText) - - # The text can now be word wrapped, if we have requested - # this and it's needed. - if tAlign == self.A_CENTRE: - if self.wordWrap > 0: - if tLen > self.wordWrap: - aText = tWrap.wrap(tText) - for n in range(len(aText)): - aText[n] = self._centreText(aText[n],self.wordWrap) - tText = "\n".join(aText) - else: - tText = self._centreText(tText,self.wordWrap) - else: - if self.wordWrap > 0 and tLen > self.wordWrap: - 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. - if tType == self.T_EMPTY: - if len(thisPar) > 0: - tTemp = "\n".join(thisPar) - self.theResult += "%s\n\n" % tTemp.rstrip() - thisPar = [] - - elif tType == self.T_HEAD1: - uLine = "="*min(tLen,self.wordWrap) - if tAlign == self.A_CENTRE: - uLine = self._centreText(uLine,self.wordWrap) - self.theResult += "%s\n%s\n\n" % (tText,uLine) - - elif tType == self.T_HEAD2: - uLine = "~"*min(tLen,self.wordWrap) - self.theResult += "%s\n%s\n\n" % (tText,uLine) - - elif tType == self.T_HEAD3: - uLine = "-"*min(tLen,self.wordWrap) - self.theResult += "%s\n%s\n\n" % (tText,uLine) - - elif tType == self.T_HEAD4: - self.theResult += "%s\n\n" % tText - - elif tType == self.T_SEP: - if self.wordWrap > 0 and tLen < self.wordWrap: - tText = self._centreText(tText,self.wordWrap) - self.theResult += "%s\n\n" % tText - - elif tType == self.T_SKIP: - self.theResult += "\n\n\n" - - elif tType == self.T_TEXT: - thisPar.append(tText) - - elif tType == self.T_COMMENT and self.doComments: - self.theResult += "%s\n\n" % tText - - elif tType == self.T_KEYWORD and self.doKeywords: - self.theResult += "%s\n\n" % tText - - return - -# END Class ToText