Loading toMkdocs/markdownTools.py +42 −5 Changes for toMkdocs/markdownTools.py: 42 added lines, 5 removed lines. Original line number Original line Diff line number Diff line Loading @@ -9,7 +9,7 @@ """ Various tools for markdown processing """ Various tools for markdown processing """ """ from __future__ import annotations from __future__ import annotations from typing import Callable from typing import Callable, Optional from dataclasses import dataclass from dataclasses import dataclass import base64, hashlib import base64, hashlib Loading Loading @@ -69,6 +69,7 @@ class LineType(Enum): TABLESEPARATOR = auto() TABLESEPARATOR = auto() TABLEROW = auto() TABLEROW = auto() TABLELASTROW = auto() TABLELASTROW = auto() RAWHTML = auto() @dataclass @dataclass Loading @@ -78,6 +79,16 @@ class Line: lineType:LineType = LineType.TEXT lineType:LineType = LineType.TEXT def __str__(self) -> str: """ Return the line as a string. """ return self.text def __repr__(self) -> str: """ Return the line as a string. """ return self.__str__() @dataclass @dataclass class Clause: class Clause: """ Represents a clause in the markdown file. """ """ Represents a clause in the markdown file. """ Loading Loading @@ -177,6 +188,18 @@ class Clause: """ """ return sum([ len(l.text.strip()) for l in self.lines ]) return sum([ len(l.text.strip()) for l in self.lines ]) def __str__(self) -> str: """ Return the clause as a string. """ return ''.join([str(l) for l in self.lines ]) def __repr__(self) -> str: """ Return the clause as a string. """ return self.__str__() class Footnote: class Footnote: """ Represents a footnote in the markdown file. """ """ Represents a footnote in the markdown file. """ def __init__(self, id:str, line:Line) -> None: def __init__(self, id:str, line:Line) -> None: Loading Loading @@ -361,12 +384,23 @@ class Document: clause.lines = lines clause.lines = lines def __str__(self) -> str: """ Return the document as a string. """ return '\n'.join([ str(c) for c in self.clauses ]) def analyseMarkdown(filename:str) -> Document: def __repr__(self) -> str: """ Return the document as a string. """ return self.__str__() def analyseMarkdown(filename:Optional[str]=None, inLines:Optional[list[str]]=None) -> Document: """ Analyse the markdown file and split it into clauses. """ Analyse the markdown file and split it into clauses. Either the filename or the inLines must be provided. Args: Args: filename: The name of the markdown file. filename: The name of the markdown file. inLines: The lines of the markdown file. Returns: Returns: The document object. The document object. Loading @@ -388,9 +422,7 @@ def analyseMarkdown(filename:str) -> Document: printDebug(htmltable) printDebug(htmltable) except Exception as e: except Exception as e: printError(f"Error: {e}") printError(f"Error: {e}") # TODO move this outside of the analyseMarkdown function !!! outClauses[-1].append(Line(htmltable, LineType.RAWHTML)) for row in htmltable: outClauses[-1].append(Line(row, LineType.TABLEROW)) gridTable = '' gridTable = '' Loading @@ -398,8 +430,13 @@ def analyseMarkdown(filename:str) -> Document: # Read the file. # Read the file. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. if filename and not inLines: with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: inLines = file.readlines() inLines = file.readlines() elif not filename and inLines: pass else: raise ValueError('Either the filename or the lines must be provided.') # The list of clauses. The first clause contains the text before the first heading. # The list of clauses. The first clause contains the text before the first heading. outClauses:list[Clause] = [Clause(0, '', '', [])] outClauses:list[Clause] = [Clause(0, '', '', [])] Loading Loading
toMkdocs/markdownTools.py +42 −5 Changes for toMkdocs/markdownTools.py: 42 added lines, 5 removed lines. Original line number Original line Diff line number Diff line Loading @@ -9,7 +9,7 @@ """ Various tools for markdown processing """ Various tools for markdown processing """ """ from __future__ import annotations from __future__ import annotations from typing import Callable from typing import Callable, Optional from dataclasses import dataclass from dataclasses import dataclass import base64, hashlib import base64, hashlib Loading Loading @@ -69,6 +69,7 @@ class LineType(Enum): TABLESEPARATOR = auto() TABLESEPARATOR = auto() TABLEROW = auto() TABLEROW = auto() TABLELASTROW = auto() TABLELASTROW = auto() RAWHTML = auto() @dataclass @dataclass Loading @@ -78,6 +79,16 @@ class Line: lineType:LineType = LineType.TEXT lineType:LineType = LineType.TEXT def __str__(self) -> str: """ Return the line as a string. """ return self.text def __repr__(self) -> str: """ Return the line as a string. """ return self.__str__() @dataclass @dataclass class Clause: class Clause: """ Represents a clause in the markdown file. """ """ Represents a clause in the markdown file. """ Loading Loading @@ -177,6 +188,18 @@ class Clause: """ """ return sum([ len(l.text.strip()) for l in self.lines ]) return sum([ len(l.text.strip()) for l in self.lines ]) def __str__(self) -> str: """ Return the clause as a string. """ return ''.join([str(l) for l in self.lines ]) def __repr__(self) -> str: """ Return the clause as a string. """ return self.__str__() class Footnote: class Footnote: """ Represents a footnote in the markdown file. """ """ Represents a footnote in the markdown file. """ def __init__(self, id:str, line:Line) -> None: def __init__(self, id:str, line:Line) -> None: Loading Loading @@ -361,12 +384,23 @@ class Document: clause.lines = lines clause.lines = lines def __str__(self) -> str: """ Return the document as a string. """ return '\n'.join([ str(c) for c in self.clauses ]) def analyseMarkdown(filename:str) -> Document: def __repr__(self) -> str: """ Return the document as a string. """ return self.__str__() def analyseMarkdown(filename:Optional[str]=None, inLines:Optional[list[str]]=None) -> Document: """ Analyse the markdown file and split it into clauses. """ Analyse the markdown file and split it into clauses. Either the filename or the inLines must be provided. Args: Args: filename: The name of the markdown file. filename: The name of the markdown file. inLines: The lines of the markdown file. Returns: Returns: The document object. The document object. Loading @@ -388,9 +422,7 @@ def analyseMarkdown(filename:str) -> Document: printDebug(htmltable) printDebug(htmltable) except Exception as e: except Exception as e: printError(f"Error: {e}") printError(f"Error: {e}") # TODO move this outside of the analyseMarkdown function !!! outClauses[-1].append(Line(htmltable, LineType.RAWHTML)) for row in htmltable: outClauses[-1].append(Line(row, LineType.TABLEROW)) gridTable = '' gridTable = '' Loading @@ -398,8 +430,13 @@ def analyseMarkdown(filename:str) -> Document: # Read the file. # Read the file. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. if filename and not inLines: with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: inLines = file.readlines() inLines = file.readlines() elif not filename and inLines: pass else: raise ValueError('Either the filename or the lines must be provided.') # The list of clauses. The first clause contains the text before the first heading. # The list of clauses. The first clause contains the text before the first heading. outClauses:list[Clause] = [Clause(0, '', '', [])] outClauses:list[Clause] = [Clause(0, '', '', [])] Loading