Skip to content

Make redown handle code block options #3034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions source/_extensions/redown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand Down