{{ $sizes := unmarshal (readFile "/data/sizes.json") }}

{{ range readDir "/data/art" }}
  {{ if .IsDir }}
    {{ $year := .Name }}
    {{ range readDir ( printf "/data/art/%s" .Name ) }}
      {{ if eq (path.Ext .Name) ".json" }}
        {{ $file := readFile ( printf "/data/art/%s/%s" $year .Name ) }}
        {{ $data := unmarshal $file }}
        {{ $basename := strings.TrimSuffix (path.Ext .Name) .Name }}

        {{ $content := dict
          "mediaType" "text/markdown"
          "value" $data.description
        }}

        {{ $split := split $data.date "-" }}
        {{ $split_len := $split | len }}
        {{ $dates := dict "date" (time.Now) }}
        {{ $exclude_feed := false }}
        {{ if gt $split_len 1 }}
            {{ $dates = dict "date" (time.AsTime $data.date) }}
        {{ else }}
            {{ $dates = dict "date" (time.AsTime (printf "%s-01-01" $data.date)) }}
            {{ $exclude_feed = true }}
        {{ end }}

        {{ $title := "Untitled Artwork" }}
        {{ with $data.title }}
          {{ $title = . }}
        {{ end }}

        {{ $aspect_ratio := math.Div (index $sizes.full $basename).width (index $sizes.full $basename).height }}

        {{ $params := dict
          "slug" $basename
          "alt_text" $data.alt_text
          "artist" $data.artist
          "excludefeed" $exclude_feed
          "arttags" $data.tags
          "characters" $data.characters
          "nsfw" $data.nsfw
          "mastodon_url" $data.mastodon_url
          "pixiv_url" $data.pixiv_url
          "newgrounds_url" $data.newgrounds_url
          "program" $data.program
          "guest" (eq $year "guest")
          "threed" (eq $data.type "3D")
          "animation" (eq $data.type "Animation")
          "orbit" (index $data "camera-orbit")
          "target" (index $data "camera-target")
          "fov" (index $data "camera-fov")
          "comic" (eq $data.type "Comic")
          "pages" $data.pages
          "width" (index $sizes.full $basename).width
          "height" (index $sizes.full $basename).height
          "thumb_width" (index $sizes.thumb $basename).width
          "thumb_height" (index $sizes.thumb $basename).height
          "filename" $basename
          "show_detail_pane" true
          "aspect_ratio" $aspect_ratio
        }}

        {{ $path := $basename }}
        {{ if eq $year "guest" }}
          {{ $path = (printf "guest/%s" $basename) }}
        {{ end }}

        {{ $page := dict
          "slug" $basename
          "content" $content
          "dates" $dates
          "kind" "page"
          "path" $path
          "title" $title
          "params" $params
          "layout" "art-detail"
        }}
        {{ $.AddPage $page }}
      {{ end }}
    {{ end }}

    {{ if not (eq $year "guest") }}
      {{ $page := dict
        "slug" $year
        "kind" "section"
        "path" $year
        "title" $year
        "layout" "year-gallery"
      }}
      {{ $.AddPage $page }}
    {{ end }}
  {{ end }}
{{ end }}
