nixos-render-docs: move some options helpers to new module

This commit is contained in:
pennae 2023-01-25 20:55:27 +01:00
parent c63a550e7b
commit aa3fd2865b
3 changed files with 21 additions and 10 deletions

View File

@ -18,6 +18,7 @@ from xml.sax.saxutils import escape, quoteattr
from .docbook import make_xml_id, DocBookRenderer
from .md import md_escape
from .options import option_is
class Converter:
def __init__(self, manpage_urls: Dict[str, str]):
@ -47,16 +48,10 @@ md = Converter(json.load(open(os.getenv('MANPAGE_URLS'))))
# converts in-place!
def convertMD(options: Dict[str, Any]) -> str:
def optionIs(option: Dict[str, Any], key: str, typ: str) -> bool:
if key not in option: return False
if type(option[key]) != dict: return False
if '_type' not in option[key]: return False
return option[key]['_type'] == typ
def convertCode(name: str, option: Dict[str, Any], key: str):
if optionIs(option, key, 'literalMD'):
if option_is(option, key, 'literalMD'):
option[key] = md.render(f"*{key.capitalize()}:*\n{option[key]['text']}")
elif optionIs(option, key, 'literalExpression'):
elif option_is(option, key, 'literalExpression'):
code = option[key]['text']
# for multi-line code blocks we only have to count ` runs at the beginning
# of a line, but this is much easier.
@ -70,14 +65,14 @@ def convertMD(options: Dict[str, Any]) -> str:
ticks, sep = ('`' * (longest + (3 if multiline else 1)), '\n' if multiline else ' ')
code = f"{ticks}{sep}{code}{sep}{ticks}"
option[key] = md.render(f"*{key.capitalize()}:*\n{code}")
elif optionIs(option, key, 'literalDocBook'):
elif option_is(option, key, 'literalDocBook'):
option[key] = f"<para><emphasis>{key.capitalize()}:</emphasis> {option[key]['text']}</para>"
elif key in option:
raise Exception(f"{name} {key} has unrecognized type", option[key])
for (name, option) in options.items():
try:
if optionIs(option, 'description', 'mdDoc'):
if option_is(option, 'description', 'mdDoc'):
option['description'] = md.render(option['description']['text'])
elif markdownByDefault:
option['description'] = md.render(option['description'])

View File

@ -0,0 +1,12 @@
from typing import Optional
from .types import Option
def option_is(option: Option, key: str, typ: str) -> Optional[dict[str, str]]:
if key not in option:
return None
if type(option[key]) != dict:
return None
if option[key].get('_type') != typ: # type: ignore[union-attr]
return None
return option[key] # type: ignore[return-value]

View File

@ -0,0 +1,4 @@
from typing import Optional, Tuple
OptionLoc = str | dict[str, str]
Option = dict[str, str | dict[str, str] | list[OptionLoc]]