diff --git a/toMkdocs/markdownTools.py b/toMkdocs/markdownTools.py index 56ea86e823dedbc79f93da83b0b4a8cd5a77d95c..6c9dc412bf6b9b9f2df992fc2e6dce5f991a5400 100644 --- a/toMkdocs/markdownTools.py +++ b/toMkdocs/markdownTools.py @@ -9,7 +9,7 @@ """ Various tools for markdown processing """ from __future__ import annotations -from typing import Callable +from typing import Callable, Optional from dataclasses import dataclass import base64, hashlib @@ -69,6 +69,7 @@ class LineType(Enum): TABLESEPARATOR = auto() TABLEROW = auto() TABLELASTROW = auto() + RAWHTML = auto() @dataclass @@ -78,6 +79,16 @@ class Line: 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 class Clause: """ Represents a clause in the markdown file. """ @@ -176,6 +187,18 @@ class Clause: The number of characters in the clause. """ 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: """ Represents a footnote in the markdown file. """ @@ -361,12 +384,23 @@ class Document: clause.lines = lines + def __str__(self) -> str: + """ Return the document as a string. """ + return '\n'.join([ str(c) for c in self.clauses ]) + + + def __repr__(self) -> str: + """ Return the document as a string. """ + return self.__str__() + -def analyseMarkdown(filename:str) -> Document: +def analyseMarkdown(filename:Optional[str]=None, inLines:Optional[list[str]]=None) -> Document: """ Analyse the markdown file and split it into clauses. + Either the filename or the inLines must be provided. Args: filename: The name of the markdown file. + inLines: The lines of the markdown file. Returns: The document object. @@ -388,9 +422,7 @@ def analyseMarkdown(filename:str) -> Document: printDebug(htmltable) except Exception as e: printError(f"Error: {e}") - # TODO move this outside of the analyseMarkdown function !!! - for row in htmltable: - outClauses[-1].append(Line(row, LineType.TABLEROW)) + outClauses[-1].append(Line(htmltable, LineType.RAWHTML)) gridTable = '' @@ -398,8 +430,13 @@ def analyseMarkdown(filename:str) -> Document: # Read the file. # Note: We use utf-8 and replace errors to avoid problems with special or unknown characters. - with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: - inLines = file.readlines() + if filename and not inLines: + with open(filename, 'r', encoding = 'utf-8', errors = 'replace') as file: + 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. outClauses:list[Clause] = [Clause(0, '', '', [])]