From 8ce00eb010c87d056a770990c7c61103749f21e1 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Fri, 24 Jul 2020 17:22:37 +0200
Subject: [PATCH] =?UTF-8?q?table=20des=20mati=C3=A8res?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

la table des matières est désormais déclarée directement avec [TOC]
---
 sass/duniter.sass      |  8 +++++--
 templates/macros.html  | 53 ++++++++++++++++++++++++++++++++++++++++--
 templates/nav.html     | 12 +++++-----
 templates/page.html    |  8 +------
 templates/section.html | 13 +++++------
 templates/toc.html     | 30 ------------------------
 6 files changed, 70 insertions(+), 54 deletions(-)
 delete mode 100644 templates/toc.html

diff --git a/sass/duniter.sass b/sass/duniter.sass
index 6301428..c89f058 100644
--- a/sass/duniter.sass
+++ b/sass/duniter.sass
@@ -38,7 +38,7 @@ a:hover
 
 .toc 
     background: #f7f7f7
-    padding: 0 2em 2em 2em
+    padding: 0 2em 0em 2em
     border-left: 5px solid #41B3FF
     display: inline-block
     ol.h2
@@ -48,4 +48,8 @@ a:hover
             ol.h4
                 list-style-type: lower-alpha
                 ol.h5
-                    list-style-type: circle
\ No newline at end of file
+                    list-style-type: circle
+
+img.icon
+    height: 1em
+    margin-bottom: 0.2em
\ No newline at end of file
diff --git a/templates/macros.html b/templates/macros.html
index c471ff5..de6e4f6 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -1,4 +1,6 @@
-{# === KATEX ===#}
+{# ============= #}
+{# === KATEX === #}
+{# ============= #}
 {% macro katex() %}
 <link rel="stylesheet" href="/katex/katex.css">
 <script defer src="/katex/katex.js"></script>
@@ -15,7 +17,54 @@
 </script>
 {% endmacro katex %}
 
+
+{# =========================== #}
 {# === resize static image === #}
+{# =========================== #}
 {% macro resize_static(path, width=160, height=160, op="fit_height") %}
 {{ resize_image(path="../static" ~ path, width=width, height=height, op=op) }}
-{% endmacro resize_static %}
\ No newline at end of file
+{% endmacro resize_static %}
+
+
+{# ========================= #}
+{# === table of contents === #}
+{# ========================= #}
+{% macro toc(toc, level, depth) %}
+{%- if level == 1 %}
+<div class="toc">
+<h3>Table des matières</h3>
+{%- endif %}
+{%- if level != 1 %}
+<ol class="h{{ level }}">
+{%- endif %}
+{%- for h in toc %}
+    {%- if level != 1 %}
+    <li>
+        <a href="{{ h.permalink | safe }}">{{ h.title }}</a>
+        {%- endif %}
+        {% if h.children and level <= depth -%}
+            {{ self::toc(toc=h.children, level=level+1, depth=depth, heading=false) }}
+        {%- endif %}
+    {%- if level != 1 %}
+    </li>
+    {%- endif %}
+    {%- endfor %}
+{%- if level != 1 %}
+</ol>
+{%- endif %}
+{%- if level == 1 %}
+</div>
+{%- endif %}
+{%- endmacro %}
+
+
+{# =================== #}
+{# === replace toc === #}
+{# =================== #}
+{% macro replace_toc(resource) %}
+	{%- set content = resource.content %}
+	{%- if content is containing("[TOC]") %}
+		{%- set content = content | replace(from="[TOC]", to=self::toc(toc=resource.toc, level=1, depth=resource.extra.toc_depth | default(value=6))) %}
+	{%- endif -%}
+	{{ content | safe }}
+{%- endmacro %}
diff --git a/templates/nav.html b/templates/nav.html
index a20e7ad..d8a41a5 100644
--- a/templates/nav.html
+++ b/templates/nav.html
@@ -2,11 +2,11 @@
   <a href="/" class="w3-bar-item w3-button w3-mobile {% if current_path %}{% if current_path == "/" %}active{% endif %}{% endif %}"><img src="/img/duniter.png" width="32px"/> Duniter</a>
   
   {% set index = get_section(path="_index.md") %}
-  {% for page in index.pages %}
-    {% if not page.extra.hide %}
-    <a href="{{ page.permalink }}" class="w3-bar-item w3-button w3-mobile
-      {% if current_path %}{% if current_path == page.path %}active{% endif %}{% endif %}">
-      {{page.title}}
+  {% for p in index.pages %}
+    {% if not p.extra.hide %}
+    <a href="{{ p.permalink }}" class="w3-bar-item w3-button w3-mobile
+      {% if current_path %}{% if current_path == p.path %}active{% endif %}{% endif %}">
+      {{p.title}}
     </a>
     {% endif %}
   {% endfor %}
@@ -17,7 +17,7 @@
     <div class="w3-dropdown-content w3-bar-block w3-dark-grey">
       <a href="/about/" class="w3-bar-item w3-button w3-mobile">À propos</a>
       <a href="https://git.duniter.org/" class="w3-bar-item w3-button w3-mobile"><i class="fa fa-gitlab"></i> GitLab</a>
-      <a href="https://forum.duniter.org/" class="w3-bar-item w3-button w3-mobile"><i class="fa fa-discourse"></i> Forum</a>
+      <a href="https://forum.duniter.org/" class="w3-bar-item w3-button w3-mobile"><img class="icon" src="https://simpleicons.org/icons/discourse.svg"/> Forum</a>
     </div>
   </div>
 
diff --git a/templates/page.html b/templates/page.html
index cd49946..eb4786e 100644
--- a/templates/page.html
+++ b/templates/page.html
@@ -11,12 +11,6 @@
 
 {% block content %}
 <main class="page w3-container w3-content w3-padding-64">
-
-  {% if not page.extra.toc %}
-    {% set toc = page.toc %}
-    {% include "toc.html" %}
-  {% endif %}
-  
-  {{ page.content | safe }}
+  {{ macros::replace_toc(resource=page) | safe }}
 </main>
 {% endblock content %}
diff --git a/templates/section.html b/templates/section.html
index a275488..299afe2 100644
--- a/templates/section.html
+++ b/templates/section.html
@@ -2,16 +2,12 @@
 
 {% block content %}
   <main class="w3-container w3-margin-mobile">
+    {# ===== content ===== #}
     <div class="page w3-container w3-content w3-padding-64">
-
-    {% if not section.extra.toc %}
-        {% set toc = section.toc %}
-        {% include "toc.html" %}
-    {% endif %}
-
-      {{ section.content | safe }}
+    {{ macros::replace_toc(resource=section) | safe }}
     </div>
 
+    {# ===== child pages and sections ===== #}
     {% if not section.extra.list_pages %}
     <hr>
       <h2>PAGES</h2>
@@ -31,3 +27,6 @@
 
   </main>
 {% endblock content %}
+
+
+{{ macros::format_content(resource=section) }}
\ No newline at end of file
diff --git a/templates/toc.html b/templates/toc.html
deleted file mode 100644
index 4312e7f..0000000
--- a/templates/toc.html
+++ /dev/null
@@ -1,30 +0,0 @@
-<ol class="toc">
-<h2>Table des matières</h2>
-{% for h1 in toc %}
-{# <li><a href="{{h1.permalink | safe}}">{{ h1.title }}</a> #}
-{% if h1.children %}
-<ol class="h2">
-    {% for h2 in h1.children %}
-    <li><a href="{{h2.permalink | safe}}">{{ h2.title }}</a>
-    {% if h2.children %}
-    <ol class="h3">
-        {% for h3 in h2.children %}
-        <li><a href="{{h3.permalink | safe}}">{{ h3.title }}</a>
-        {% if h3.children %}
-        <ol class="h4">
-            {% for h4 in h3.children %}
-            <li><a href="{{h4.permalink | safe}}">{{ h4.title }}</a></li>
-            {% endfor %}
-        </ol>
-        {% endif %}
-        </li>
-        {% endfor %}
-    </ol>
-    {% endif %}
-    </li>
-    {% endfor %}
-</ol>
-{% endif %}
-</li>
-{% endfor %}
-</ol>
\ No newline at end of file
-- 
GitLab