Clean up variables and typing in doc converters

This commit is contained in:
Veronica Berglyd Olsen
2023-09-26 18:39:50 +02:00
parent 609366076a
commit 0700dd59bb
3 changed files with 75 additions and 82 deletions
+30 -34
View File
@@ -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: "<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 = "<br/>".join(thisPar)
tmpResult.append(f"<p{parClass+parStyle}>{tTemp.rstrip()}</p>\n")
thisPar = []
parStyle = None
pClass = ""
if len(para) > 0:
tTemp = "<br/>".join(para)
lines.append(f"<p{pClass+pStyle}>{tTemp.rstrip()}</p>\n")
para = []
pStyle = None
elif tType == self.T_TITLE:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<h1 class='title'{hStyle}>{aNm}{tHead}</h1>\n")
lines.append(f"<h1 class='title'{hStyle}>{aNm}{tHead}</h1>\n")
elif tType == self.T_UNNUM:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
lines.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
elif tType == self.T_HEAD1:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}</{h1}>\n")
lines.append(f"<{h1}{h1Cl}{hStyle}>{aNm}{tHead}</{h1}>\n")
elif tType == self.T_HEAD2:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
lines.append(f"<{h2}{hStyle}>{aNm}{tHead}</{h2}>\n")
elif tType == self.T_HEAD3:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<{h3}{hStyle}>{aNm}{tHead}</{h3}>\n")
lines.append(f"<{h3}{hStyle}>{aNm}{tHead}</{h3}>\n")
elif tType == self.T_HEAD4:
tHead = tText.replace(nwHeadFmt.BR, "<br/>")
tmpResult.append(f"<{h4}{hStyle}>{aNm}{tHead}</{h4}>\n")
lines.append(f"<{h4}{hStyle}>{aNm}{tHead}</{h4}>\n")
elif tType == self.T_SEP:
tmpResult.append(f"<p class='sep'{hStyle}>{tText}</p>\n")
lines.append(f"<p class='sep'{hStyle}>{tText}</p>\n")
elif tType == self.T_SKIP:
tmpResult.append(f"<p class='skip'{hStyle}>&nbsp;</p>\n")
lines.append(f"<p class='skip'{hStyle}>&nbsp;</p>\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"<p{hStyle}>{self._formatKeywords(tText)}</p>\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)
+25 -28
View File
@@ -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
+20 -20
View File
@@ -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")