aboutsummaryrefslogtreecommitdiff
path: root/templates/_nav.html
blob: 0ab8ac7e38fe1727f21788a7619ba93e9b5b5106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{# (Public) Entrypoint to be used by zola "pages" #}
{% macro page(target) %}
  {% set root = get_section(path=target.ancestors | last) %}
  {{ nav::inner_nav(root=root, current=target) }}
{% endmacro %}

{# (Public) Entrypoint to be used by zola "sections" #}
{% macro section(target) %}
  {{ nav::inner_nav(root=target, current=target) }}
{% endmacro %}

{# -------------------------- #}
{# -------------------------- #}

{# (Private) Shared+root logic to build the menu #}
{% macro inner_nav(root, current) %}
  {{ nav::hamburger(root=root) }}
  
  {# Section title #}
  <div class="toc-item toc-section">
    <a class="subtext" href="{{root.permalink | safe}}">{{ root.title }}</a>
  </div>

  {# Choose between "tree" (has extra.parent) and "list" (default) collections #}
  {% set root_tree = root.pages | group_by(attribute="extra.parent") %}
  {% if root.relative_path in root_tree %}
    {% set tree_breadcrumb = nav::breadcrumb(corpus=root.pages, root=root.relative_path, target=current)|split(pat=":")|slice(start=1) %}
    {{ nav::tree(
         tree=root_tree, 
         cursor=root.relative_path, 
         selected=current, 
         crumb=tree_breadcrumb) }}
  {% else %}
    {{ nav::list(list=root.pages, selected=current) }}
  {% endif %}   
{% endmacro %}

{# (Private) On small screens, like a smartphone, hide the menu behind an hamburger icon #}
{% macro hamburger(root) %}
  <input id="menu-toggle" type="checkbox" />
  <label class='menu-button-container' for="menu-toggle">
    <div class="menu-button"></div>
    <div class="toc-item toc-menu-title subtext">{{ root.title }}</div>
  </label>
{% endmacro %}

{# (Private) Build a breadcrumb for the page #}
{# It's ugly because this is the hacky part of the project #}
{% macro breadcrumb(corpus, root, target) %}{% if 'parent' in target.extra and target.extra.parent != root %}{% set new_target = get_page(path=target.extra.parent) %}{{ nav::breadcrumb(corpus=corpus, root=root, target=new_target) }}:{{ new_target.relative_path }}{% endif %}{% endmacro %}

{# (Private) Render a list menu (this is the simple fallback when extra.parent is not defined #}
{% macro list(list, selected) %}
  {% for page in list %}
    {% set is_selected = page.relative_path == selected.relative_path %}
    <div class="toc-item">
      {{ nav::link(page=page, is_selected=is_selected, is_parent=false) }}
    </div>
  {% endfor %}
{% endmacro %}

{# (Private) Tree menu rendering; this function is recursive #}
{# this function takes a breadcrumb to know which part of the menu must be unfolded #}
{% macro tree(tree, cursor, selected, crumb) %}
  {% for page in tree | get(key=cursor) %}
  {% set is_selected = page.relative_path == selected.relative_path %}
  <div class="toc-item">

    {# Link with a (possible) subsection #}
    {% if page.relative_path in tree %}
      {# Display link as a section #}
      {{ nav::link(page=page, is_selected=is_selected, is_parent=true) }} 

      {# Should we unroll this part of the tree? #}
      {% if page.relative_path in crumb or is_selected %}
        <div class="nav-subsection">
          {# do the recursive call #}
          {{ nav::tree(tree=tree, cursor=page.relative_path, selected=selected, crumb=crumb) }}
        </div>
      {% endif %}

    {# Simple link, ie. a leaf of the tree #}
    {% else %}
      {{ nav::link(page=page, is_selected=is_selected, is_parent=false) }}
    {% endif %}
  </div>
  {% endfor %}
{% endmacro %}

{# (Private) Render a single link #}
{% macro link(page, is_selected, is_parent) %}
  <a class="subtext" href="{{page.permalink | safe}}">
      {% if is_selected %}<b>{% endif %}
      {% if is_parent %}{% endif %}
      {{ page.title }}
      {% if is_selected %}</b>{% endif %}
  </a>
{% endmacro %}