From e62d89d3c33242c6ba14975c07b07c523e17e539 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Sun, 4 Jun 2023 01:40:30 +0200
Subject: [PATCH] Add summary to main index page.

---
 muppet/__main__.py   | 26 +++++++++++++++++++-------
 templates/index.html |  8 +++++++-
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/muppet/__main__.py b/muppet/__main__.py
index d2880ca..88d6968 100644
--- a/muppet/__main__.py
+++ b/muppet/__main__.py
@@ -67,6 +67,7 @@ class ModuleEntry:
     name: str
     path: str
     strings_output: bytes
+    metadata: dict[str, Any]
 
     def file(self, path: str) -> str:
         """Return the absolute path of a path inside the module."""
@@ -125,7 +126,13 @@ def get_modules(dir: str) -> list[ModuleEntry]:
         path = os.path.join(env, entry)
         strings_data = get_puppet_strings(path)
 
-        modules.append(ModuleEntry(name, path, strings_data))
+        try:
+            with open(os.path.join(path, 'metadata.json')) as f:
+                metadata = json.load(f)
+        except FileNotFoundError:
+            metadata = {}
+
+        modules.append(ModuleEntry(name, path, strings_data, metadata))
 
     return modules
 
@@ -171,7 +178,7 @@ def isprivate(entry: dict[str, Any]) -> bool:
     return False
 
 
-def setup_index(base: str) -> None:
+def setup_index(base: str, modules: list[ModuleEntry]) -> None:
     """Create the main index.html file."""
     template = jinja.get_template('index.html')
     with open(os.path.join(base, 'index.html'), 'w') as f:
@@ -403,9 +410,14 @@ def setup_module(base: str, module: ModuleEntry) -> None:
     # data['resource_types']
 
 
-modules = get_modules(env)
+def __main() -> None:
+    modules = get_modules(env)
+    setup_index('output', modules)
+
+    for module in modules:
+        # print(module)
+        setup_module('output', module)
+
 
-setup_index('output')
-for module in modules:
-    # print(module)
-    setup_module('output', module)
+if __name__ == '__main__':
+    __main()
diff --git a/templates/index.html b/templates/index.html
index 693c6f3..8703117 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -17,9 +17,15 @@
 	<ul>
 		{% for module in modules %}
 		<li>
-			<a href="/{{ module.name }}"
+			{%- if module.metadata.get('name') -%}
+				{{ module.metadata['name'].split('-')[0] }}/
+			{%- endif -%}
+			<a href="{{ module.name }}"
 			   class="{{ 'error' if module.strings_output == None }}"
 			   >{{ module.name }}</a>
+			{%- if module.metadata.get('summary') %}
+				&mdash; {{ module.metadata['summary'] }}
+			{%- endif -%}
 		</li>
 		{% endfor %}
 	</ul>
-- 
GitLab