From d39cf35c001f3601c77dba7bcd4354e03c545ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se> Date: Mon, 29 May 2023 15:39:28 +0200 Subject: [PATCH] Fix type aliases. --- muppet/__main__.py | 59 ++++++++++++++++++++++++++++++++++------------ muppet/format.py | 5 ++++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/muppet/__main__.py b/muppet/__main__.py index b35d5f0..48832b0 100644 --- a/muppet/__main__.py +++ b/muppet/__main__.py @@ -26,7 +26,10 @@ from collections.abc import ( from .cache import Cache from .puppet.strings import puppet_strings -from .format import format_class +from .format import ( + format_class, + format_type_alias, +) jinja = Environment( loader=FileSystemLoader('templates'), @@ -255,11 +258,32 @@ def defined_types_index(defined_list: list) -> IndexCategory: } -# def type_aliases_index(alias_list: list) -> IndexCategory: -# """Prepare type alias index list.""" -# # TODO -# -# +def type_aliases_index(alias_list: list) -> IndexCategory: + """Prepare type alias index list.""" + groups = group_by(isprivate, alias_list) + lst: list[IndexSubcategory] = [] + if publics := groups.get(False): + lst.append({ + 'title': 'Public Type Aliases', + 'list': ({'name': i['name'], + 'file': os.path.splitext(i['file'])[0]} + for i in publics), + }) + + if privates := groups.get(True): + lst.append({ + 'title': 'Private Type Aliases', + 'list': ({'name': i['name'], + 'file': os.path.splitext(i['file'])[0]} + for i in privates), + }) + + return { + 'title': 'Type Aliases', + 'list': lst, + } + + # def resource_types_index(resource_list: list) -> IndexCategory: # """ # Prepare resource type index list. @@ -280,13 +304,7 @@ def setup_module_index(base: str, module: ModuleEntry, data: dict[str, Any]) -> data['data_types'] - data['data_type_aliases'] - - # content.append({ - # 'title': 'Type Aliases', - # 'list': [ - # ] - # }) + content.append(type_aliases_index(data['data_type_aliases'])) content.append(defined_types_index(data['defined_types'])) @@ -318,7 +336,6 @@ def setup_module(base: str, module: ModuleEntry) -> None: for puppet_class in data['puppet_classes'] + data['defined_types']: # localpath = puppet_class['name'].split('::') localpath, _ = os.path.splitext(puppet_class['file']) - # localdir, _ = os.path.splitext(puppet_class['name']) dir = os.path.join(path, localpath) pathlib.Path(dir).mkdir(parents=True, exist_ok=True) # puppet_class['docstring'] @@ -343,7 +360,19 @@ def setup_module(base: str, module: ModuleEntry) -> None: # puppet_class['line'] for type_alias in data['data_type_aliases']: - ... + localpath, _ = os.path.splitext(type_alias['file']) + dir = os.path.join(path, localpath) + pathlib.Path(dir).mkdir(parents=True, exist_ok=True) + + with open(os.path.join(dir, 'source.pp.txt'), 'w') as f: + f.write(type_alias['alias_of']) + + with open(os.path.join(dir, 'source.json'), 'w') as f: + json.dump(type_alias, f, indent=2) + + template = jinja.get_template('code_page.html') + with open(os.path.join(dir, 'index.html'), 'w') as f: + f.write(template.render(content=format_type_alias(type_alias))) os.system("cp -r static output") diff --git a/muppet/format.py b/muppet/format.py index 1b190b2..4065136 100644 --- a/muppet/format.py +++ b/muppet/format.py @@ -175,6 +175,11 @@ def handle_case_body(forms: list[dict[str, Any]], return tag(ret) +# Hyperlinks for +# - qn +# - qr +# - var (except when it's the var declaration) + def parse(form: Any, indent: int, context: list[str]) -> Tag: """ Print everything from a puppet parse tree. -- GitLab