From 0700dd59bb9f865370d4f25a905f73a899429fe1 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 26 Sep 2023 18:39:50 +0200 Subject: [PATCH] Clean up variables and typing in doc converters --- novelwriter/core/tohtml.py | 64 ++++++++++++++++++-------------------- novelwriter/core/tomd.py | 53 +++++++++++++++---------------- novelwriter/core/toodt.py | 40 ++++++++++++------------ 3 files changed, 75 insertions(+), 82 deletions(-) diff --git a/novelwriter/core/tohtml.py b/novelwriter/core/tohtml.py index 8459ae59..a4d4bb3f 100644 --- a/novelwriter/core/tohtml.py +++ b/novelwriter/core/tohtml.py @@ -111,7 +111,7 @@ class ToHtml(Tokenizer): def getFullResultSize(self) -> int: """Return the size of the full HTML result.""" - return sum([len(x) for x in self._fullHTML]) + return sum(len(x) for x in self._fullHTML) def doPreProcessing(self) -> None: """Extend the auto-replace to also properly encode some unicode @@ -122,9 +122,7 @@ class ToHtml(Tokenizer): return def doConvert(self) -> None: - """Convert the list of text tokens into a HTML document saved - to _result. - """ + """Convert the list of text tokens into an HTML document.""" if self._genMode == self.M_PREVIEW: htmlTags = { # HTML4 + CSS2 (for Qt) self.FMT_B_B: "", @@ -160,9 +158,9 @@ class ToHtml(Tokenizer): self._result = "" - thisPar = [] - parStyle = None - tmpResult = [] + para = [] + pStyle = None + lines = [] for tType, tLine, tText, tFormat, tStyle in self._tokens: @@ -231,69 +229,67 @@ class ToHtml(Tokenizer): # Process Text Type if tType == self.T_EMPTY: - if parStyle is None: - parStyle = "" - if len(thisPar) > 1 and self._cssStyles: - parClass = " class='break'" + if pStyle is None: + pStyle = "" + if len(para) > 1 and self._cssStyles: + pClass = " class='break'" else: - parClass = "" - if len(thisPar) > 0: - tTemp = "
".join(thisPar) - tmpResult.append(f"{tTemp.rstrip()}

\n") - thisPar = [] - parStyle = None + pClass = "" + if len(para) > 0: + tTemp = "
".join(para) + lines.append(f"{tTemp.rstrip()}

\n") + para = [] + pStyle = None elif tType == self.T_TITLE: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"

{aNm}{tHead}

\n") + lines.append(f"

{aNm}{tHead}

\n") elif tType == self.T_UNNUM: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"<{h2}{hStyle}>{aNm}{tHead}\n") + lines.append(f"<{h2}{hStyle}>{aNm}{tHead}\n") elif tType == self.T_HEAD1: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}\n") + lines.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}\n") elif tType == self.T_HEAD2: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"<{h2}{hStyle}>{aNm}{tHead}\n") + lines.append(f"<{h2}{hStyle}>{aNm}{tHead}\n") elif tType == self.T_HEAD3: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"<{h3}{hStyle}>{aNm}{tHead}\n") + lines.append(f"<{h3}{hStyle}>{aNm}{tHead}\n") elif tType == self.T_HEAD4: tHead = tText.replace(nwHeadFmt.BR, "
") - tmpResult.append(f"<{h4}{hStyle}>{aNm}{tHead}\n") + lines.append(f"<{h4}{hStyle}>{aNm}{tHead}\n") elif tType == self.T_SEP: - tmpResult.append(f"

{tText}

\n") + lines.append(f"

{tText}

\n") elif tType == self.T_SKIP: - tmpResult.append(f"

 

\n") + lines.append(f"

 

\n") elif tType == self.T_TEXT: tTemp = tText - if parStyle is None: - parStyle = hStyle + if pStyle is None: + pStyle = hStyle for xPos, xLen, xFmt in reversed(tFormat): tTemp = tTemp[:xPos] + htmlTags[xFmt] + tTemp[xPos+xLen:] - thisPar.append(stripEscape(tTemp.rstrip())) + para.append(stripEscape(tTemp.rstrip())) elif tType == self.T_SYNOPSIS and self._doSynopsis: - tmpResult.append(self._formatSynopsis(tText)) + lines.append(self._formatSynopsis(tText)) elif tType == self.T_COMMENT and self._doComments: - tmpResult.append(self._formatComments(tText)) + lines.append(self._formatComments(tText)) elif tType == self.T_KEYWORD and self._doKeywords: tTemp = f"{self._formatKeywords(tText)}

\n" - tmpResult.append(tTemp) - - self._result = "".join(tmpResult) - tmpResult = [] + lines.append(tTemp) + self._result = "".join(lines) if self._genMode != self.M_PREVIEW: self._fullHTML.append(self._result) diff --git a/novelwriter/core/tomd.py b/novelwriter/core/tomd.py index c9859318..ec1646ad 100644 --- a/novelwriter/core/tomd.py +++ b/novelwriter/core/tomd.py @@ -65,10 +65,12 @@ class ToMarkdown(Tokenizer): ## def setStandardMarkdown(self) -> None: + """Set the converter to use standard Markdown formatting.""" self._genMode = self.M_STD return def setGitHubMarkdown(self) -> None: + """Set the converter to use GitHub Markdown formatting.""" self._genMode = self.M_GH return @@ -78,12 +80,10 @@ class ToMarkdown(Tokenizer): def getFullResultSize(self) -> int: """Return the size of the full Markdown result.""" - return sum([len(x) for x in self._fullMD]) + return sum(len(x) for x in self._fullMD) def doConvert(self) -> None: - """Convert the list of text tokens into a HTML document saved - to theResult. - """ + """Convert the list of text tokens into a Markdown document.""" if self._genMode == self.M_STD: # Standard mdTags = { @@ -107,68 +107,65 @@ class ToMarkdown(Tokenizer): self._result = "" - thisPar = [] - tmpResult = [] + para = [] + lines = [] for tType, _, tText, tFormat, tStyle in self._tokens: - # Process Text Type if tType == self.T_EMPTY: - if len(thisPar) > 0: - tTemp = (" \n".join(thisPar)).rstrip(" ") - tmpResult.append(f"{tTemp}\n\n") - thisPar = [] + if len(para) > 0: + tTemp = (" \n".join(para)).rstrip(" ") + lines.append(f"{tTemp}\n\n") + para = [] elif tType == self.T_TITLE: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"# {tHead}\n\n") + lines.append(f"# {tHead}\n\n") elif tType == self.T_UNNUM: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"## {tHead}\n\n") + lines.append(f"## {tHead}\n\n") elif tType == self.T_HEAD1: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"# {tHead}\n\n") + lines.append(f"# {tHead}\n\n") elif tType == self.T_HEAD2: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"## {tHead}\n\n") + lines.append(f"## {tHead}\n\n") elif tType == self.T_HEAD3: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"### {tHead}\n\n") + lines.append(f"### {tHead}\n\n") elif tType == self.T_HEAD4: tHead = tText.replace(nwHeadFmt.BR, "\n") - tmpResult.append(f"#### {tHead}\n\n") + lines.append(f"#### {tHead}\n\n") elif tType == self.T_SEP: - tmpResult.append("%s\n\n" % tText) + lines.append(f"{tText}\n\n") elif tType == self.T_SKIP: - tmpResult.append("\n\n\n") + lines.append("\n\n\n") elif tType == self.T_TEXT: tTemp = tText for xPos, xLen, xFmt in reversed(tFormat): tTemp = tTemp[:xPos] + mdTags[xFmt] + tTemp[xPos+xLen:] - thisPar.append(tTemp.rstrip()) + para.append(tTemp.rstrip()) elif tType == self.T_SYNOPSIS and self._doSynopsis: - locName = self._localLookup("Synopsis") - tmpResult.append(f"**{locName}:** {tText}\n\n") + label = self._localLookup("Synopsis") + lines.append(f"**{label}:** {tText}\n\n") elif tType == self.T_COMMENT and self._doComments: - locName = self._localLookup("Comment") - tmpResult.append(f"**{locName}:** {tText}\n\n") + label = self._localLookup("Comment") + lines.append(f"**{label}:** {tText}\n\n") elif tType == self.T_KEYWORD and self._doKeywords: - tmpResult.append(self._formatKeywords(tText, tStyle)) - - self._result = "".join(tmpResult) - tmpResult = [] + lines.append(self._formatKeywords(tText, tStyle)) + self._result = "".join(lines) self._fullMD.append(self._result) return diff --git a/novelwriter/core/toodt.py b/novelwriter/core/toodt.py index b69af17b..4f557dff 100644 --- a/novelwriter/core/toodt.py +++ b/novelwriter/core/toodt.py @@ -394,9 +394,9 @@ class ToOdt(Tokenizer): self.FMT_D_E: "s_", # Strikethrough close format } - thisPar = [] - thisFmt = [] - parStyle = None + fmt = [] + para = [] + pStyle = None for tType, _, tText, tFormat, tStyle in self._tokens: # Styles @@ -429,20 +429,20 @@ class ToOdt(Tokenizer): # Process Text Types if tType == self.T_EMPTY: - if len(thisPar) > 1 and parStyle is not None: + if len(para) > 1 and pStyle is not None: if self._doJustify: - parStyle.setTextAlign("left") + pStyle.setTextAlign("left") - if len(thisPar) > 0 and parStyle is not None: - tTemp = "\n".join(thisPar) - fTemp = " ".join(thisFmt) + if len(para) > 0 and pStyle is not None: + tTemp = "\n".join(para) + fTemp = " ".join(fmt) tTxt = tTemp.rstrip() tFmt = fTemp[:len(tTxt)] - self._addTextPar("Text_20_body", parStyle, tTxt, tFmt=tFmt) + self._addTextPar("Text_20_body", pStyle, tTxt, tFmt=tFmt) - thisPar = [] - thisFmt = [] - parStyle = None + fmt = [] + para = [] + pStyle = None elif tType == self.T_TITLE: tHead = tText.replace(nwHeadFmt.BR, "\n") @@ -475,8 +475,8 @@ class ToOdt(Tokenizer): self._addTextPar("Separator", oStyle, "") elif tType == self.T_TEXT: - if parStyle is None: - parStyle = oStyle + if pStyle is None: + pStyle = oStyle tFmt = " "*len(tText) for xPos, xLen, xFmt in tFormat: @@ -484,8 +484,8 @@ class ToOdt(Tokenizer): tTxt = tText.rstrip() tFmt = tFmt[:len(tTxt)] - thisPar.append(tTxt) - thisFmt.append(tFmt) + para.append(tTxt) + fmt.append(tFmt) elif tType == self.T_SYNOPSIS and self._doSynopsis: tTemp, fTemp = self._formatSynopsis(tText) @@ -501,8 +501,8 @@ class ToOdt(Tokenizer): return - def closeDocument(self): - """Return the serialised XML document""" + def closeDocument(self) -> None: + """Pack the styles of the XML document.""" # Build the auto-generated styles for styleName, styleObj in self._autoPara.values(): styleObj.packXML(self._xAuto, styleName) @@ -510,7 +510,7 @@ class ToOdt(Tokenizer): styleObj.packXML(self._xAuto, styleName) return - def saveFlatXML(self, path: str | Path): + def saveFlatXML(self, path: str | Path) -> None: """Save the data to an .fodt file.""" with open(path, mode="wb") as fObj: xml = ET.ElementTree(self._dFlat) @@ -519,7 +519,7 @@ class ToOdt(Tokenizer): logger.info("Wrote file: %s", path) return - def saveOpenDocText(self, path: str | Path): + def saveOpenDocText(self, path: str | Path) -> None: """Save the data to an .odt file.""" mMani = _mkTag("manifest", "manifest") mVers = _mkTag("manifest", "version")