diff --git a/source/_extensions/redown.py b/source/_extensions/redown.py index de40c94a51..486ecc386a 100644 --- a/source/_extensions/redown.py +++ b/source/_extensions/redown.py @@ -14,6 +14,7 @@ from pathlib import Path from sphinx.application import Sphinx from dataclasses import dataclass +from sphinx.directives.code import CodeBlock LINK_CORE = r""" @@ -67,13 +68,30 @@ def replace(match: re.Match) -> str: ret = "" ret += start - ret += btindent + f".. code-block:: {lang}\n\n" + ret += btindent + f".. code-block:: {lang}\n" cindent = 3 * " " + firstCode = False for line in code.splitlines(keepends=True): - if line.strip() == "": + if line.strip().startswith(":") and not firstCode: + colon_start = line.find(":") + colon_end = line.rfind(":") + if colon_start != -1 and colon_end != -1 and colon_start < colon_end: + option = line[colon_start + 1 : colon_end].strip() + if option in CodeBlock.option_spec: + ret += cindent + line + else: + ret += "\n" + cindent + line + firstCode = True + elif line.strip() == "": + if not firstCode: + firstCode = True + ret += "\n" ret += "\n" else: + if not firstCode: + firstCode = True + ret += "\n" ret += cindent + line return ret