Commit 391f4f34 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Replace markdown markers by html tags in titles for mkdocs.yml

parent 49f86e23
Loading
Loading
Loading
Loading
Loading
+43 −5
Original line number Diff line number Diff line
@@ -19,6 +19,41 @@ verbose = False
veryVerbose = False


def replaceMarkdownMarkersWithHtmlTags(text:str, marker:str, tagStart:str, tagEnd:str) -> str:
	"""	Replace markdown markers by alternating HTML tags.

		Args:
			text: The text to replace the markers in.
			marker: The marker to replace.
			tagStart: The start tag to replace the marker with.
			tagEnd: The end tag to replace the marker with.

		Returns:
			The text with the markers replaced by the HTML tags.
	"""
	result:list[str] = []
	openTag = True
	i = 0
	while i < len(text):
		# Check if the marker is preceded by a space then tagStart, or followed by a space then tagEnd, or neither then just add the text
		if text[i:i+len(marker)] == marker: # Marker found
			if i == 0 or text[i-1] == ' ': # TagStart is preceded by a space
				result.append(tagStart)
				openTag = True
			elif  i+len(marker) < len(text) and text[i+len(marker)] == ' ': # TagEnd is followed by a space
				result.append(tagEnd)
				openTag = False
			else:
				# No space before or after the marker. Add the text as is.
				result.append(text[i])
			i += len(marker)
		else:
			# No marker found. Add the text as is.
			result.append(text[i])
			i += 1
	return ''.join(result)


def printDebug(text:str) -> None:
	"""	Print a debug message.

@@ -140,11 +175,14 @@ def writeClausesMkDocs(document:Document, filename:str, navTitle:str, addNavTitl
			# TODO handle if the next clause is more than one level deeper

			# Reformat the title to be valid for YAML (remove all markdown marks like italic/bold/etc. )
			_title = f.title.replace("'", '"')
			_title = _title.replace("**", "")
			_title = _title.replace("*", "")
			_title = _title.replace("__", "")
			_title = _title.replace("~~", "")
			# Examples: "6.1 **Title**" -> "6.1 Title", "Annex *Title*" -> "Annex Title", "8.1.2.3 __Title__" -> "8.1.2.3 Title", "~~Title~~" -> "Title"
			# Also, replace single quotes with double quotes.
			# This is done to avoid errors when the title contains markdown marks.
			_title = replaceMarkdownMarkersWithHtmlTags(f.title.replace("'", '"'), "**", "<b>", "</b>")
			_title = replaceMarkdownMarkersWithHtmlTags(_title, "_", "<i>", "</i>")
			_title = replaceMarkdownMarkersWithHtmlTags(_title, "*", "<i>", "</i>")
			_title = replaceMarkdownMarkersWithHtmlTags(_title, "__", "<b>", "</b>")
			_title = replaceMarkdownMarkersWithHtmlTags(_title, "~~", "<s>", "</s>")


			nextClause = document.clauses[i+1] if i+1 < len(document.clauses) else None