From 3a46297692715037fc5742ebf3ee338123bfceb3 Mon Sep 17 00:00:00 2001
From: ankraft <an.kraft@gmail.com>
Date: Tue, 17 Sep 2024 12:20:58 +0200
Subject: [PATCH] Improved marking of hanging paragraphs

---
 toMkdocs/toMkdocs.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/toMkdocs/toMkdocs.py b/toMkdocs/toMkdocs.py
index cf5e273..a357d12 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:
-- 
GitLab