{% extends "base.html" %}
{% block title %}All tags{% endblock title %}
{% block top_center %}
    <h1>
        <a href="{{ SITEURL }}/{{ TAGS_SAVE_AS }}"
           rel="bookmark"
           title="Permalink to tag list">Tags</a>
    </h1>
{% endblock top_center %}
{% block content %}
    {% set all_tags = [] %}
    {% for tag, articles in tags %}
        {% if all_tags.append({'name': tag.name, 'url': tag.url, 'articles': articles|length()}) %}{% endif %}
    {% endfor %}
    {% set tier_1 = [] %}
    {% set tier_2 = [] %}
    {% set tier_3 = [] %}
    {% set total_tags = all_tags|sum(attribute = 'articles') %}
    {# Sort by tag frequency, then alphabetically #}
    {% for tag in all_tags|sort(attribute='name')|sort(reverse=True, attribute='articles') %}
        {% if tier_1|sum(attribute = 'articles') < total_tags / 3.0 or tier_1[-1].articles == tag.articles %}
            {% if tier_1.append(tag) %}{% endif %}
        {% elif tier_2|sum(attribute = 'articles') < total_tags / 3.0 or tier_2[-1].articles == tag.articles %}
            {% if tier_2.append(tag) %}{% endif %}
        {% else %}
            {% if tier_3.append(tag) %}{% endif %}
        {% endif %}
    {% endfor %}
    {% for (tier_name, tier_tags) in [("Top", tier_1), ("Middle", tier_2), ("Lower", tier_3)] %}
        <h2>{{ tier_name }} tier</h2>
        <ul class="list-inline">
            {% for tag in tier_tags %}<li class="list-inline-item">{{ m.render_tag(tag, counter=tag.articles) }}</li>{% endfor %}
        </ul>
    {% endfor %}
{% endblock content %}
