{#
MIT License

Copyright (c) 2021 vuquangtrong
Copyright (c) 2021 Liang Yesheng

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#}

{% block style %}
<style>
    .md-typeset .blog-post:first-of-type h3 {
        margin-top: 0;
    }

    .md-typeset .blog-post-title {
        margin-bottom: 0;
    }

    .md-typeset .blog-post-extra {
        color: var(--md-default-fg-color--light);
    }

    .md-typeset .blog-center {
        text-align: center;
    }

    .md-typeset .blog-pagination {
        display: inline-block;
    }

    .md-typeset .blog-pagination a {
        color: var(--md-typeset-color);
        float: left;
        padding: .25em 1em;
        text-decoration: none;
        border-radius: 1em;
        margin-left: .25em;
        margin-right: .25em;
        transition: all .15s ease-in-out;
    }

    .md-typeset .blog-pagination a.active {
        background-color: var(--md-typeset-a-color);
        color: white;
        font-weight: bold;
    }

    .md-typeset .blog-pagination a:hover:not(.active) {
        background-color: #dddddda1;
    }

    .md-typeset .blog-hidden {
        display: none;
    }
</style>
{% endblock %}

{% set page_num = (pages|count / page_size)|round(method='ceil')|int %}

{% if page_num == 0 or not paging %}
    {% set page_num = 1 %}
    {% if not paging %}
        {% set page_size = 10000 %}
    {% endif %}
{% endif %}

{# Define a macro to render tags, if not prodided. #}
{% if render_tags is not defined %}
{% from "blog-tags-render.html" import render_tags %}
{% endif %}

{% if get_tags_style is not defined %}
{% from "blog-tags-render.html" import get_tags_style %}
{% endif %}

{% block tags_style %}
    {% call get_tags_style() %}
    {% endcall %}
{% endblock %}

{# Render blogs. If a marco is provided, then use the marco to render. #}
{% if render_blog is not defined %}
{% macro render_blog(title, description, time, url, pg) -%}
<div class="blog-post">
    <h3 class="blog-post-title">
        <a class="link" href="{{ url }}">{{ title }}</a>
    </h3>
    {% if show_tags and "tags" in pg.meta %}
        {% call render_tags(pg.meta["tags"], index_url) %}
        {% endcall %}
    {% endif %}
    <p class="blog-post-description">
        {{ description }}
    </p>
    <div class="blog-post-extra">
        {{ ("Updated" if is_revision else "Published") + " at: " + time }}
    </div>
    <hr />
</div>
{{ caller() }}
{%- endmacro %}
{% endif %}

<div class="pages">
    {% for page_idx in range(0, page_num) %}
        {% set pg_group = pages[page_idx*page_size:(page_idx + 1)*page_size] %}
        <div class="page" id="page{{ page_idx + 1 }}">

            {% for pg in pg_group %}
                {# Setting variables. #}
                {% set url = pg.canonical_url %}
                {% set title = pg.title %}
                {% if pg.meta and pg.meta.title_full %}
                    {% set title = pg.meta.title_full %}
                {% endif %}

                {% set description = "" %}
                {% if pg.meta.description %}
                    {% set description = pg.meta.description|truncate %}
                {% endif %}

                {% set time = "" %}
                {% if pg.meta and pg.meta["localized-time"] %}
                    {% set time = pg.meta["localized-time"] %}
                {% endif %}
                
                {% call render_blog(title, description, time, url, pg) %}
                {% endcall %}
            {% endfor %}
        </div>
    {% endfor %}
</div>

<div class="blog-center {{'blog-hidden' if page_num == 1 else '' }}">
    <div class="blog-pagination " id="blog-pagination">
        {% for num in range(page_num) %}
            <a class="page-number" href="#blog-p{{ num + 1 }}">{{ num + 1 }}</a>
        {% endfor %}
    </div>
    {% if show_total %}
        <div>Total <b>{{ pages|count }}</b> posts.</div>
    {% endif %}
</div>