{{ define "main" }}

<h1>{{ .Title }}</h1>

<div class="post">
    {{ partial "headline-hash.html" .Content }}

    {{ with site.Data.users }}
        <!-- https://datatables.net/manual/installation#CDN
             "remember to include jQuery as well"
        -->
        <script src="{{ "js/libs/jquery/3.3.1/jquery.min.js" | absURL }}" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
        <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
        <!-- SHA256 of the below script:
             https://report-uri.com/home/hash/
             'sha256-YiWMVJIplziLxmHOI6iqs51JQS9W2hg1wDsKBD3u09g='

             NOTE that the hash has to be recalculated and updated
             in the CSP in the headers each time there's even any
             whitespace/indentation change in the below script.
        -->
        <script type="text/javascript">
         $(document).ready(function() {
             $('.ox-hugo-examples').DataTable({
                 "order": [[ 3, 'desc' ]],// Sort rows based on 4th (counting begins from 0) column, sorted in descending order
                 "paging": false,
                 "info": true // Default is true, prints "Showing x of y entries"
             }); } );
        </script>

        <table class="ox-hugo-examples">
            <thead>
                <tr><th>Author</th><th>Site</th><th>Org Source</th><th>Last Updated*</th></tr>
            </thead>
            <tbody>
                {{ range $key, $val := . }}
                    {{ $org_dir := $val.org_dir | default "" | replaceRE "/$" "" }}
                    {{ $branch := $val.branch | default "master" | replaceRE "/" "%2F" }}

                    <!-- Setting $hosting and $api -->
                    {{ $hosting := $val.hosting }}
                    {{ $api := $val.api }}
                    {{ if (findRE "github\\.com" $val.source) }}
                        {{ $hosting = "github" }}
                        {{ $api = "github" }}
                    {{ else if (findRE "gitlab\\.com" $val.source) }}
                        {{ $hosting = "gitlab" }}
                        {{ $api = "gitlab" }}
                    {{ else if (findRE "git\\.sr\\.ht" $val.source) }}
                        {{ $hosting = "sr.ht" }}
                    {{ else if (findRE "coding\\.net" $val.source) }}
                        {{ $hosting = "coding.net" }}
                    {{ else if (findRE "[Gg]itweb" $val.source) }}
                        {{ $hosting = "gitweb" }}
                    {{ end }}
                    {{/* partial "debugprint.html" (slice $key $hosting $api) */}}

                    <!-- Get JSON via API to figure out the $last_updated time -->
                    {{ $last_updated := "" }}
                    <!-- Do not do the getJSON calls if running hugo server -->
                    {{ if (not site.IsServer) }}
                        {{ if (eq "github" $api) }}
                            {{ $repo := replaceRE "^.*github\\.com/([^/]+/[^/]+).*" "${1}" $val.source }}
                            <!-- https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository -->
                            <!-- https://api.github.com/repos/kaushalmodi/ox-hugo/commits?path=doc&sha=master -->
                            {{ $repo_json := getJSON (printf "https://api.github.com/repos/%s/commits?path=%s&sha=%s" $repo $org_dir $branch) }}
                            {{ with $repo_json }}
                                {{ $last_updated = (index (index (index (index . 0) "commit") "committer") "date") }}
                            {{ end }}
                        {{ else if (eq "gitlab" $api) }}
                            {{ $repo := replaceRE "^.*gitlab\\.com/([^/]+/[^/]+).*" "${1}" $val.source | replaceRE "/" "%2F" }}
                            {{ $org_dir = $org_dir | replaceRE "/" "%2F" }}
                            <!-- https://docs.gitlab.com/ee/api/commits.html#list-repository-commits -->
                            <!-- https://gitlab.com/api/v4/projects/kaushalmodi%2Fkaushalmodi.gitlab.io/repository/commits?path=content-org&ref_name=master -->
                            {{ $repo_json := getJSON (printf "https://gitlab.com/api/v4/projects/%s/repository/commits?path=%s&?ref_name=%s" $repo ($org_dir | replaceRE "/" "%2F") $branch) }}
                            {{/* partial "debugprint.html" $repo_json */}}
                            {{ with $repo_json }}
                                {{ $last_updated = (index (index . 0) "committed_date") }}
                            {{ end }}
                        {{ else if (eq "gitea" $api) }}
                            {{ $repo := replaceRE "^.*//[^/]+/([^/]+/[^/]+).*" "${1}" $val.source }}
                            {{ $api_url := printf "%s/api/v1" (replaceRE "^(.*//[^/]+)/.+" "${1}" $val.source) }}
                            <!-- https://try.gitea.io/api/swagger#/repository/repoGet -->
                            <!-- https://git.fsfe.org/api/v1/repos/adolflow/infotics.es -->
                            {{ $repo_json := getJSON (printf "%s/repos/%s" $api_url $repo) }}
                            {{/* partial "debugprint.html" $repo_json */}}
                            {{ with $repo_json }}
                                {{ $last_updated = index . "updated_at" }}
                            {{ end }}
                        {{ else if (eq "micro.json" $api) }}
                            {{ $site_url := $val.site | replaceRE "/$" "" }}
                            {{ $micro_json := getJSON (printf "%s/micro.json" $site_url) }}
                            {{/* partial "debugprint.html" $micro_json */}}
                            {{ with $micro_json }}
                                {{ $last_updated = index . "last_updated" }}
                            {{ end }}
                        {{ end }}
                    {{ end }}

                    <!-- Get the Org source link -->
                    {{ $org_dir_link := $val.source }}
                    {{ if (in (slice "github" "gitlab" "sr.ht" "coding.net") $hosting) }}
                        {{ $org_dir_link = (printf "%s/tree/%s/%s" ($val.source | replaceRE "/$" "") $branch $org_dir) }}
                    {{ else if (eq "gitea" $hosting) }}
                        {{ $org_dir_link = (printf "%s/src/branch/%s/%s" ($val.source | replaceRE "/$" "") $branch $org_dir) }}
                    {{ else if (eq "gitweb" $hosting) }}
                        <!-- Example: https://www.kengrimes.com/gitweb/?p=kengrimes.com/content.git;a=tree;f=content;hb=refs/heads/master
                             $val.source = https://www.kengrimes.com/gitweb/?p=kengrimes.com/content.git
                             $org_dir if non-nil will be used as f=$org_dir
                             $branch = master
                        -->
                        {{ $folder_switch := (cond (eq "" $org_dir) "" (printf ";f=%s" $org_dir)) }}
                        {{ $org_dir_link = (printf "%s;a=tree%s;hb=refs/heads/%s" ($val.source | replaceRE "/$" "") $folder_switch $branch) }}
                    {{ else if (findRE "\\.org$" $val.source) }} <!-- If the "source" is direct link to an Org file -->
                        {{ $org_dir_link = $val.source }}
                    {{ end }}

                    <tr>
                        <!-- Author -->
                        <td>{{ $val.author }}</td>
                        <!-- Site -->
                        <!-- Don't make htmltest error out becaue of user-submitted non-secure http domains.
                             https://github.com/wjdp/htmltest/
                        -->
                        {{ $data_proofer_ignore_tag := "" }}
                        {{ $secure_icon := "" }}
                        {{ $user_site_is_nonsecure := findRE "^http://" $val.site }}
                        {{ if $user_site_is_nonsecure }}
                            {{ $data_proofer_ignore_tag = " data-proofer-ignore" }}
                        {{ else }}
                            {{ $secure_icon = "🔐&nbsp;" | safeHTML }}
                        {{ end }}
                        <td>{{ printf `%s<a href="%s"%s>%s</a>` $secure_icon $val.site $data_proofer_ignore_tag ($val.site | replaceRE "^https?://" "" | replaceRE "/$" "") | safeHTML }}</td>
                        <!-- Org Source -->
                        <td>{{ printf `<a href="%s">%s</a>` $org_dir_link ($org_dir | default (or $val.org_src_desc "<em>repo root</em>") | replaceRE "[^<]*/" "") | safeHTML }}</td>
                        <!-- Last Updated -->
                        <td>
                            {{ with $last_updated }}
                                {{ . | dateFormat "2006/01/02" }}
                            {{ end }}
                        </td>
                    </tr>
                {{ end }}
            </tbody>
        </table>
    {{ end }}

    {{ with .Params.after_content }}
        <p>
            {{ . | markdownify }}
        </p>
    {{ end }}

    <hr />
    <small>
        <sup>*</sup> The "Last Updated" dates were retrieved when this page was last updated.. on {{ now | dateFormat "Jan 2, 2006" }}.
    </small>
</div>

{{ end }}
