{% load i18n %}

<ul class="pagination {{ pagination_size|default:"" }}">
    <li class="prev {% if not page.has_previous %}disabled{% endif %}">
        <a href="{% if page.has_previous %}?q={{ query }}&amp;page={{ page.previous_page_number }}{% endif %}">&laquo;</a>
    </li>
    {% for number in paginator.page_range %}
        {% if forloop.first %}
            <li{% if page.number == number %} class="active"{% endif %}><a href="?q={{ query }}&amp;page={{ number }}">{{ number }}</a></li>
            {% if page.number > 4 %}
                <li><span>...</span></li>
            {% endif %}
        {% elif forloop.last %}
            {% if page.number < paginator.num_pages|add:"-3" %}
                <li><span>...</span></li>
            {% endif %}
            <li{% if page.number == number %} class="active"{% endif %}><a href="?q={{ query }}&amp;page={{ number }}">{{ number }}</a></li>
        {% else %}
            {% if page.number < 3 and number <= 5 %}
                <li{% if page.number == number %} class="active"{% endif %}><a href="?q={{ query }}&amp;page={{ number }}">{{ number }}</a></li>
            {% elif page.number > paginator.num_pages|add:"-2" and number >= paginator.num_pages|add:"-4" %}
                <li{% if page.number == number %} class="active"{% endif %}><a href="?q={{ query }}&amp;page={{ number }}">{{ number }}</a></li>
            {% elif number >= page.previous_page_number|add:"-1" and number <= page.next_page_number|add:"1" %}
                <li{% if page.number == number %} class="active"{% endif %}><a href="?q={{ query }}&amp;page={{ number }}">{{ number }}</a></li>
            {% endif %}
        {% endif %}
    {% endfor %}
    <li class="next {% if not page.has_next %}disabled{% endif %}">
        <a href="{% if page.has_next %}?q={{ query }}&amp;page={{ page.next_page_number }}{% endif %}" >&raquo;</a>
    </li>
</ul>
