<!--
  https://mro.name/vefa

  https://w3.org/TR/xslt10
-->
<xsl:stylesheet
  xmlns:cal="http://www.w3.org/2002/12/cal/ical#"
  xmlns:dct="http://purl.org/dc/terms/"
  xmlns:foaf="http://xmlns.com/foaf/0.1/"
  xmlns:math="http://exslt.org/math"
  xmlns:owl="http://www.w3.org/2002/07/owl#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:vefa="http://vefalod.darknet.mro.name/2026#"
  xmlns:xs="http://www.w3.org/2001/XMLSchema#"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  extension-element-prefixes="math"
  version="1.0">

<xsl:output
  method="html"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  indent="yes"/>

<!-- replace linefeeds with <br> tags -->
<xsl:template name="linefeed2br">
  <xsl:param name="string" select="''"/>
  <xsl:param name="pattern" select="'&#10;'"/>
  <xsl:choose>
    <xsl:when test="contains($string, $pattern)">
      <xsl:value-of select="substring-before($string, $pattern)"/><br class="br"/><xsl:comment> Why do we see 2 br on Safari and output/@method=html here? http://purl.mro.name/safari-xslt-br-bug </xsl:comment>
      <xsl:call-template name="linefeed2br">
        <xsl:with-param name="string" select="substring-after($string, $pattern)"/>
        <xsl:with-param name="pattern" select="$pattern"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="calculate-day-of-the-week">
  <!-- https://www.oreilly.com/library/view/xslt-cookbook/0596003722/ch03s02.html -->
  <xsl:param name="date-time"/>
  <xsl:param name="date" select="substring-before($date-time,'T')"/>
  <xsl:param name="year" select="substring-before($date,'-')"/>
  <xsl:param name="month" select="substring-before(substring-after($date,'-'),'-')"/>
  <xsl:param name="day" select="substring-after(substring-after($date,'-'),'-')"/>
  <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
  <xsl:variable name="y" select="$year - $a"/>
  <xsl:variable name="m" select="$month + 12 * $a - 2"/>
  <xsl:value-of select="($day + $y + floor($y div 4) - floor($y div 100) + floor($y div 400) + floor((31 * $m) div 12)) mod 7"/>
</xsl:template>

<xsl:template name="human_time">
  <xsl:param name="time">-</xsl:param>
  <xsl:variable name="wday">
    <xsl:call-template name="calculate-day-of-the-week">
      <xsl:with-param name="date-time" select="$time"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="0 = $wday">So</xsl:when>
    <xsl:when test="1 = $wday">Mo</xsl:when>
    <xsl:when test="2 = $wday">Di</xsl:when>
    <xsl:when test="3 = $wday">Mi</xsl:when>
    <xsl:when test="4 = $wday">Do</xsl:when>
    <xsl:when test="5 = $wday">Fr</xsl:when>
    <xsl:when test="6 = $wday">Sa</xsl:when>
    <xsl:otherwise>?</xsl:otherwise>
  </xsl:choose>
  <xsl:text>, </xsl:text>
  <xsl:value-of select="substring($time, 9, 2)"/><xsl:text>. </xsl:text>
  <xsl:variable name="month" select="substring($time, 6, 2)"/>
  <xsl:choose>
    <xsl:when test="'01' = $month">Jan</xsl:when>
    <xsl:when test="'02' = $month">Feb</xsl:when>
    <xsl:when test="'03' = $month">Mär</xsl:when>
    <xsl:when test="'04' = $month">Apr</xsl:when>
    <xsl:when test="'05' = $month">Mai</xsl:when>
    <xsl:when test="'06' = $month">Jun</xsl:when>
    <xsl:when test="'07' = $month">Jul</xsl:when>
    <xsl:when test="'08' = $month">Aug</xsl:when>
    <xsl:when test="'09' = $month">Sep</xsl:when>
    <xsl:when test="'10' = $month">Okt</xsl:when>
    <xsl:when test="'11' = $month">Nov</xsl:when>
    <xsl:when test="'12' = $month">Dez</xsl:when>
    <xsl:otherwise>?</xsl:otherwise>
  </xsl:choose><xsl:text> </xsl:text>
  <xsl:value-of select="substring($time, 1, 4)"/><xsl:text> </xsl:text>
  <xsl:value-of select="substring($time, 12, 5)"/><!-- xsl:text> Uhr</xsl:text -->
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates select="rdf:RDF/rdf:Description[@rdf:about]"/>
</xsl:template>

<xsl:template match="rdf:Description[@rdf:about]">
  <xsl:variable name="title" select="dct:title"/>
  <xsl:variable name="rfc3339" select="cal:dtstart"/>
  <html xmlns="http://www.w3.org/1999/xhtml" lang="de">
    <head>
      <link rel="alternate" type="text/html" href="about.html"/>
      <link rel="alternate" type="text/turtle" href="about.ttl"/>
      <link rel="alternate" type="application/rdf+xml" href="about.rdf"/>
      <link rel="source" href="{dct:source/@rdf:resource}"/>
      <link rel="stylesheet" href="style.css"/>
      <link rel="up" href=".."/>
      <meta name="keywords" content="Niederschrift Sitzung Stadtrat Traunstein" />
      <title><xsl:value-of select="$title"/></title>
    </head>
    <body>
      <p role="banner"><img alt="Wappen" src="../../../../../../wappen.svg"/>unverbindliche
        <a href="https://de.wikipedia.org/wiki/Linked_Open_Data">Linked Open Governmental Data</a>
        Abschrift (<a href="about.ttl">Rohdaten</a> bzw.
        <a href="{dct:source/@rdf:resource}">Original</a> und <a href="https://mro.name/vefalod">Code</a>)
        der</p>
      <div role="main">
      <h1><xsl:value-of select="$title"/></h1>
      <h2 data-dtstart="{$rfc3339}"><xsl:call-template name="human_time"><xsl:with-param name="time" select="$rfc3339"/></xsl:call-template></h2>
      <p><xsl:value-of select="cal:location"/></p>
      <h2>Anwesenheit</h2>
      <h3>Vorsitz</h3>
      <ul>
  <xsl:for-each select="vefa:anwesend/foaf:Person">
        <xsl:sort select="foaf:name"/>
        <li><xsl:value-of select="foaf:name"/></li>
  </xsl:for-each>
      </ul>
  <xsl:for-each select="vefa:anwesend/foaf:Group">
       <h3><xsl:value-of select="foaf:name"/></h3>
       <ul>
    <xsl:for-each select="foaf:member/foaf:Person">
         <xsl:sort select="foaf:name"/>
         <li><xsl:value-of select="foaf:name"/> (<xsl:value-of select="vefa:role"/>)</li>
    </xsl:for-each>
       </ul>
  </xsl:for-each>
      <h3>Entschuldigt / abwesend</h3>
      <ul>
  <xsl:for-each select="vefa:entschuldigt//foaf:Person">
        <xsl:sort select="foaf:name"/>
        <li><xsl:value-of select="foaf:name"/> (<xsl:value-of select="vefa:role"/>)</li>
  </xsl:for-each>
      </ul>
      <h2>Öffentliche Sitzung</h2>
  <xsl:for-each select="vefa:protokoll/rdf:Description/vefa:topic/rdf:Description">
    <h3 id="top:{vefa:top}">Top <xsl:value-of select="vefa:top"/>: <xsl:value-of select="dct:title"/></h3>
    <p>
      <xsl:call-template name="linefeed2br">
        <xsl:with-param name="string" select="dct:description"/>
      </xsl:call-template>
    </p>
  </xsl:for-each>
    </div>
      <p role="contentinfo">
        <a rel="privacy" href="pri">privacy</a>,
        <a rel="license" href="lic">license</a>,
        <a rel="terms-of-service" href="ter">terms-of-service</a>
      </p>
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>
