From 446295983e013ede3ca6908c710cf36b4fd3df8c Mon Sep 17 00:00:00 2001
From: ankraft <an.kraft@gmail.com>
Date: Fri, 26 Apr 2024 11:30:27 +0200
Subject: [PATCH] Optionally (default: true) remove hanging paragraphs from
 clauses with sub-clauses

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

diff --git a/toMkdocs/toMkdocs.py b/toMkdocs/toMkdocs.py
index 531b97e..4acaae0 100644
--- a/toMkdocs/toMkdocs.py
+++ b/toMkdocs/toMkdocs.py
@@ -275,12 +275,13 @@ def splitMarkdownDocument(clauses:list[Clause],
 	return outClauses
 
 
-def prepareForMkdocs(clauses:list[Clause]) -> list[Clause]:
+def prepareForMkdocs(clauses:list[Clause], includeHangingParagraphs:bool = False) -> list[Clause]:
 	"""	Prepare the clauses for MkDocs. This includes removing the heading
 		from the clauses and marking the clauses that are only for navigation.
 
 		Args:
 			clauses: The list of clauses.
+			includeHangingParagraphs: Include hanging paragraphs in the output.
 		
 		Returns:
 			The list of clauses.
@@ -298,6 +299,17 @@ def prepareForMkdocs(clauses:list[Clause]) -> list[Clause]:
 			while clause.linesCount > 0 and clause.lines[0].text.strip() == '':
 				clause.lines.pop(0)
 	
+	# Detect and handle hanging paragraphs. This is extra text in a clause, which
+	# has sub-clauses. This text is not allowed in oneM2M specifications.
+	for i, clause in enumerate(clauses):
+		if clause.level > 0 and clause.linesCount > 0:
+			# 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)"}')
+				if not includeHangingParagraphs:
+					clauses[i].lines = []
+
 	# Repair wrong markdown for indented lines.
 	# Add 2 spaces to existing 2-space indentions
 	for clause in clauses:
@@ -495,7 +507,7 @@ def processDocument(args:argparse.Namespace) -> None:
 									args.include_title)
 	clauses = updateLinks(clauses)
 	clauses = updateNotes(clauses)
-	clauses = prepareForMkdocs(clauses)
+	clauses = prepareForMkdocs(clauses, args.include_hanging_paragraphs)
 
 	# Write the clauses to files
 	writeClauses(clauses, document, args.title)
@@ -511,6 +523,7 @@ if __name__ == '__main__':
 	parser.add_argument('--verbose', '-v', action = 'store_true', help = 'verbose output during processing')
 	parser.add_argument('--very-verbose', '-vv', action = 'store_true', help = 'very verbose output during processing')
 	parser.add_argument('--ignore-clause', '-ic', metavar = 'clause', nargs = '+', default = [ 'Contents', 'History' ], help = 'ignore headers in the markdown document')
+	parser.add_argument('--include-hanging-paragraphs', '-ihp', action = 'store_true', default = False, help = 'include hanging paragraphs (text in clauses with sub-clauses) in the output files')
 	parser.add_argument('--include-title', '-it', action = 'store_true', help = 'include the content before the first heading in the output files as "0.md"')
 	parser.add_argument('--split-level', '-sl', metavar = 'level', type = int, default = 2, help = 'on which level to split clauses to separate files')
 	parser.add_argument('--media-directory', '-md', metavar = 'media-directory', default = 'media', help = 'directory name where media files are stored')
-- 
GitLab