diff --git a/toMkdocs/toMkdocs.py b/toMkdocs/toMkdocs.py index cf5e273646c4583a57c3e0e981c596816a4c2da8..a357d1248dcc2073b0c753d58d6d12244c76834b 100644 --- a/toMkdocs/toMkdocs.py +++ b/toMkdocs/toMkdocs.py @@ -34,7 +34,7 @@ class LineType(Enum): @dataclass class Line: """ Represents a line in the markdown file. """ - text:str = '' + text:str = '\n' lineType:LineType = LineType.TEXT @@ -128,12 +128,13 @@ class Clause: def __len__(self) -> int: - """ Return the number of characters in the clause. + """ Return the number of characters in the clause. This does not include + empty lines or lines that contain only whitespace. Returns: The number of characters in the clause. """ - return sum([ len(l.text) for l in self.lines ]) + return sum([ len(l.text.strip()) for l in self.lines ]) _matchHeader = re.compile(r'(#+)\s+(.*)', re.IGNORECASE) @@ -341,11 +342,11 @@ def prepareForMkdocs(clauses:list[Clause], includeHangingParagraphs:bool = False # Check if there is a sub-clause in the next clause if i + 1 < len(clauses) and clauses[i+1].level > clause.level: # This is a hanging paragraph. Remove the text from the current clause. - print(f'[yellow]Hanging paragraph in clause "{clause.title}" {"(removed)" if not includeHangingParagraphs else "(kept)"}') + print(f'[yellow]Hanging paragraph in clause "{clause.title}" {"(removed)" if not includeHangingParagraphs else "(kept + warning)"}') if not includeHangingParagraphs: clauses[i].lines = [] else: - clauses[i].lines = [Line("<mark>Editor note: This is a hanging paragraph and it must be moved to its own clause</mark>")] + [Line()] + clauses[i].lines + clauses[i].lines = [Line('<mark>Editor note: This is a hanging paragraph and it must be moved to its own clause</mark>\n'), Line()] + clauses[i].lines # Repair wrong markdown for indented lines. # Add 2 spaces to existing 2-space indentions @@ -510,7 +511,9 @@ def writeClauses(outClauses:list[Clause], filename:str, navTitle:str, addNavTitl else: file.write(f"{indentation}{' '*f.level}- '{_title}':\n") if len(f) > 0: - file.write(f"{indentation}{' '*nextClause.level}- 'Hanging paragraph': '{navTitle}/{f.clauseNumber}.md'\n") + file.write(f"{indentation}{' '*nextClause.level}- '<mark>Hanging paragraph</mark>': '{navTitle}/{f.clauseNumber}.md'\n") + if verbose: + print(f'[dim]Added hanging paragraph to navigation for clause "{f.title}"') def copyMediaFiles(filename:str, navTitle:str, mediaDirectory:str = 'media') -> None: