summaryrefslogtreecommitdiff
path: root/templates/macros/blog.html
blob: d0767625e8cf768bbb093ef476b17d0c22861ca7 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
{% from "macros/pagination.html" import render_pagination %}

{% macro render_toc(toc, maxdepth=2) %}
  <nav aria-label="Table of contents">
    <p>Table of contents</p>
    <ol>
      {% for item in toc recursive %}
        <li>
          <a href="#{{ item.anchor }}">{{ item.title }}</a>
          {% if item.children and loop.depth < maxdepth %}
            <ol>
              {{ loop(item.children) }}
            </ol>
          {% endif %}
        </li>
      {% endfor %}
    </ol>
  </nav>
{% endmacro %}

{% macro render_blog_post(post, show_toc=True) %}
  {% set toc = namespace(items=[]) %}

  {% if show_toc and post.render_toc %}
    {% for block in post.body.blocks %}
      {% if block._flowblock == "markdown" and block.text %}
        {% set toc.items = toc.items + (block.text.toc | default([])) %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {% set latest_change = post.changelog.blocks | max(attribute="date") if post.changelog else none %}

  <article class="h-entry">
    <header>
      <div>
        <h2>
          <a href="{{ post|url }}" class="u-url p-name">{{ post.title }}</a>
        </h2>
        <dl>
          {% if post.publication_date %}
            <div>
              <dt>Posted:</dt>
              <dd>
                <a href="{{ ('/blog@index-pages/year/{}'.format(post.publication_date.year) ) | url }}">
                  <time datetime="{{ post.publication_date | dateformat('yyyy-MM-dd') }}"
                        class="dt-published">{{ post.publication_date | dateformat("d MMMM yyyy") }}</time>
                </a>
              </dd>
            </div>
          {% else %}
            <div class="missing-publication-date-warning" role="alert">
              <dt>⚠</dt>
              <dd>
                Missing publication date
              </dd>
            </div>
          {% endif %}
          {% if latest_change and post.publication_date and latest_change.date > post.publication_date %}
            <div>
              <dt>Last updated:</dt>
              <dd>
                <time datetime="{{ latest_change.date | dateformat('yyyy-MM-dd') }}"
                      class="dt-updated">{{ latest_change.date | dateformat("d MMMM yyyy") }}</time>
              </dd>
            </div>
          {% endif %}
          {% if post.category %}
            <div>
              <dt>Category:</dt>
              <dd>
                {% set category = site.get('/categories/'+post.category) %}
                <a href="{{ category|url }}" class="p-category">{{ category.name }}</a>
              </dd>
            </div>
          {% endif %}
          <a rel="author"
             class="p-author h-card display-none"
             href="{{ '/about' | url(absolute=True) }}">Dennis Fink</a>
        </dl>
      </div>
      {% if show_toc and post.render_toc %}
        {{ render_toc(toc.items) }}
      {% endif %}
    </header>
    <div class="e-content">{{ post.body }}</div>
    {% if post.tags or post.changelog %}
      <footer>
        {% if post.tags %}
          <ul aria-label="Tags">
            <li>Tagged with</li>
            {% for tag in post.tags %}
              <li>
                {% set tag_slug = tag|trim|lower|replace(" ", "-") %}
                <a href="{{ ('/blog/tag/' ~ tag_slug ~ '/')|url }}"
                   rel="tag"
                   class="p-category">{{ tag }}</a>
                {%- if not loop.last -%}, {%- else -%}.{%- endif -%}
              </li>
            {% endfor %}
          </ul>
        {% endif %}
        {% if post.changelog %}
          <section class="changelog" aria-labelledby="changelog-heading">
            <h3 id="changelog-heading">Changelog</h3>
            <ul>
              {% for entry in post.changelog.blocks %}
                <li>
                  <time datetime="{{ entry.date | dateformat('yyyy-MM-dd') }}">
                    {{ entry.date | dateformat("d MMMM yyyy") }}
                  </time>
                  <span>{{ entry.description }}</span>
                </li>
              {% endfor %}
            </ul>
          </section>
        {% endif %}
      </footer>
    {% endif %}
  </article>
{% endmacro %}

{% macro render_blog_index(pagination) %}
  {% for post in pagination.items %}
    {{ render_blog_post(post, False) }}
    {% if not loop.last %}
      <hr>
    {% endif %}
  {% endfor %}
  {% if pagination.pages > 1 %}
    <footer>
      {{ render_pagination(pagination) }}
    </footer>
  {% endif %}
{% endmacro %}