From afca515e2c2db240b4d354a0ae665479894ef89b Mon Sep 17 00:00:00 2001 From: Damien Martin Date: Mon, 26 Jul 2021 18:47:44 +0200 Subject: [PATCH] docs: make mkdocs_hooks.transform more robust MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Keeps the markdown inside the code blocks as is - Logs the modified lines (mkdocs serve —verbose) Signed-off-by: Damien Martin --- docs/_klipper3d/mkdocs_hooks.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/docs/_klipper3d/mkdocs_hooks.py b/docs/_klipper3d/mkdocs_hooks.py index 5b122a1d..8815c776 100644 --- a/docs/_klipper3d/mkdocs_hooks.py +++ b/docs/_klipper3d/mkdocs_hooks.py @@ -1,18 +1,31 @@ import re +import logging + +logger = logging.getLogger('mkdocs.mkdocs_hooks.transform') def transform(markdown: str, page, config, files): + in_code_block = 0 in_list = False lines = markdown.splitlines() for i in range(len(lines)): - lines[i] = lines[i].replace('](../', - f"]({config['repo_url']}blob/master/") - lines[i] = re.sub(r"\\", "
", lines[i]) - # check that lists at level 0 are not indented (no space before *|-|1.) - if len(lines[i]) == 0: - in_list = False - elif re.match(r"^(\*|-|\d+\.) ", lines[i]): - in_list = True - if not in_list: - lines[i] = re.sub(r"^\s+(\*|-|\d+\.) ", r"\1 ", lines[i]) + line_out = lines[i] + in_code_block = (in_code_block + + len(re.findall("\s*[`]{3,}", line_out))) % 2 + if not in_code_block: + line_out = line_out.replace('](../', + f"]({config['repo_url']}blob/master/") + line_out = re.sub("\\\s*$", "
", line_out) + # check that lists at level 0 are not indented + # (no space before *|-|1.) + if len(line_out) == 0: + in_list = False + elif re.match(r"^(\*|-|\d+\.) ", line_out): + in_list = True + if not in_list: + line_out = re.sub(r"^\s+(\*|-|\d+\.) ", r"\1 ", line_out) + if line_out != lines[i]: + logger.debug((f'[mkdocs_hooks] rewrite line {i+1}: ' + f'"{lines[i]}" -> "{line_out}"')) + lines[i] = line_out output = "\n".join(lines) return output