Commit ea4853f4 authored by ankraft's avatar ankraft
Browse files

Improved support for stringify documents. Added support for stdin/stdout file processing

parent 4f4e21fe
Loading
Loading
Loading
Loading
+44 −7
Original line number Diff line number Diff line
@@ -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. """
@@ -177,6 +188,18 @@ class 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. """
	def __init__(self, id:str, line:Line) -> None:
@@ -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 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.
		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.
	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, '', '', [])]