<?xml version="1.0" encoding="utf-8"?>
<!-- gestures.xsl · bursts each <gesture> into g/{slug}.html -->
<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"/>

  <!-- Output base: project root. Run with -o pointed at the root,
       or from the root with a processor that resolves against cwd. -->

  <xsl:template match="/gestures">
    <xsl:for-each select="gesture">
      <xsl:variable name="slug" select="ig:slug(time)"/>
      <xsl:variable name="prev" select="preceding-sibling::gesture[1]"/>
      <xsl:variable name="next" select="following-sibling::gesture[1]"/>
      <xsl:variable name="page-url" select="$site || 'g/' || $slug || '.html'"/>
      <xsl:variable name="imgs" select="image/file/string(),
                                        figure[not(file/type = 'video')]/file/name/string()"/>
      <xsl:variable name="vids" select="video"/>
      <xsl:variable name="jl" select="map:merge((
        map{'@context':'https://schema.org',
            '@type':'BlogPosting',
            'headline': ig:when(time),
            'url': $page-url,
            'mainEntityOfPage': $page-url,
            'dateCreated': string(ig:dt(time)),
            'datePublished': string(ig:dt(time)),
            'inLanguage': 'en',
            'author': ig:author-map(),
            'isPartOf': map{'@type':'Blog', 'name':'In the Gesture', 'url': $site},
            'contentLocation': map{'@type':'Place', 'name': string(place)}},
        if (exists($imgs)) then
          map{'image': array{ for $f in $imgs return $site || 'captures/' || $f }}
        else map{},
        if (exists($vids)) then
          map{'video': array{ for $v in $vids return
            map{'@type':'VideoObject',
                'name': string($v/caption),
                'contentUrl': $site || 'captures/' || $v/file,
                'uploadDate': string(ig:dt(time))} }}
        else map{},
        if (exists(toward)) then
          map{'about': array{ for $t in toward return
            map{'@type':'VisualArtwork',
                'name': string($works-doc/works/work[id = $t]/title),
                'url': $site || 'html/works/' || $t || '.html'} }}
        else map{}
      ))"/>
      <xsl:result-document href="g/{$slug}.html">
        <xsl:call-template name="ig:page">
          <xsl:with-param name="title" select="ig:when(time)"/>
          <xsl:with-param name="root" select="'../'"/>
          <xsl:with-param name="gesture" select="."/>
          <xsl:with-param name="jsonld" select="$jl"/>
          <xsl:with-param name="body">
            <article class="gesture">
              <header>
                <h1><time datetime="{time}">{ig:when(time)}</time></h1>
                <p class="place">{place}</p>
              </header>
              <xsl:apply-templates select="* except (time, place)" mode="render">
                <xsl:with-param name="root" select="'../'" tunnel="yes"/>
              </xsl:apply-templates>
            </article>
            <nav class="pager">
              <span class="prev">
                <xsl:if test="$prev">
                  <a href="{ig:slug($prev/time)}.html" rel="prev">← {ig:when($prev/time)}</a>
                </xsl:if>
              </span>
              <span class="next">
                <xsl:if test="$next">
                  <a href="{ig:slug($next/time)}.html" rel="next">{ig:when($next/time)} →</a>
                </xsl:if>
              </span>
            </nav>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:result-document>
    </xsl:for-each>

    <!-- ============ the compiled log ============ -->
    <xsl:result-document href="html/log.html">
      <xsl:call-template name="ig:page">
        <xsl:with-param name="title" select="'Log'"/>
        <xsl:with-param name="root" select="'../'"/>
        <xsl:with-param name="path" select="'html/log.html'"/>
        <xsl:with-param name="jsonld" select="map{
          '@context':'https://schema.org',
          '@type':'Blog',
          'name':'In the Gesture — Log',
          'url': $site || 'html/log.html',
          'author': ig:author-map(),
          'blogPost': array{ for $g in gesture return
            map{'@type':'BlogPosting',
                'headline': ig:when($g/time),
                'url': $site || 'g/' || ig:slug($g/time) || '.html'} }}"/>
        <xsl:with-param name="body">
          <h1>Log</h1>
          <xsl:for-each select="gesture">
            <xsl:sort select="ig:dt(time)" order="descending"/>
            <article class="gesture">
              <header>
                <h2>
                  <a href="../g/{ig:slug(time)}.html">
                    <time datetime="{time}">{ig:when(time)}</time></a>
                </h2>
                <p class="place">{place}</p>
              </header>
              <xsl:apply-templates select="* except (time, place)" mode="log">
                <xsl:with-param name="root" select="'../'" tunnel="yes"/>
              </xsl:apply-templates>
            </article>
          </xsl:for-each>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:result-document>
  </xsl:template>

</xsl:stylesheet>
