Add annotations to tokenizer, html and markdown classes

This commit is contained in:
Veronica Berglyd Olsen
2023-06-06 00:35:57 +02:00
parent 850ee73ed4
commit 6b6c517b1f
3 changed files with 177 additions and 166 deletions
+29 -38
View File
@@ -22,10 +22,14 @@ 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 <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import logging
from pathlib import Path
from novelwriter.constants import nwLabels
from novelwriter.core.project import NWProject
from novelwriter.core.tokenizer import Tokenizer
logger = logging.getLogger(__name__)
@@ -36,8 +40,8 @@ class ToMarkdown(Tokenizer):
M_STD = 0 # Standard Markdown
M_GH = 1 # GitHub Markdown
def __init__(self, theProject):
super().__init__(theProject)
def __init__(self, project: NWProject):
super().__init__(project)
self._genMode = self.M_STD
self._fullMD = []
@@ -49,7 +53,8 @@ class ToMarkdown(Tokenizer):
##
@property
def fullMD(self):
def fullMD(self) -> list:
"""Return the markdown as a list."""
return self._fullMD
##
@@ -68,9 +73,8 @@ class ToMarkdown(Tokenizer):
# Class Methods
##
def getFullResultSize(self):
"""Return the size of the full Markdown result.
"""
def getFullResultSize(self) -> int:
"""Return the size of the full Markdown result."""
return sum([len(x) for x in self._fullMD])
def doConvert(self):
@@ -166,49 +170,36 @@ class ToMarkdown(Tokenizer):
return
def saveMarkdown(self, savePath):
"""Save the data to a plain text file.
"""
with open(savePath, mode="w", encoding="utf-8") as outFile:
theText = "".join(self._fullMD)
outFile.write(theText)
def saveMarkdown(self, path: str | Path):
"""Save the data to a plain text file."""
with open(path, mode="w", encoding="utf-8") as outFile:
outFile.write("".join(self._fullMD))
return
def replaceTabs(self, nSpaces=8, spaceChar=" "):
"""Replace tabs with spaces.
"""
fullMD = []
eightSpace = spaceChar*nSpaces
for aPage in self._fullMD:
fullMD.append(aPage.replace("\t", eightSpace))
self._fullMD = fullMD
def replaceTabs(self, nSpaces: int = 8, spaceChar: str = " "):
"""Replace tabs with spaces."""
spaces = spaceChar*nSpaces
self._fullMD = [p.replace("\t", spaces) for p in self._fullMD]
return
##
# Internal Functions
##
def _formatKeywords(self, tText, tStyle):
"""Apply Markdown formatting to keywords.
"""
isValid, theBits, _ = self._project.index.scanThis("@"+tText)
if not isValid or not theBits:
def _formatKeywords(self, text: str, style: int) -> str:
"""Apply Markdown formatting to keywords."""
valid, bits, _ = self._project.index.scanThis("@"+text)
if not valid or not bits:
return ""
retText = ""
if theBits[0] in nwLabels.KEY_NAME:
retText += f"**{nwLabels.KEY_NAME[theBits[0]]}:** "
result = ""
if bits[0] in nwLabels.KEY_NAME:
result += f"**{nwLabels.KEY_NAME[bits[0]]}:** "
if len(bits) > 1:
result += ", ".join(bits[1:])
if len(theBits) > 1:
retText += ", ".join(theBits[1:])
result += " \n" if style & self.A_Z_BTMMRG else "\n\n"
if tStyle & self.A_Z_BTMMRG:
retText += " \n"
else:
retText += "\n\n"
return retText
return result
# END Class ToMarkdown