## -*- coding: utf-8 -*-

<%def name="_head_feed_link(link_type, link_name, link_postfix, classification, kind, language)">
    % if len(translations) > 1:
        <link rel="alternate" type="${link_type}" title="${link_name|h} (${language})" hreflang="${language}" href="${_link(kind + '_' + link_postfix, classification, language)}">
    % else:
        <link rel="alternate" type="${link_type}" title="${link_name|h}" hreflang="${language}" href="${_link(kind + '_' + link_postfix, classification, language)}">
    % endif
</%def>

<%def name="_html_feed_link(link_type, link_name, link_postfix, classification, kind, language, name=None)">
    % if len(translations) > 1:
        % if name and kind != "archive" and kind != "author":
            <a href="${_link(kind + '_' + link_postfix, classification, language)}" hreflang="${language}" type="${link_type}">${messages(link_name, language)} (${name|h}, ${language})</a>
        % else:
            <a href="${_link(kind + '_' + link_postfix, classification, language)}" hreflang="${language}" type="${link_type}">${messages(link_name, language)} (${language})</a>
        % endif
    % else:
        % if name and kind != "archive" and kind != "author":
            <a href="${_link(kind + '_' + link_postfix, classification, language)}" hreflang="${language}" type="${link_type}">${messages(link_name, language)} (${name|h})</a>
        % else:
            <a href="${_link(kind + '_' + link_postfix, classification, language)}" hreflang="${language}" type="${link_type}">${messages(link_name, language)}</a>
        % endif
    % endif
</%def>

<%def name="_html_translation_link(classification, kind, language, name=None)">
    % if name and kind != "archive" and kind != "author":
        <a href="${_link(kind, classification, language)}" hreflang="${language}" rel="alternate">${messages("LANGUAGE", language)} (${name|h})</a>
    % else:
        <a href="${_link(kind, classification, language)}" hreflang="${language}" rel="alternate">${messages("LANGUAGE", language)}</a>
    % endif
</%def>

<%def name="_head_rss(classification=None, kind='index', rss_override=True)">
    % if rss_link and rss_override:
        ${rss_link}
    % endif
    % if generate_rss and not (rss_link and rss_override) and kind != 'archive':
        % if len(translations) > 1 and has_other_languages and classification and kind != 'index':
            % for language, classification, name in all_languages:
                <link rel="alternate" type="application/rss+xml" title="RSS for ${kind} ${name|h} (${language})" hreflang="${language}" href="${_link(kind + "_rss", classification, language)}">
            % endfor
        % else:
            % for language in sorted(translations):
                % if (classification or classification == '') and kind != 'index':
                    ${_head_feed_link('application/rss+xml', 'RSS for ' + kind + ' ' + classification, 'rss', classification, kind, language)}
                % else:
                    ${_head_feed_link('application/rss+xml', 'RSS', 'rss', classification, 'index', language)}
                % endif
            % endfor
        % endif
    % endif
</%def>

<%def name="_head_atom(classification=None, kind='index')">
    % if generate_atom:
        % if len(translations) > 1 and has_other_languages and classification and kind != 'index':
            % for language, classification, name in all_languages:
                <link rel="alternate" type="application/atom+xml" title="Atom for ${kind} ${name|h} (${language})" hreflang="${language}" href="${_link(kind + "_atom", classification, language)}">
            % endfor
        % else:
            % for language in sorted(translations):
                % if (classification or classification == '') and kind != 'index':
                    ${_head_feed_link('application/atom+xml', 'Atom for ' + kind + ' ' + classification, 'atom', classification, kind, language)}
                % else:
                    ${_head_feed_link('application/atom+xml', 'Atom', 'atom', classification, 'index', language)}
                % endif
            % endfor
        % endif
    % endif
</%def>

## Handles both feeds and translations
<%def name="head(classification=None, kind='index', feeds=True, other=True, rss_override=True, has_no_feeds=False)">
    % if feeds and not has_no_feeds:
        ${_head_rss(classification, 'index' if (kind == 'archive' and rss_override) else kind, rss_override)}
        ${_head_atom(classification, kind)}
    % endif
    % if other and has_other_languages and other_languages:
        % for language, classification, _ in other_languages:
            <link rel="alternate" hreflang="${language}" href="${_link(kind, classification, language)}">
        % endfor
    % endif
</%def>

<%def name="feed_link(classification, kind)">
    % if generate_atom or generate_rss:
        % if len(translations) > 1 and has_other_languages and kind != 'index':
            % for language, classification, name in all_languages:
                <p class="feedlink">
                    % if generate_atom:
                        ${_html_feed_link('application/atom+xml', 'Atom feed', 'atom', classification, kind, language, name)}
                    % endif
                    % if generate_rss and kind != 'archive':
                        ${_html_feed_link('application/rss+xml', 'RSS feed', 'rss', classification, kind, language, name)}
                    % endif
                </p>
            % endfor
        % else:
            % for language in sorted(translations):
                <p class="feedlink">
                    % if generate_atom:
                        ${_html_feed_link('application/atom+xml', 'Atom feed', 'atom', classification, kind, language)}
                    % endif
                    % if generate_rss and kind != 'archive':
                        ${_html_feed_link('application/rss+xml', 'RSS feed', 'rss', classification, kind, language)}
                    % endif
                </p>
            % endfor
        % endif
    % endif
</%def>

<%def name="translation_link(kind)">
    % if has_other_languages and other_languages:
        <div class="translationslist translations">
            <h3 class="translationslist-intro">${messages("Also available in:")}</h3>
        % for language, classification, name in other_languages:
            <p>${_html_translation_link(classification, kind, language, name)}</p>
        % endfor
        </div>
    % endif
</%def>
