From 0900f2910e908ff8c997b684c2f7ae69791f50de Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 1 Jun 2022 16:41:21 +0200 Subject: Hierarchical navigation menu --- templates/_nav.html | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 56 +++++++++++++++------------------------------------- 2 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 templates/_nav.html diff --git a/templates/_nav.html b/templates/_nav.html new file mode 100644 index 0000000..92ed4c5 --- /dev/null +++ b/templates/_nav.html @@ -0,0 +1,53 @@ +{% macro navsection(hierarchy, level, current) %} + {% set sec = hierarchy | nth(n=level) %} + {% set sec = get_section(path=sec) %} + {% set maxlevel = hierarchy | length %} + + {% if sec.subsections %} + {% for s in sec.subsections %} + {% set p = get_section(path=s) %} +
+ {% if p.path == current.path %} + {{ p.title }} + {% elif hierarchy is containing(s) %} + ⯆ {{ p.title }} + {% else %} + ⯈ {{ p.title }} + {% endif %} + {% if hierarchy is containing(s) or current.path == p.path %} + {% if level + 1 < maxlevel %} + {{ nav::navsection(hierarchy=hierarchy,level=level + 1,current=current) }} + {% endif %} + {% endif %} +
+ {% endfor %} + {% endif %} + + {% if sec.pages %} + {% for p in sec.pages %} +
+ {% if p.path == current.path %} +   {{ p.title }} + {% else %} +   {{ p.title }} + {% endif %} +
+ {% endfor %} + {% endif %} +{% endmacro navsection %} + +{% macro navmenu(current) %} + {% if current.ancestors %} + {% set hierarchy = current.ancestors | concat(with=current.relative_path) %} + {% else %} + {% set hierarchy = [current.relative_path] %} + {% endif %} + + {% set root_path = hierarchy | nth(n=0) %} + {% set root = get_section(path=root_path) %} +
+ {{ root.title }} +
+ + {{ nav::navsection(hierarchy=hierarchy,level=0,current=current) }} +{% endmacro %} diff --git a/templates/index.html b/templates/index.html index 54fb473..004a4fa 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,5 @@ {% import "_macros.html" as macros %} +{% import "_nav.html" as nav %} @@ -7,7 +8,7 @@ {% block title %}{{ config.title }}{% endblock title %} {% block favicon %} - + {% endblock favicon %} {% include "_variables.html" %} @@ -24,46 +25,21 @@ {% endblock header %}
- {% block toc %} - {% if page.ancestors %} - {% set section = get_section(path=page.ancestors | last) %} - {% endif %} - {% if section.ancestors %} - {% set parent = get_section(path=section.ancestors | last) %} - {% endif %} + {% block toc %}
-
- - {% if section.subsections %} - {% for s in section.subsections %} - {% set p = get_section(path=s) %} - - {% endfor %} - {% endif %} - - {% if section.pages %} - {% for p in section.pages %} - - {% endfor %} - {% endif %} - {% if parent %} - - {% endif %} -
+
+ {% if page %} + {{ nav::navmenu(current=page) }} + {% else %} + {{ nav::navmenu(current=section) }} + {% endif %} +
- {% endblock toc %} + {% endblock toc %}
{% block content %} -
{{ section.title }}
+
{{ section.title }}
{{ section.content | safe }} {% endblock content %}
@@ -75,10 +51,10 @@ {% block footer %} {% endblock footer %} -- cgit v1.2.3 From d99afca9ea2cecee09e980264f9a75c1918a593a Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 1 Jun 2022 16:55:07 +0200 Subject: No arrows for empty sections --- templates/_nav.html | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/templates/_nav.html b/templates/_nav.html index 92ed4c5..3eb6900 100644 --- a/templates/_nav.html +++ b/templates/_nav.html @@ -7,16 +7,24 @@ {% for s in sec.subsections %} {% set p = get_section(path=s) %}
- {% if p.path == current.path %} - {{ p.title }} - {% elif hierarchy is containing(s) %} - ⯆ {{ p.title }} + {% if p.subsections or p.pages %} + {% if p.path == current.path %} + {{ p.title }} + {% elif hierarchy is containing(s) %} + ⯆ {{ p.title }} + {% else %} + ⯈ {{ p.title }} + {% endif %} + {% if hierarchy is containing(s) or current.path == p.path %} + {% if level + 1 < maxlevel %} + {{ nav::navsection(hierarchy=hierarchy,level=level + 1,current=current) }} + {% endif %} + {% endif %} {% else %} - ⯈ {{ p.title }} - {% endif %} - {% if hierarchy is containing(s) or current.path == p.path %} - {% if level + 1 < maxlevel %} - {{ nav::navsection(hierarchy=hierarchy,level=level + 1,current=current) }} + {% if p.path == current.path %} + {{ p.title }} + {% else %} + {{ p.title }} {% endif %} {% endif %}
@@ -27,9 +35,9 @@ {% for p in sec.pages %}
{% if p.path == current.path %} -   {{ p.title }} + {{ p.title }} {% else %} -   {{ p.title }} + {{ p.title }} {% endif %}
{% endfor %} -- cgit v1.2.3 From 71980e93aecc7137017f769f8de746d9b2959b9d Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 1 Jun 2022 16:58:35 +0200 Subject: Simplify navigation a bit --- templates/_nav.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/_nav.html b/templates/_nav.html index 3eb6900..9b76c96 100644 --- a/templates/_nav.html +++ b/templates/_nav.html @@ -46,7 +46,7 @@ {% macro navmenu(current) %} {% if current.ancestors %} - {% set hierarchy = current.ancestors | concat(with=current.relative_path) %} + {% set hierarchy = current.ancestors | slice(start=1) | concat(with=current.relative_path) %} {% else %} {% set hierarchy = [current.relative_path] %} {% endif %} -- cgit v1.2.3