lol

nixos/make-option-docs: wrap md parser+renderer into Converter

the new rendering tool will consist of a number of different Converters,
each with a possibly specialized Renderer.

pennae ba1f5331 3e45994a

+24 -14
+1
nixos/lib/make-options-doc/default.nix
··· 177 177 in [ 178 178 markdown-it-py 179 179 mdit-py-plugins 180 + p.frozendict 180 181 ])) 181 182 ]; 182 183 } ''
+23 -14
nixos/lib/make-options-doc/optionsToDocbook.py
··· 2 2 import json 3 3 import os 4 4 import sys 5 + from collections.abc import MutableMapping, Sequence 5 6 from typing import Any, Dict, List 6 - from collections.abc import MutableMapping, Sequence 7 + from frozendict import frozendict 7 8 8 9 # for MD conversion 9 10 import markdown_it ··· 14 15 from mdit_py_plugins.deflist import deflist_plugin 15 16 from mdit_py_plugins.myst_role import myst_role_plugin 16 17 from xml.sax.saxutils import escape, quoteattr 17 - 18 - manpage_urls = json.load(open(os.getenv('MANPAGE_URLS'))) 19 18 20 19 class Renderer(markdown_it.renderer.RendererProtocol): 21 20 __output__ = "docbook" ··· 187 186 title = f"<refentrytitle>{escape(page)}</refentrytitle>" 188 187 vol = f"<manvolnum>{escape(section)}</manvolnum>" 189 188 ref = f"<citerefentry>{title}{vol}</citerefentry>" 190 - if man in manpage_urls: 191 - return f"<link xlink:href={quoteattr(manpage_urls[man])}>{ref}</link>" 189 + if man in env['manpage_urls']: 190 + return f"<link xlink:href={quoteattr(env['manpage_urls'][man])}>{ref}</link>" 192 191 else: 193 192 return ref 194 193 raise NotImplementedError("md node not supported yet", token) 195 194 196 - md = ( 197 - markdown_it.MarkdownIt(renderer_cls=Renderer) 198 - # TODO maybe fork the plugin and have only a single rule for all? 199 - .use(container_plugin, name="{.note}") 200 - .use(container_plugin, name="{.important}") 201 - .use(container_plugin, name="{.warning}") 202 - .use(deflist_plugin) 203 - .use(myst_role_plugin) 204 - ) 195 + class Converter: 196 + def __init__(self, manpage_urls: Dict[str, str]): 197 + self._md = markdown_it.MarkdownIt(renderer_cls=Renderer) 198 + # TODO maybe fork the plugin and have only a single rule for all? 199 + self._md.use(container_plugin, name="{.note}") 200 + self._md.use(container_plugin, name="{.important}") 201 + self._md.use(container_plugin, name="{.warning}") 202 + self._md.use(deflist_plugin) 203 + self._md.use(myst_role_plugin) 204 + 205 + self._manpage_urls = frozendict(manpage_urls) 206 + 207 + def render(self, src: str) -> str: 208 + env = { 209 + 'manpage_urls': self._manpage_urls 210 + } 211 + return self._md.render(src, env) 212 + 213 + md = Converter(json.load(open(os.getenv('MANPAGE_URLS')))) 205 214 206 215 # converts in-place! 207 216 def convertMD(options: Dict[str, Any]) -> str: