diff --git a/main.py b/main.py index 80b40caf6fbc5b5233d5d9959f9b3fee3cb40a16..8b244658bb445c028d2cca45bc56c3b58730ce7a 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,11 @@ +""" +Pretty print a complete puppet documentation. + +An `output.json`, as per produced by `./merge-json.py` should be +provided as the first element. This program goes through every +definition in it, and outputs a complete index.html. +""" + from cache import parse_puppet from commonmark import commonmark from reflow import traverse @@ -22,6 +30,7 @@ param_doc: dict[str, str] = {} def print_hash(hash, indent=0): + """Print the contents of a puppet hash literal.""" if not hash: return # namelen = 0 @@ -42,7 +51,8 @@ def print_hash(hash, indent=0): print(',') -def ops_namelen(ops): +def ops_namelen(ops) -> int: + """Calculate max key length a list of puppet operators.""" namelen = 0 for item in ops: match item: @@ -58,6 +68,7 @@ def ops_namelen(ops): def print_array(arr, indent=0): + """Print a puppet array literal.""" if not arr: print('[]', end='') return @@ -70,6 +81,15 @@ def print_array(arr, indent=0): def print_var(x, dollar=True): + """ + Print the given variable. + + If documentation exists, then add that documentation as hoover text. + :param: x + The variable to print + :param: dollar + If there should be a dollar prefix. + """ dol = '$' if dollar else '' if doc := param_doc.get(x): print(f'<span class="var">{dol}{x}<div class="documentation">{doc}</div></span>', @@ -99,6 +119,15 @@ symbols: dict[str, str] = { def parse(form, indent=0): + """ + Print everything from a puppet parse tree. + + :param: from + A puppet AST. + :param: indent + How many levels deep in indentation the code is. + Will get multiplied by the indentation width. + """ # Sorted per `sort -V` match form: case None: @@ -713,6 +742,16 @@ def parse(form, indent=0): def print_docstring(docstring): + """ + Format docstrings as they appear in some puppet types. + + Those types being + - puppet_classes, + - puppet_type_aliases, and + - defined_types + """ + global param_doc + if 'tags' in docstring: param_doc = {tag['name']: tag.get('text') or '' for tag in docstring['tags']