Grimoire-
Command
.es

GNU+Linux command memo

Pelican / jinja full featured blog pagination

Pagination pour Pelican, codée avec le langage de templates Jinja. Elle comporte : page suivante, page précédente, accès direct aux 5 pages suivantes (ou précédentes) et accès direct à n’importe quelle page quand on clique sur les points de suspension, via dialogue JavaScript. C’est la pagination mise en place pour ce blog.

Pagination for Pelican, made with Jinja. It features : page next, previous page, direct access to the 5 next pages (or previous) and direct access to every pages when clicking on the '…' via JavaScript dialog. It’s the pagination of this blog.

{% if DEFAULT_PAGINATION and articles_paginator and articles_paginator.num_pages > 1 %}
{% set first_page = articles_paginator.page(1) %}
{% set second_page = articles_paginator.page(2) %}
{% set last_page = articles_paginator.page(articles_paginator.num_pages) %}

{% set next_2 = articles_page.number + 2 %}
{% set next_3 = articles_page.number + 3 %}
{% set next_4 = articles_page.number + 4 %}
{% set next_5 = articles_page.number + 5 %}

{% set prev_2 = articles_page.number - 2 %}
{% set prev_3 = articles_page.number - 3 %}
{% set prev_4 = articles_page.number - 4 %}
{% set prev_5 = articles_page.number - 5 %}

{% if articles_paginator.num_pages >= next_2 %}
  {% set next_2pg = articles_paginator.page(next_2) %}
{% endif %}
{% if articles_paginator.num_pages >= next_3 %}
  {% set next_3pg = articles_paginator.page(next_3) %}
{% endif %}
{% if articles_paginator.num_pages >= next_4 %}
  {% set next_4pg = articles_paginator.page(next_4) %}
{% endif %}
{% if articles_paginator.num_pages >= next_5 %}
  {% set next_5pg = articles_paginator.page(next_5) %}
{% endif %}

{% if prev_2 > 0 %}
  {% set prev_2pg = articles_paginator.page(prev_2) %}
{% endif %}
{% if prev_3 > 0 %}
  {% set prev_3pg = articles_paginator.page(prev_3) %}
{% endif %}
{% if prev_4 > 0 %}
  {% set prev_4pg = articles_paginator.page(prev_4) %}
{% endif %}
{% if prev_5 > 0 %}
  {% set prev_5pg = articles_paginator.page(prev_5) %}
{% endif %}

<ul class="uk-pagination">
  {% if articles_page.number > 1 %}
  <li><a href="/{{ articles_previous_page.url }}">«</a></li>
  {% endif %}
  {% if articles_page.number > 6 %}
  <li><a href="/{{ first_page.url }}">{{ first_page.number }}</a></li>
  {% endif %}
  {% if articles_page.number > 7 %}
  <li><a href="javascript:choose_your_page()">…</a></li>
  {% endif %}
  {% if prev_5pg %}<li><a href="/{{ prev_5pg.url }}">{{ prev_5pg.number }}</a></li>{% endif %}
  {% if prev_4pg %}<li><a href="/{{ prev_4pg.url }}">{{ prev_4pg.number }}</a></li>{% endif %}
  {% if prev_3pg %}<li><a href="/{{ prev_3pg.url }}">{{ prev_3pg.number }}</a></li>{% endif %}
  {% if prev_2pg %}<li><a href="/{{ prev_2pg.url }}">{{ prev_2pg.number }}</a></li>{% endif %}
  {% if articles_previous_page %}<li><a href="/{{ articles_previous_page.url }}">{{ articles_previous_page.number }}</a></li>{% endif %}
  <li class="uk-active"><span>{{ articles_page.number }}</span></li>
  {% if articles_next_page %}<li><a href="/{{ articles_next_page.url }}">{{ articles_next_page.number }}</a></li>{% endif %}
  {% if next_2pg %}<li><a href="/{{ next_2pg.url }}">{{ next_2pg.number }}</a></li>{% endif %}
  {% if next_3pg %}<li><a href="/{{ next_3pg.url }}">{{ next_3pg.number }}</a></li>{% endif %}
  {% if next_4pg %}<li><a href="/{{ next_4pg.url }}">{{ next_4pg.number }}</a></li>{% endif %}
  {% if next_5pg %}<li><a href="/{{ next_5pg.url }}">{{ next_5pg.number }}</a></li>{% endif %}
  {% if articles_page.number < last_page.number - 5 %}
  <li><a href="javascript:choose_your_page()">…</a></li>
  <li><a href="/{{ last_page.url }}">{{ last_page.number }}</a></li>
  {% endif %}
  {% if articles_page.number < last_page.number %}
  <li><a href="/{{ articles_next_page.url}}">»</a></li>
  {% endif %}
</ul>
<script>
  if (typeof choose_your_page === 'undefined')
    function choose_your_page () {
        let p_nb=prompt('# page')
        if (p_nb == 1)
          return location.href=('{{ first_page.url }}')
        if (p_nb > 1 && p_nb <= {{ articles_paginator.num_pages }})
          location.href=('{{ second_page.url }}'.replace(/2\.html/, `${p_nb}.html`))
        else
          alert('/!\\ Impossible /!\\')
    }
</script>
{% endif %}