aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-06-26 19:33:36 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-06-26 19:33:36 +0200
commit4b981e88d8bb35aef38db09a1f7da6f5e4758d9d (patch)
tree8418013a9226aef5c300cdc3f14e77eb8e79c670
parentAdd clear notice in output about non-implemented features. (diff)
downloadmuppet-strings-4b981e88d8bb35aef38db09a1f7da6f5e4758d9d.tar.gz
muppet-strings-4b981e88d8bb35aef38db09a1f7da6f5e4758d9d.tar.xz
Add breadcrumbs.
-rw-r--r--muppet/breadcrumbs.py59
-rw-r--r--muppet/output.py10
-rw-r--r--static/style.css14
-rw-r--r--templates/base.html9
4 files changed, 91 insertions, 1 deletions
diff --git a/muppet/breadcrumbs.py b/muppet/breadcrumbs.py
new file mode 100644
index 0000000..ae88d3c
--- /dev/null
+++ b/muppet/breadcrumbs.py
@@ -0,0 +1,59 @@
+"""
+Page breadcrumbs.
+
+Page breadcrumbs are the "how did we get here" at the top of web pages.
+
+For example, the page
+``/modules/example-module/example-class`` would have a breadcrumb list like
+
+- ``/modules`` - Modules
+- ``/modules/example-module`` - Example Module
+- ``/modules/example-module/example-class`` Example Class
+"""
+
+import re
+from dataclasses import dataclass
+from typing import (
+ Tuple,
+)
+
+
+@dataclass
+class Breadcrumb:
+ """
+ A breadcrumb entry.
+
+ :param ref:
+ A url path. Meaning that it should contain all parents.
+ :param text:
+ The displayed text for this entry.
+ """
+
+ ref: str
+ text: str
+
+
+def breadcrumbs(*items: str | Tuple[str, str]) -> list[Breadcrumb]:
+ """
+ Generate a breadcrumb trail.
+
+ :param items:
+ The parts of the trace.
+
+ Each item should either be a single string, which is then used
+ as both the name, and the url component, or a tuple, in which
+ case the left value will be the displaed string, and the right
+ value the url component.
+ """
+ url = '/'
+ result = []
+ for item in items:
+ if isinstance(item, str):
+ url += item + '/'
+ text = item
+ else:
+ url += item[1] + '/'
+ text = item[0]
+ url = re.sub('/+', '/', url)
+ result.append(Breadcrumb(ref=url, text=text))
+ return result
diff --git a/muppet/output.py b/muppet/output.py
index 0aae7ed..676bbb4 100644
--- a/muppet/output.py
+++ b/muppet/output.py
@@ -29,6 +29,7 @@ from collections.abc import (
)
from .util import group_by
from .puppet.strings import isprivate
+from .breadcrumbs import breadcrumbs
# TODO replace 'output' with base, or put this somewhere else
@@ -301,8 +302,15 @@ def setup_module(base: str, module: ModuleEntry, *, path_base: str) -> None:
with open(os.path.join(dir, 'index.html'), 'w') as f:
template = jinja.get_template('code_page.html')
+ crumbs = breadcrumbs(
+ ('Environment', ''),
+ module.name,
+ (puppet_class['name'],
+ 'manifests/' + '/'.join(puppet_class['name'].split('::')[1:])),
+ )
f.write(template.render(content=format_class(puppet_class),
- path_base=path_base))
+ path_base=path_base,
+ breadcrumbs=crumbs))
# puppet_class['file']
# puppet_class['line']
diff --git a/static/style.css b/static/style.css
index fb19678..7ab8ae0 100644
--- a/static/style.css
+++ b/static/style.css
@@ -80,3 +80,17 @@ code.json {
.comment p:last-child {
margin-bottom: 0;
}
+
+.breadcrumb {
+ padding: 0;
+}
+
+.breadcrumb li {
+ display: inline-block;
+ padding: 0;
+}
+
+.breadcrumb li:not(:first-child)::before {
+ content: "»";
+ padding: 1ex;
+}
diff --git a/templates/base.html b/templates/base.html
index c435175..abc40f1 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -20,6 +20,15 @@
</noscript>
</head>
<body>
+ <header>
+ {% if breadcrumbs %}
+ <ul class="breadcrumb">
+ {%- for item in breadcrumbs -%}
+ <li><a href="{{ path_base }}{{ item.ref }}">{{ item.text }}</a></li>
+ {%- endfor -%}
+ </ul>
+ {% endif %}
+ </header>
{% block content %}
{% endblock %}
</body>