Skip to content
Snippets Groups Projects
Commit 4b981e88 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Add breadcrumbs.

parent 8b0f2f68
Branches
No related tags found
No related merge requests found
"""
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
......@@ -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']
......
......@@ -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;
}
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment