{{ define "urls" }}
 <h2>Backlinks, outgoing URLs, bibliographic references and internal links:</h2>
 {{ $s := partialCached "scratch.html" . }}

 <div id="toggleAllLinks" onclick="toggleAllLinks()">[toggle expand/collapse]</div>
 <p></p>
 {{ range $p := .Site.RegularPages }}
  {{ with or ($p.Scratch.Get "urls") ($p.Scratch.Get "bibs") ($p.Scratch.Get "links") ($p.Scratch.Get "backlinks" )}}
   <details><summary>{{ with $p.File }}<span class="sup">{{- substr .Dir 0 -1  -}}⁄</span>{{ end }}<a href="{{ $p.RelPermalink }}{{ if eq hugo.Environment "offline" }}index.html{{ end }}">{{ $p.Title }}</a></summary>

   {{ $pPath := printf "/%s" $p.File }}
   {{ with $ss := $s.Get $pPath }}
    {{ with $ss.backlinks }}
     {{ range $i, $_ := $ss.backlinks }}
      {{ if eq $i 0 }}
       <details><summary>Mentioned in other Sandpoints pages ({{ len $ss.backlinks }}):</summary><ul>
      {{ end }}
      <li>{{ with $_.File }}<span class="sup">{{ substr .Dir 0 -1 }}⁄</span>{{ end }}<a href="{{ $_.RelPermalink }}">{{ $_.Title }}</a></li>
     {{ end }}
     </ul></details>
    {{ end }}
   {{ end }}

   {{ with ($p.Scratch.Get "links") }}
    {{ range $i, $_ := ($p.Scratch.Get "links") }}
     {{ if eq $i 0 }}
       <details><summary>Internal links to other Sandpoint pages ({{ len ($p.Scratch.Get "links") }}):</summary><ul>
     {{ end }}
     <li>{{ with $_.File }}<span class="sup">{{ substr .Dir 0 -1 }}⁄</span>{{ end }}<a href="{{ $_.RelPermalink }}">{{ $_.Title }}</a></li>
    {{ end }}
    </ul></details>
   {{ end }}

   {{ $http_links := (slice) }}
   {{ $library_links := (slice) }}
   {{ $other_links := (slice) }}

   {{ with ($p.Scratch.Get "urls") }}
    {{- $c_prefix := $p.Site.Params.sandpointsCatalogPrefix | default "/library/" -}}
    {{ range $i, $_ := ($p.Scratch.Get "urls") }}
     {{ if strings.HasPrefix $_ "http" }}
      {{ $http_links = $http_links | append $_ }}
     {{ else if strings.HasPrefix $_ $c_prefix }}
      {{ $library_links = $library_links | append $_ }}
     {{ else}}
      {{ $other_links = $other_links | append $_ }}
     {{ end }}
    {{ end }}
   {{ end }}

   {{ with $http_links }}
    {{ range $i, $_ := $http_links }}
     {{ if eq $i 0 }}
       <details><summary>Outgoing links to other pages on Internet ({{ len $http_links }}):</summary><ul>
     {{ end }}
     <li><span class="sup">www⁄</span><a href="{{ $_ }}" target="_blank">{{ $_ }}</a></li>
    {{ end }}
    </ul></details>
   {{ end }}

   {{ with $library_links }}
    {{ range $i, $_ := $library_links }}
     {{ if eq $i 0 }}
       <details><summary>Links to Sandpoint's library ({{ len $library_links }}):</summary><ul>
     {{ end }}
     <li><span class="sup">lib⁄</span><a href="{{ $_ }}" target="_blank">{{ $_ }}</a></li>
    {{ end }}
    </ul></details>
   {{ end }}

   {{ with ($p.Scratch.Get "bibs") }}
    {{ range $i, $_ := ($p.Scratch.Get "bibs") }}
     {{ if eq $i 0 }}
       <details><summary>Bibliographic references ({{ len ($p.Scratch.Get "bibs") }}):</summary><ul>
     {{ end }}
     <li><span class="sup">bib⁄</span><a href="{{ index $_ 0 }}" target="_blank">{{ index $_ 1 }}</a></li>
    {{ end }}
    </ul></details>
   {{ end }}

   {{ with $other_links }}
    {{ range $i, $_ := $other_links }}
     {{ if eq $i 0 }}
       <details><summary>Other links ({{ len $other_links }}):</summary><ul>
     {{ end }}
     <li><span class="sup">other⁄</span><a href="{{ $_ }}" target="_blank">{{ $_ }}</a></li>
    {{ end }}
    </ul></details>
   {{ end }}

   </details>

  {{ end }}
 {{ end }}
{{ end }}
