From 8b2d051b3d248a4acea0a70543c848da8b2ed4bf Mon Sep 17 00:00:00 2001 From: ankraft <an.kraft@gmail.com> Date: Fri, 16 Feb 2024 17:28:14 +0100 Subject: [PATCH] Made <br> replacement a bit more flexible. Also, ignoring code blocks --- generateChangemarks/pandocFilter.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/generateChangemarks/pandocFilter.py b/generateChangemarks/pandocFilter.py index fdc00c9..75e78be 100644 --- a/generateChangemarks/pandocFilter.py +++ b/generateChangemarks/pandocFilter.py @@ -132,14 +132,23 @@ def replaceLineBreaks(progress: Progress, mdLines: list[str]) -> list[str]: """ _taskID = progress.add_task('[blue]Replacing linebreaks', start=False, total=0) # progress.update() - linebreaksregex = re.compile('<br />') + linebreaksregex = re.compile('<br\s*/?\s*>') _lines: list[str] = [] + _inCodeBlock = False for line in mdLines: + # Check if we are in a code block + if line.strip().startswith('```'): + _inCodeBlock = not _inCodeBlock + if _inCodeBlock: + _lines.append(line) + continue + + # Replace linebreaks matches = re.findall(linebreaksregex, line) if matches: # Replace the linebreak with "\(newline)" - _lines.append(re.sub(r'<br />', f'\\\n', line)) + _lines.append(re.sub(linebreaksregex, f'\\\n', line)) else: _lines.append(line) -- GitLab