Commit 26eccfc0 authored by Naum Spaseski's avatar Naum Spaseski
Browse files

Added function to cleanup excess newlines in markdown

parent 2db3d159
Loading
Loading
Loading
Loading
+62 −0
Original line number Diff line number Diff line
@@ -1083,6 +1083,68 @@ def processDocuments(documents:list[str],
					frontmatterLines = docConfig.frontmatter.split('\n')
					lines = frontmatterLines + [''] + lines
			
			#
			#	Clean up redundant spaces and normalize blank lines
			#
			def cleanupMarkdown(lines: list[str]) -> list[str]:
				"""Remove trailing spaces and normalize blank lines.
				Figure captions should have exactly one blank line before and after."""
				# First pass: remove trailing spaces
				lines = [line.rstrip() for line in lines]
				
				# Second pass: collapse multiple consecutive blank lines and handle figure captions
				result: list[str] = []
				blank_count = 0
				
				for i, line in enumerate(lines):
					isBlank = (len(line) == 0)
					isFigureCaption = line.startswith('**Figure') and line.endswith('**')
					
					# Check previous and next lines for context
					prevLine = result[-1] if len(result) > 0 else ''
					prevIsBlank = (len(prevLine) == 0)
					prevIsFigureCaption = (prevLine.startswith('**Figure') and prevLine.endswith('**'))
					
					nextLine = lines[i + 1] if i + 1 < len(lines) else ''
					nextIsBlank = (len(nextLine) == 0)
					nextIsFigureCaption = (nextLine.startswith('**Figure') and nextLine.endswith('**'))
					
					if isBlank:
						# Handle blank lines
						if prevIsFigureCaption:
							# After figure caption: we already added one blank line, skip this one
							continue
						elif nextIsFigureCaption:
							# Before figure caption: add exactly one blank line
							if not prevIsBlank:
								result.append('')
								blank_count = 1
						else:
							# Regular blank line: allow max 2 consecutive blank lines
							blank_count += 1
							if blank_count <= 2:
								result.append('')
							# Skip if more than 2 consecutive blank lines
					else:
						# Non-blank line
						blank_count = 0
						
						if isFigureCaption:
							# Figure caption: ensure exactly one blank line before and after
							if not prevIsBlank and len(result) > 0:
								result.append('')
							result.append(line)
							# Add blank line after if next line is not blank
							if not nextIsBlank:
								result.append('')
						else:
							# Regular line
							result.append(line)
				
				return result
			
			lines = cleanupMarkdown(lines)
			
			#
			#	Write produced Markdown file
			#