<?xml version="1.0" encoding="utf-8"?>
<!-- works.xsl · builds html/works.html and html/{work-id}.html,
     gathering figures and toward-gestures from gestures.xml -->
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ig="https://inthegesture.art/ns"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    exclude-result-prefixes="xs ig map"
    expand-text="yes">

  <xsl:import href="common.xsl"/>

  <xsl:template match="/works">

    <!-- ============ the compiled works page ============ -->
    <xsl:result-document href="html/works.html">
      <xsl:call-template name="ig:page">
        <xsl:with-param name="title" select="'Works'"/>
        <xsl:with-param name="root" select="'../'"/>
        <xsl:with-param name="path" select="'html/works.html'"/>
        <xsl:with-param name="jsonld" select="map{
          '@context':'https://schema.org',
          '@type':'CollectionPage',
          'name':'In the Gesture — Works',
          'url': $site || 'html/works.html',
          'author': ig:author-map(),
          'hasPart': array{ for $w in work return
            map{'@type':'VisualArtwork',
                'name': string($w/title),
                'url': $site || 'html/works/' || $w/id || '.html'} }}"/>
        <xsl:with-param name="body">
          <h1>In the Gesture: The Work</h1>
          <!-- works in progress first, then finished works, newest first -->
          <xsl:for-each select="work">
            <xsl:sort select="exists(ig:finished(.))"/>
            <xsl:sort select="ig:finished(.)" order="descending"/>
            <xsl:variable name="heading" as="element()+">
              <h2><a href="works/{id}.html"><i>{title}</i></a></h2>
              <p class="tombstone">
                <xsl:value-of select="(ig:medium-line(.), ig:dim-line(.),
                                       ig:date-line(., '[Y]'))"
                              separator=" · "/>
              </p>
            </xsl:variable>
            <!-- Thumbnail: the latest image figure toward this work, else
                 the latest image from a gesture toward it — the same pick
                 as the work page's og:image (ig:og-capture). Thumbnails
                 share their capture's filename, made by build.sh. -->
            <xsl:variable name="capture" select="ig:og-capture((), .)"/>
            <article class="work-entry">
              <xsl:choose>
                <xsl:when test="exists($capture)">
                  <figure class="thumb">
                    <a href="works/{id}.html">
                      <img src="../thumbnails/{$capture}" alt="{title}" loading="lazy"/>
                    </a>
                    <figcaption>
                      <xsl:sequence select="$heading"/>
                    </figcaption>
                  </figure>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:sequence select="$heading"/>
                </xsl:otherwise>
              </xsl:choose>
            </article>
          </xsl:for-each>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:result-document>

    <!-- ============ one page per work ============ -->
    <xsl:for-each select="work">
      <xsl:variable name="wid" select="id/string()"/>
      <xsl:variable name="work" select="."/>
      <xsl:variable name="figures"
          select="$gestures-doc/gestures/gesture[figure/work = $wid]"/>
      <xsl:variable name="towards"
          select="$gestures-doc/gestures/gesture[toward = $wid]"/>
      <xsl:variable name="work-url" select="$site || 'html/works/' || $wid || '.html'"/>
      <xsl:variable name="fig-imgs"
          select="$figures/figure[work = $wid][not(file/type = 'video')]/file/name/string()"/>
      <xsl:variable name="jl" select="map:merge((
        map{'@context':'https://schema.org',
            '@type':'VisualArtwork',
            'name': string(title),
            'url': $work-url,
            'creator': ig:author-map(),
            'artMedium': string-join(mediums/medium, ', '),
            'artworkSurface': string-join(supports/support, ', ')},
        (: a finished work carries its date; one in progress says so instead :)
        if (exists(ig:finished(.))) then
          map{'dateCreated': string(ig:finished(.))}
        else map{'creativeWorkStatus': 'Incomplete'},
        if (dimensions) then
          map{'width': map{'@type':'QuantitativeValue',
                           'value': number(dimensions/width),
                           'unitText': string(dimensions/units)},
              'height': map{'@type':'QuantitativeValue',
                            'value': number(dimensions/height),
                            'unitText': string(dimensions/units)}}
        else map{},
        if (exists($fig-imgs)) then
          map{'image': array{ for $f in $fig-imgs return $site || 'captures/' || $f }}
        else map{},
        if (exists($towards)) then
          map{'subjectOf': array{ for $g in $towards return
            map{'@type':'BlogPosting',
                'headline': ig:when($g/time),
                'url': $site || 'g/' || ig:slug($g/time) || '.html'} }}
        else map{}
      ))"/>
      <xsl:result-document href="html/works/{$wid}.html">
        <xsl:call-template name="ig:page">
          <xsl:with-param name="title" select="title/string()"/>
          <xsl:with-param name="root" select="'../../'"/>
          <xsl:with-param name="work" select="."/>
          <xsl:with-param name="jsonld" select="$jl"/>
          <xsl:with-param name="body">
            <article class="work">
              <header>
                <h1><i>{title}</i></h1>
                <p class="tombstone">
                  <xsl:value-of select="(ig:medium-line(.), ig:dim-line(.),
                                         ig:date-line(., '[D] [MNn] [Y]'))"
                                separator=" · "/>
                </p>
              </header>

              <xsl:if test="exists($figures)">
                <section class="figures">
                  <h2>Figures</h2>
                  <xsl:for-each select="$figures">
                    <xsl:for-each select="figure[work = $wid]">
                      <figure>
                        <xsl:choose>
                          <xsl:when test="file/type = 'video'">
                            <video controls="controls" src="../../captures/{file/name}"
                                   preload="metadata"/>
                          </xsl:when>
                          <xsl:otherwise>
                            <img src="../../captures/{file/name}" alt="{$work/title}"/>
                          </xsl:otherwise>
                        </xsl:choose>
                        <figcaption>
                          <a href="../../g/{ig:slug(../time)}.html">
                            <time datetime="{../time}">{ig:when(../time)}</time></a>
                          <xsl:text> · </xsl:text>
                          <xsl:value-of select="../place"/>
                        </figcaption>
                      </figure>
                    </xsl:for-each>
                  </xsl:for-each>
                </section>
              </xsl:if>

              <xsl:if test="exists($towards)">
                <section class="gestures-toward">
                  <h2>Gestures</h2>
                  <xsl:for-each select="$towards">
                    <article class="gesture">
                      <header>
                        <h3>
                          <a href="../../g/{ig:slug(time)}.html">
                            <time datetime="{time}">{ig:when(time)}</time></a>
                        </h3>
                        <p class="place">{place}</p>
                      </header>
                      <xsl:apply-templates select="* except (time, place, toward)"
                                           mode="render">
                        <xsl:with-param name="root" select="'../../'" tunnel="yes"/>
                      </xsl:apply-templates>
                    </article>
                  </xsl:for-each>
                </section>
              </xsl:if>
            </article>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:result-document>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>
