mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-28 18:03:04 +00:00
nixos-render-docs: move escaping functions to new modules
these modules will be extended with more functionality. md will ultimately contain MD-related code such as parsers and converter base classes. docbook will contain renderers.
This commit is contained in:
parent
be6a25368f
commit
986e48ca22
@ -16,6 +16,9 @@ from mdit_py_plugins.deflist import deflist_plugin
|
|||||||
from mdit_py_plugins.myst_role import myst_role_plugin
|
from mdit_py_plugins.myst_role import myst_role_plugin
|
||||||
from xml.sax.saxutils import escape, quoteattr
|
from xml.sax.saxutils import escape, quoteattr
|
||||||
|
|
||||||
|
from .docbook import make_xml_id
|
||||||
|
from .md import md_escape
|
||||||
|
|
||||||
class Renderer(markdown_it.renderer.RendererProtocol):
|
class Renderer(markdown_it.renderer.RendererProtocol):
|
||||||
__output__ = "docbook"
|
__output__ = "docbook"
|
||||||
def __init__(self, parser=None):
|
def __init__(self, parser=None):
|
||||||
@ -276,30 +279,6 @@ def convertMD(options: Dict[str, Any]) -> str:
|
|||||||
|
|
||||||
return options
|
return options
|
||||||
|
|
||||||
id_translate_table = {
|
|
||||||
ord('*'): ord('_'),
|
|
||||||
ord('<'): ord('_'),
|
|
||||||
ord(' '): ord('_'),
|
|
||||||
ord('>'): ord('_'),
|
|
||||||
ord('['): ord('_'),
|
|
||||||
ord(']'): ord('_'),
|
|
||||||
ord(':'): ord('_'),
|
|
||||||
ord('"'): ord('_'),
|
|
||||||
}
|
|
||||||
|
|
||||||
md_escape_table = {
|
|
||||||
ord('*'): '\\*',
|
|
||||||
ord('<'): '\\<',
|
|
||||||
ord('['): '\\[',
|
|
||||||
ord('`'): '\\`',
|
|
||||||
ord('.'): '\\.',
|
|
||||||
ord('#'): '\\#',
|
|
||||||
ord('&'): '\\&',
|
|
||||||
ord('\\'): '\\\\',
|
|
||||||
}
|
|
||||||
def md_escape(s: str) -> str:
|
|
||||||
return s.translate(md_escape_table)
|
|
||||||
|
|
||||||
def need_env(n):
|
def need_env(n):
|
||||||
if n not in os.environ:
|
if n not in os.environ:
|
||||||
raise RuntimeError("required environment variable not set", n)
|
raise RuntimeError("required environment variable not set", n)
|
||||||
@ -369,7 +348,7 @@ def main():
|
|||||||
|
|
||||||
for name in keys:
|
for name in keys:
|
||||||
opt = options[name]
|
opt = options[name]
|
||||||
id = OTD_OPTION_ID_PREFIX + name.translate(id_translate_table)
|
id = OTD_OPTION_ID_PREFIX + make_xml_id(name)
|
||||||
print(f"""<varlistentry>""")
|
print(f"""<varlistentry>""")
|
||||||
# NOTE adding extra spaces here introduces spaces into xref link expansions
|
# NOTE adding extra spaces here introduces spaces into xref link expansions
|
||||||
print(f"""<term xlink:href={quoteattr("#" + id)} xml:id={quoteattr(id)}>""", end='')
|
print(f"""<term xlink:href={quoteattr("#" + id)} xml:id={quoteattr(id)}>""", end='')
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
_xml_id_translate_table = {
|
||||||
|
ord('*'): ord('_'),
|
||||||
|
ord('<'): ord('_'),
|
||||||
|
ord(' '): ord('_'),
|
||||||
|
ord('>'): ord('_'),
|
||||||
|
ord('['): ord('_'),
|
||||||
|
ord(']'): ord('_'),
|
||||||
|
ord(':'): ord('_'),
|
||||||
|
ord('"'): ord('_'),
|
||||||
|
}
|
||||||
|
def make_xml_id(s: str) -> str:
|
||||||
|
return s.translate(_xml_id_translate_table)
|
12
pkgs/tools/nix/nixos-render-docs/src/nixos_render_docs/md.py
Normal file
12
pkgs/tools/nix/nixos-render-docs/src/nixos_render_docs/md.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
_md_escape_table = {
|
||||||
|
ord('*'): '\\*',
|
||||||
|
ord('<'): '\\<',
|
||||||
|
ord('['): '\\[',
|
||||||
|
ord('`'): '\\`',
|
||||||
|
ord('.'): '\\.',
|
||||||
|
ord('#'): '\\#',
|
||||||
|
ord('&'): '\\&',
|
||||||
|
ord('\\'): '\\\\',
|
||||||
|
}
|
||||||
|
def md_escape(s: str) -> str:
|
||||||
|
return s.translate(_md_escape_table)
|
Loading…
Reference in New Issue
Block a user