{{ $currentNode := . }}

{{ $debug := (slice) }}

{{ $pPath := "" }}
{{ range $p := .Site.RegularPages }}
 {{ $pPath = printf "/%s" $p.File }}
 {{ $pageDir := "" }}
 {{ with $p.File }}
  {{ $pageDir = (substr .Dir 0 -1)}}
 {{ end }}

 {{ $gp := $.GetPage $pPath }}
 {{ $gpPath := printf "/%s" $gp.File }}
 {{ range $k, $v := $gp.Params }}
  {{ with in $k "has_" }}
   {{ $s := index (split $k "has_") 1 }}
   {{ range $sin,$plur := $currentNode.Site.Data.sandpointsnamegraph }}
    {{ if eq $s $plur }}
     {{ range index $gp.Params $k }}
      {{ $cp := $.GetPage (printf "/%s/%s" $sin .) }}
      {{ $cpPath := printf "/%s" $cp.File }}
      {{ range $kcp, $vcp := $cp.Params }}
       {{ with in $kcp "has_" }}
        {{ $scp := index (split $kcp "has_") 1 }}
        {{ range $sincp,$plurcp := $currentNode.Site.Data.sandpointsnamegraph }}
         {{ if eq $scp $plurcp }}

          {{ $.Scratch.SetInMap $gpPath "depth" "core" }}
          {{ $.Scratch.SetInMap $gpPath "child_depth" "mantle" }}
          {{ $.Scratch.SetInMap $gpPath "has" $s }}

          {{ with $gp.File }}
           {{ $core := substr .Dir 0 -1 }}
           {{ if isset ($.Scratch.Get $gpPath) "core" }}
            {{ $.Scratch.SetInMap $gpPath "core" (uniq ((index ($.Scratch.Get $gpPath) "core") | append $core)) }}
           {{ else }}
            {{ $.Scratch.SetInMap $gpPath "core" (slice $core) }}
           {{ end }}
           {{ if isset ($.Scratch.Get $cpPath) "core" }}
            {{ $.Scratch.SetInMap $cpPath "core" (uniq ((index ($.Scratch.Get $cpPath) "core") | append $core)) }}
           {{ else }}
            {{ $.Scratch.SetInMap $cpPath "core" (slice $core) }}
           {{ end }}
          {{ end }}

          {{ with $cp.File }}
           {{ $mantle := substr .Dir 0 -1 }}
           {{ if isset ($.Scratch.Get $gpPath) "mantle" }}
            {{ $.Scratch.SetInMap $gpPath "mantle" (uniq ((index ($.Scratch.Get $gpPath) "mantle") | append $mantle)) }}
           {{ else }}
            {{ $.Scratch.SetInMap $gpPath "mantle" (slice $mantle) }}
           {{ end }}
           {{ if isset ($.Scratch.Get $cpPath) "mantle" }}
             {{ $.Scratch.SetInMap $cpPath "mantle" (uniq ((index ($.Scratch.Get $cpPath) "mantle") | append $mantle)) }}
           {{ else }}
            {{ $.Scratch.SetInMap $cpPath "mantle" (slice $mantle) }}
           {{ end }}
          {{ end }}

          {{ with $.Scratch.Get $gpPath }}
           {{ if .has_children }}
               {{ $hasChildren := index . "has_children" | append $cp }}
               {{ $.Scratch.SetInMap $gpPath "has_children" (uniq $hasChildren) }}
           {{ else }}
               {{ $.Scratch.SetInMap $gpPath "has_children" (slice $cp) }}
           {{ end }}
          {{ end }}

          {{ $.Scratch.SetInMap $cpPath "depth" "mantle" }}
          {{ $.Scratch.SetInMap $cpPath "child_depth" "crust" }}
          {{ $.Scratch.SetInMap $cpPath "plural" $plur }}
          {{ $.Scratch.SetInMap $cpPath "has" $scp }}

          {{ if isset $cp.Params $kcp }}
           {{ $.Scratch.SetInMap $cpPath "has_children" (slice) }}
           {{ if isset ($.Scratch.Get $cpPath) "is_in" }}
            {{ $isincore := index ($.Scratch.Get $cpPath) "is_in" | append $gp}}
            {{ $.Scratch.SetInMap $cpPath "is_in" (uniq $isincore) }}
           {{ else }}
            {{ $.Scratch.SetInMap $cpPath "is_in" (slice $gp) }}
           {{ end }}
          {{ end }}

          {{ range index $cp.Params $kcp }}
           {{ $gcp := $.GetPage (printf "/%s/%s" $sincp .) }}
           {{ $gcpPath := printf "/%s" $gcp.File }}

           {{ with $.Scratch.Get $cpPath }}
            {{ if .has_children }}
                {{ $hasGChildren := index . "has_children" | append $gcp }}
                {{ $.Scratch.SetInMap $cpPath "has_children" (uniq $hasGChildren) }}
            {{ else }}
                {{ $.Scratch.SetInMap $cpPath "has_children" (slice $gcp) }}
            {{ end }}
           {{ end }}

           {{ $.Scratch.SetInMap $gcpPath "depth" "crust" }}
           {{ $.Scratch.SetInMap $gcpPath "plural" $plurcp }}
           {{ $.Scratch.SetInMap $gcpPath "in" (slice $sin $plur) }}

           {{ with $gp.File }}
            {{ $core := substr .Dir 0 -1 }}
            {{ if isset ($.Scratch.Get $gcpPath) "core" }}
             {{ $.Scratch.SetInMap $gcpPath "core" (uniq ((index ($.Scratch.Get $gcpPath) "core") | append $core)) }}
            {{ else }}
             {{ $.Scratch.SetInMap $gcpPath "core" (slice $core) }}
            {{ end }}
           {{ end }}

           {{ with $cp.File }}
            {{ $mantle := substr .Dir 0 -1 }}
            {{ if isset ($.Scratch.Get $gcpPath) "mantle" }}
             {{ $.Scratch.SetInMap $gcpPath "mantle" (uniq ((index ($.Scratch.Get $gcpPath) "mantle") | append $mantle)) }}
            {{ else }}
             {{ $.Scratch.SetInMap $gcpPath "mantle" (slice $mantle) }}
            {{ end }}
           {{ end }}

           {{ with $gcp.File }}
            {{ $crust := substr .Dir 0 -1 }}
             {{ if isset ($.Scratch.Get $gpPath) "crust" }}
              {{ $.Scratch.SetInMap $gpPath "crust" (uniq ((index ($.Scratch.Get $gpPath) "crust") | append $crust)) }}
             {{ else }}
              {{ $.Scratch.SetInMap $gpPath "crust" (slice $crust) }}
             {{ end }}
              {{ if isset ($.Scratch.Get $cpPath) "crust" }}
              {{ $.Scratch.SetInMap $cpPath "crust" (uniq ((index ($.Scratch.Get $cpPath) "crust") | append $crust)) }}
             {{ else }}
              {{ $.Scratch.SetInMap $cpPath "crust" (slice $crust) }}
             {{ end }}
             {{ if isset ($.Scratch.Get $gcpPath) "crust" }}
              {{ $.Scratch.SetInMap $gcpPath "crust" (uniq ((index ($.Scratch.Get $gcpPath) "crust") | append $crust)) }}
             {{ else }}
              {{ $.Scratch.SetInMap $gcpPath "crust" (slice $crust) }}
             {{ end }}
           {{ end }}

           {{ with $.Scratch.Get $gcpPath }}
            {{ if .is_in }}
             {{ $isinmantle := index . "is_in" | append $cp}}
             {{ $.Scratch.SetInMap $gcpPath "is_in" (uniq $isinmantle) }}
            {{ else }}
             {{ $.Scratch.SetInMap $gcpPath "is_in" (slice $cp) }}
            {{ end }}
           {{ end }}
          {{ end }}
         {{ end }}
        {{ end }}
       {{ end }}
      {{ end }}
     {{ end }}
    {{ end }}
   {{ end }}
  {{ end }}
 {{ end }}

{{ $.Scratch.SetInMap $pPath "debug" (printf "%#v" $debug) }}
{{ end }}

{{ return $.Scratch }}
