nixos-render-docs: extend md_make_code

add the ability to set the info string for a newly created fenced code
block, and a flag to always emit a fenced block. the commonmark
converter will need this to faithfully recreate fenced and indented code
blocks.
This commit is contained in:
pennae 2023-02-10 07:22:53 +01:00 committed by pennae
parent 895d9e69dd
commit 45619b3c4a

View file

@ -27,10 +27,10 @@ _md_escape_table = {
def md_escape(s: str) -> str:
return s.translate(_md_escape_table)
def md_make_code(code: str) -> str:
def md_make_code(code: str, info: str = "", multiline: Optional[bool] = None) -> str:
# for multi-line code blocks we only have to count ` runs at the beginning
# of a line, but this is much easier.
multiline = '\n' in code
multiline = multiline or info != "" or '\n' in code
longest, current = (0, 0)
for c in code:
current = current + 1 if c == '`' else 0
@ -38,7 +38,7 @@ def md_make_code(code: str) -> str:
# inline literals need a space to separate ticks from content, code blocks
# need newlines. inline literals need one extra tick, code blocks need three.
ticks, sep = ('`' * (longest + (3 if multiline else 1)), '\n' if multiline else ' ')
return f"{ticks}{sep}{code}{sep}{ticks}"
return f"{ticks}{info}{sep}{code}{sep}{ticks}"
AttrBlockKind = Literal['admonition', 'example']