{% extends "base.html" %}

{% block main_content %}

{{ macros_page_header::page_header(title=section.title) }}

{# Set locale for date #}
{% set date_locale = macros_translate::translate(key="date_locale", default="en_GB", language_strings=language_strings) %}

{#- Check for language-specific date formats -#}
{%- set language_format = "" -%}
{%- if config.extra.date_formats -%}
    {%- for format_config in config.extra.date_formats -%}
        {%- if format_config.lang == lang -%}
            {%- if format_config.archive -%}
                {%- set_global language_format = format_config.archive -%}
            {%- endif -%}
        {%- endif -%}
    {%- endfor -%}
{%- endif -%}

<div class="archive">
    <ul class="list-with-title">
        {%- set source_paths = section.extra.section_path | default(value="blog/") -%}
        {%- if source_paths is iterable -%}
            {%- set paths = source_paths -%}
        {%- else -%}
            {%- set paths = [source_paths] -%}
        {%- endif %}
        {%- set all_posts = [] -%}
        {%- for path in paths -%}
            {%- if lang == config.default_language %}
                {%- set section_item = get_section(path=path ~ "_index.md") -%}
            {%- else %}
                {%- set section_item = get_section(path=path ~ "_index." ~ lang ~ ".md") -%}
            {%- endif %}
            {%- set_global all_posts = all_posts | concat(with=section_item.pages) -%}
        {%- endfor %}

        {# Sort all posts by date #}
        {%- set archive_reverse = section.extra.archive_reverse | default(value=false) -%}
        {%- set all_posts = all_posts | sort(attribute="date") -%}
        {%- if not archive_reverse -%}
            {%- set all_posts = all_posts | reverse -%}
        {%- endif -%}

        {# Group posts by year. #}
        {% set posts_by_year = all_posts | group_by(attribute="year") %}
        {% set years = [] %}
        {% for year, ignored in posts_by_year %}
            {% set_global years = years | concat(with=[year]) %}
        {% endfor %}

        {# Iterate over years #}
        {% set years = years | sort %}
        {%- if not archive_reverse -%}
            {%- set years = years | reverse -%}
        {%- endif -%}

        {% for year in years %}
            {% set posts = posts_by_year[year] %}
            {% if posts | length > 0 %}
            <li>
                <h2 class="listing-title">{{ year }}</h2>
                <ul class="listing">
                    {% for post in posts %}
                    <li class="listing-item">
                        <div class="post-time">
                            <span class="date">
                                {%- if language_format -%}
                                    {{ post.date | date(format=language_format, locale=date_locale) }}
                                {%- elif config.extra.archive_date_format -%}
                                    {{ post.date | date(format=config.extra.archive_date_format, locale=date_locale) }}
                                {%- else -%}
                                    {{ post.date | date(format="%d %b", locale=date_locale) }}
                                {%- endif -%}
                            </span>
                        </div>
                        <a href="{{ post.permalink }}" title="{{ post.title }}">{{ post.title | markdown(inline=true) | safe }}</a>
                    </li>
                    {% endfor %}
                </ul>
            </li>
            {% endif %}
        {% endfor %}
    </ul>
</div>

{% endblock main_content %}
