{% set LAYOUT = "full-width" %}
{% extends "base.html" %}
{# Map URL substring matches to link's icon, then label.
Sorted by priority: the first items takes precedence over those at the end. #}
{% set ICONS = [
    ('github.com',                'github',         'Git repository'),
    ('youtube.com',               'play-fill',      'Play video'),
    ('ohloh.net',                 'graph-up',       'Contribution statistics'),
    ('stackoverflow.com',         'stack-overflow', 'Community support'),
    ('stackexchange.com',         'stack-overflow', 'Community support'),
    ('twitter.com',               'twitter',        'Social media activities'),
    ('bugs.launchpad.net',        'bug-fill',       'Reported bugs'),
    ('trac.wordpress.org',        'bug-fill',       'Reported bugs'),
    ('sourceforge.net/apps/trac', 'bug-fill',       'Reported bugs'),
    ('bugs.debian.org',           'bug-fill',       'Reported bugs'),
    ('bugs.kde.org',              'bug-fill',       'Reported bugs'),
    ('mantis',                    'bug-fill',       'Reported bugs'),
    ('ticket=on',                 'bug-fill',       'Reported bugs'),
    ('gmane.org',                 'envelope-fill',  'Mailing-list participation'),
    ('type_of_search=mlists',     'envelope-fill',  'Mailing-list participation'),
    (SITEURL,                     'megaphone-fill', 'Announcement on this blog'),
] %}
{% macro render_card(all_tags, project) %}
    <div class="card">
        {% if project.inactive %}<div class="corner-banner shadow-sm bg-danger text-white fw-bold">Inactive</div>{% endif %}
        {% if project.thumb %}
            {% if project.thumb_link %}<a href="{{ project.thumb_link }}">{% endif %}
                <img class="card-img-top"
                     alt="{{ project.name }} thumbnail"
                     src="{{ project.thumb }}" />
                {% if project.thumb_link %}</a>{% endif %}
        {% endif %}
        <div class="card-body">
            <h4 class="card-title">{{ project.name }}</h4>
            {% if project.roles or project.tools %}
                <p class="card-text">
                    <ul class="list-inline">
                        {% for role in project.roles %}
                            <li class="list-inline-item">
                                <span class="badge text-bg-success">{{ role }}</span>
                            </li>
                        {% endfor %}
                        {# Sort by tag frequency, then alphabetically #}
                        {% for tag in all_tags|sort(attribute='name')|sort(reverse=True, attribute='articles') %}
                            {% if tag.name in project.tools %}<li class="list-inline-item">{{ m.render_tag(tag) }}</li>{% endif %}
                        {% endfor %}
                        {% set tag_names = [] %}
                        {% for tag in all_tags %}
                            {% if tag_names.append(tag.name) %}{% endif %}
                        {% endfor %}
                        {% for tool in project.tools %}
                            {% if tool not in tag_names %}
                                <li class="list-inline-item">
                                    <span class="badge text-bg-secondary">{{ tool }}</span>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ul>
                </p>
            {% endif %}
            {% if project.desc %}<p class="card-text">{{ project.desc }}</p>{% endif %}
            {% if project.links %}
                <p class="card-text">
                    {% for link in project.links %}
                        {# Default icon in case of no match #}
                        {% set matches = [('', 'house-fill', 'Project home')] %}
                        {% for rule in ICONS|reverse %}
                            {% if rule[0] and link.find(rule[0]) != -1 %}
                                {% if matches.append(rule) %}{% endif %}
                            {% endif %}
                        {% endfor %}
                        <a class="btn btn-light rounded-circle p-2"
                           href="{{ link }}"
                           title="{{ matches[-1][2]|e }}">
                            <i class="bi bi-{{ matches[-1][1] }}"></i>
                        </a>
                    {% endfor %}
                </p>
            {% endif %}
        </div>
    </div>
{% endmacro %}
{% macro render_projects(projects) %}
    {% set all_tags = [] %}
    {% for tag, articles in tags %}
        {% if all_tags.append({'name': tag.name, 'url': tag.url, 'articles': articles|length()}) %}{% endif %}
    {% endfor %}
    {% if projects is defined %}
        <div class="row" data-masonry='{"percentPosition": true }'>
            {% block project_pre_content %}{% endblock project_pre_content %}
            {% set active_projects = [] %}
            {% set inactive_projects = [] %}
            {% for project in projects %}
                {% if project.inactive %}
                    {% if inactive_projects.append(project) %}{% endif %}
                {% else %}
                    {% if active_projects.append(project) %}{% endif %}
                {% endif %}
            {% endfor %}
            {% for project_list in [active_projects, inactive_projects] %}
                {% for project in project_list %}
                    <div class="col-sm-6 col-lg-4 mb-4">{{ render_card(all_tags, project) }}</div>
                {% endfor %}
            {% endfor %}
            {% block project_post_content %}{% endblock project_post_content %}
        </div>
    {% endif %}
{% endmacro %}
{% macro render_project_group(group, projects) %}
    {{ group.pre }}
    {{ render_projects(projects) }}
    {{ group.post }}
{% endmacro %}
{% block extra_js %}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/masonry/4.2.2/masonry.pkgd.min.js"
            integrity="sha512-JRlcvSZAXT8+5SQQAvklXGJuxXTouyq8oIMaYERZQasB8SBDHZaUbeASsJWpk0UUrf89DP3/aefPPrlMR1h1yQ=="
            crossorigin="anonymous"
            referrerpolicy="no-referrer"></script>
{% endblock extra_js %}
{% block content %}
    {% if PROJECT_GROUPS is defined %}
        {% for group in PROJECT_GROUPS %}
            {{ render_project_group(group, PROJECTS|selectattr("group", "sameas", group.id) ) }}
        {% endfor %}
        {{ render_projects(PROJECTS|rejectattr("group", "defined") ) }}
    {% else %}
        {{ render_projects(PROJECTS) }}
    {% endif %}
{% endblock content %}
