{% load gadjo %}
{% comment %}
Pagination bar

Expected context variables:
 - request: django request object
 - page_obj: Paginator page object
 - page_key (optional): name of page parameter (default: page)
 - anchor (optional): anchor to use in links

{% endcomment %}
{% if page_obj.paginator.num_pages > 1 %}
{% with page_key=page_key|default:"page" %}
{% spaceless %}
<p class="paginator">
  {% if page_obj.number > 1 %}
    {% if page_obj.previous_page_number != 1 %}
      <a href="{% querystring page_key=1 %}{{ anchor }}">1</a>
      …
    {% endif %}
  {% endif %}

  {% if page_obj.has_previous %}
    <a href="{% querystring page_key=page_obj.previous_page_number %}{{ anchor }}">{{ page_obj.previous_page_number }}</a>
  {% endif %}

  <span class="this-page">{{ page_obj.number }}</span>

  {% if page_obj.has_next %}
    <a href="{% querystring page_key=page_obj.next_page_number %}{{ anchor }}">{{ page_obj.next_page_number }}</a>
  {% endif %}
  {% if page_obj.number != page_obj.paginator.num_pages %}
    {% if page_obj.next_page_number != page_obj.paginator.num_pages %}
      …
      <a href="{% querystring page_key=page_obj.paginator.num_pages %}{{ anchor }}">{{ page_obj.paginator.num_pages }}</a>
    {% endif %}
  {% endif %}
</p>
{% endspaceless %}
{% endwith %}
{% endif %}
