<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:output method="xml"
              version="1.0"
              encoding="utf-8"/>

  <xsl:variable name="version" select="'$Id: talk.xsl,v 1.10 2024/11/13 17:37:52 flo Exp $'"/>
  <xsl:param name="page-width" select="'32cm'"/>
  <xsl:param name="page-height" select="'24cm'"/>
  <xsl:param name="title-slide-image" select="'./socrates.jpg'"/>

  <xsl:attribute-set name="hl-tag">
    <xsl:attribute name="color">#8f0075</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="hl-attr">
    <xsl:attribute name="color">#005e8b</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="hl-string">
    <xsl:attribute name="color">#3548cf</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="hl-indent">
    <xsl:attribute name="start-indent">2em</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="hl-ignore">
    <xsl:attribute name="color">#666</xsl:attribute>
  </xsl:attribute-set>

  <xsl:template match="/">
    <fo:root xml:lang="en" xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="sans-serif">
      <xsl:call-template name="pageLayout"/>
      <fo:declarations>
        <x:xmpmeta xmlns:x="adobe:ns:meta/">
          <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
            <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
              <dc:title><xsl:value-of select="/talk/title"/></dc:title>
              <dc:creator><xsl:value-of select="/talk/author"/></dc:creator>
            </rdf:Description>
          </rdf:RDF>
        </x:xmpmeta>
      </fo:declarations>
      <fo:bookmark-tree>
        <xsl:for-each select="talk/content/slide[not(@cont)]">
          <fo:bookmark internal-destination="{generate-id(.)}">
            <fo:bookmark-title><xsl:value-of select="title"/></fo:bookmark-title>
          </fo:bookmark>
        </xsl:for-each>
      </fo:bookmark-tree>
      <xsl:call-template name="title-slide"/>
      <xsl:apply-templates/>
    </fo:root>
  </xsl:template>

  <xsl:template name="pageLayout">
    <fo:layout-master-set>
      <fo:simple-page-master
          master-name="pagemaster"
          page-width="{$page-width}"
          page-height="{$page-height}">
        <fo:region-body
            region-name="body"
            margin-left="1cm"
            margin-top="3cm"
            margin-right="1cm"
            margin-bottom="1cm"/>
        <fo:region-before region-name="header" extent="3cm"/>
        <fo:region-after region-name="footer" extent="1cm"/>
      </fo:simple-page-master>
      <fo:simple-page-master
          master-name="title-slide"
          page-width="{$page-width}"
          page-height="{$page-height}">
        <fo:region-body region-name="body"/>
        <fo:region-after region-name="footer" extent="1.5cm"/>
      </fo:simple-page-master>
    </fo:layout-master-set>
  </xsl:template>

  <xsl:template name="title-slide">
    <fo:page-sequence master-reference="title-slide">
      <fo:static-content flow-name="footer">
        <fo:block-container color="#333"
                            font-size="14pt"
                            width="40em"
                            padding-bottom="10pt"
                            font-family="monospace"
                            background-color="#eeeeeeaa">
          <fo:block start-indent="1em">
            <xsl:value-of select="/talk/version"/>
          </fo:block>
          <fo:block start-indent="1em">
            <xsl:value-of select="$version"/>
          </fo:block>
        </fo:block-container>
      </fo:static-content>
      <fo:flow flow-name="body">
        <fo:block-container
            absolute-position="absolute"
            top="0" left="0" z-index="-1"
            width="{$page-width}" height="{$page-height}"
            background-repeat="no-repeat"
            background-position-horizontal="center"
            background-image="url('{$title-slide-image}')">
          <fo:block-container
              background-color="#eee"
              color="#333"
              width="14cm"
              margin-top="1cm"
              padding="5mm">
            <fo:block margin-left="5mm" font-size="28pt" font-weight="bold">
              <xsl:value-of select="/talk/title"/>
            </fo:block>
            <fo:block margin-left="5mm" font-size="18pt"  margin-top="10mm">
              <xsl:value-of select="/talk/author"/>
            </fo:block>
            <fo:block margin-left="5mm" font-size="16pt" margin-top="10mm">
              <xsl:value-of select="/talk/date"/>
            </fo:block>
          </fo:block-container>
        </fo:block-container>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>

  <xsl:template name="copy-attributes">
    <xsl:for-each select="@*[name() != 'ref' and name() != 'vertical-rule']">
      <xsl:attribute name="{name()}">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="slide">
    <fo:page-sequence master-reference="pagemaster" id="{generate-id(.)}">
      <xsl:choose>
        <xsl:when test="@cont = 'true'">
          <xsl:call-template name="slide-title">
            <xsl:with-param name="title" select="preceding-sibling::slide[not(@cont)][1]/title"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="slide-title">
            <xsl:with-param name="title" select="title"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>

      <fo:static-content flow-name="footer">
        <fo:block margin-right="2em" text-align="right" color="#666">
          <fo:page-number/>
        </fo:block>
      </fo:static-content>

      <fo:flow flow-name="body" color="#333" font-size="24pt">
        <fo:block-container>
          <xsl:if test="@centering = 'true'">
            <xsl:attribute name="text-align">
              <xsl:value-of select="'center'"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:if test="not(*[not(self::title)])">
            <fo:block text-align="center" color="red">TODO</fo:block>
          </xsl:if>
          <xsl:apply-templates/>
        </fo:block-container>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>

  <xsl:template name="slide-title">
    <xsl:param name="title"/>
    <fo:title>
      <xsl:value-of select="$title"/>
    </fo:title>
    <fo:static-content flow-name="header">
      <fo:block-container padding="5mm" background-color="#466766" color="#eee">
        <fo:block font-size="24pt" font-weight="bold" margin-left="5mm" text-align="center">
          <xsl:value-of select="$title"/>
        </fo:block>
      </fo:block-container>
    </fo:static-content>
  </xsl:template>

  <xsl:template match="p">
    <fo:block>
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="span">
    <fo:inline>
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="b">
    <fo:inline font-weight="bold">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="me">
    <fo:inline font-style="italic" color="#8f0075">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="emph | hyperbole">
    <fo:inline font-style="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="rm">
    <fo:inline font-family="monospace">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  
  <xsl:template match="ul | ol">
    <fo:list-block margin-left="10pt">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="ul/li">
    <fo:list-item line-height="1.6em">
      <fo:list-item-label end-indent="label-end()">
        <fo:block>•</fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <xsl:choose>
          <xsl:when test="p">
            <xsl:apply-templates select="p"/>
          </xsl:when>
          <xsl:otherwise>
            <fo:block alignment-baseline="middle">
              <xsl:apply-templates/>
            </fo:block>
          </xsl:otherwise>
        </xsl:choose>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>

  <xsl:template match="ol/li">
    <fo:list-item line-height="1.6em">
      <fo:list-item-label end-indent="label-end()">
        <fo:block font-weight="bold"><xsl:value-of select="count(preceding-sibling::li) + 1"/>.</fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <xsl:choose>
          <xsl:when test="p">
            <xsl:apply-templates select="p"/>
          </xsl:when>
          <xsl:otherwise>
            <fo:block alignment-baseline="middle">
              <xsl:apply-templates/>
            </fo:block>
          </xsl:otherwise>
        </xsl:choose>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>

  <xsl:template match="a">
    <fo:basic-link external-destination="{@href}"
                   text-decoration="underline"
                   color="#466766">
      <xsl:apply-templates/>
    </fo:basic-link>
  </xsl:template>

  <xsl:template match="definition">
    <fo:block margin="10pt">
      <fo:inline font-weight="bold" margin-right="1em">Def.</fo:inline>
      <xsl:apply-templates select="label"/>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="example">
    <fo:block margin="10pt">
      <fo:inline font-weight="bold" margin-right="1em">Example</fo:inline>
      <xsl:apply-templates select="label"/>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="definition/label | example/label">
    <xsl:text>(</xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>)</xsl:text>
  </xsl:template>

  <xsl:template match="remark">
    <fo:block background-color="#eee" padding="5pt">
      <fo:inline background-color="#8f0075" color="#eee" font-weight="bold" padding="5pt" padding-start="15pt" padding-end="15pt" margin-right="20pt">!</fo:inline>&#160;
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="tag-name">
    <fo:inline xsl:use-attribute-sets="hl-tag">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="attribute-name">
    <fo:inline xsl:use-attribute-sets="hl-attr">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="attribute-value">
    <fo:inline xsl:use-attribute-sets="hl-string">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="ignore">
    <fo:inline xsl:use-attribute-sets="hl-ignore">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="code">
    <fo:block-container text-align="left" font-family="monospace">
      <xsl:call-template name="copy-attributes"/>
      <xsl:apply-templates select="*" mode="verbatim"/>
    </fo:block-container>
  </xsl:template>

  <xsl:template match="node()" mode="verbatim">
    <fo:block xsl:use-attribute-sets="hl-indent">
      <xsl:text>&lt;</xsl:text><fo:inline xsl:use-attribute-sets="hl-tag"><xsl:value-of select="name()"/></fo:inline>
      <xsl:for-each select="@*">
        <xsl:text> </xsl:text>
        <fo:inline xsl:use-attribute-sets="hl-attr"><xsl:value-of select="name()"/></fo:inline>
        <xsl:text>=</xsl:text>
        <fo:inline xsl:use-attribute-sets="hl-string"><xsl:text>"</xsl:text><xsl:value-of select="."/><xsl:text>"</xsl:text></fo:inline>
      </xsl:for-each>
      <xsl:choose>
        <xsl:when test="node() | text()">
          <xsl:text>&gt;</xsl:text>
          <fo:inline>
            <xsl:apply-templates mode="verbatim"/>
          </fo:inline>
          <xsl:text>&lt;/</xsl:text><fo:inline xsl:use-attribute-sets="hl-tag"><xsl:value-of select="name()"/></fo:inline><xsl:text>&gt;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>/&gt;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </fo:block>
  </xsl:template>

  <xsl:template match="text()" mode="verbatim">
    <fo:block xsl:use-attribute-sets="hl-indent">
      <xsl:value-of select="."/>
    </fo:block>
  </xsl:template>

  <xsl:template match="two-column">
    <fo:table>
      <xsl:call-template name="copy-attributes"/>
      <fo:table-column column-width="50%" />
      <fo:table-column column-width="50%" />
      <fo:table-body>
        <fo:table-row>
          <xsl:apply-templates/>
        </fo:table-row>
      </fo:table-body>
    </fo:table>
  </xsl:template>

  <xsl:template match="column">
    <fo:table-cell>
      <xsl:if test="preceding-sibling::column">
        <xsl:attribute name="start-indent">30pt</xsl:attribute>
        <xsl:if test="ancestor::two-column[@vertical-rule]">
          <xsl:attribute name="border-left">solid 3pt #333</xsl:attribute>
          <xsl:attribute name="height">23em</xsl:attribute>
        </xsl:if>
      </xsl:if>
      <xsl:apply-templates/>
    </fo:table-cell>
  </xsl:template>

  <xsl:template match="syllogism">
    <fo:block-container>
      <xsl:apply-templates select="premise"/>
      <fo:block>
        <fo:leader leader-pattern="rule" leader-length="100%" rule-thickness="3pt"/>
      </fo:block>
      <xsl:apply-templates select="consequent"/>
    </fo:block-container>
  </xsl:template>

  <xsl:template match="syllogism/premise">
    <fo:block>
      <xsl:if test="@type = 'major'">
        <xsl:attribute name="font-weight">bold</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="syllogism/consequent">
    <fo:block font-style="italic">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="footnote">
    <xsl:variable name="id" select="count(preceding-sibling::footnote)"/>
    <fo:footnote>
      <fo:inline baseline-shift="super" font-size="60%">
        <xsl:value-of select="$id"/>
      </fo:inline>
      <fo:footnote-body font-size="80%">
        <fo:block font-family="sans-serif" text-align="left">
          <fo:inline>
            <xsl:text>[</xsl:text><xsl:value-of select="$id"/><xsl:text>]&#xA0;</xsl:text>
          </fo:inline>
          <xsl:apply-templates/>
        </fo:block>
      </fo:footnote-body>
    </fo:footnote>
  </xsl:template>

  <xsl:template match="cip">
    <fo:external-graphic padding-start="1em" src="./cip.gif"/>
  </xsl:template>

  <xsl:template match="argument">
    <fo:table margin-left="1em" margin-top="5em">
      <xsl:call-template name="copy-attributes"/>
      <fo:table-column column-width="50%" />
      <fo:table-column column-width="50%" />
      <fo:table-body>
        <xsl:apply-templates/>
      </fo:table-body>
    </fo:table>
  </xsl:template>
  
  <xsl:template match="objection | refutation | opinion">
    <fo:table-row >
      <fo:table-cell>
        <fo:block font-weight="bold" padding-end="2em">
          <xsl:choose>
            <xsl:when test="self::objection">
              <xsl:text>AD </xsl:text><xsl:value-of select="@nr"/><xsl:text> SIC PROCEDITUR.</xsl:text>
            </xsl:when>
            <xsl:when test="self::refutation">
              <xsl:text>SED CONTRA,</xsl:text>
            </xsl:when>
            <xsl:when test="self::opinion">
              <xsl:text>RESPONDEO</xsl:text>
            </xsl:when>
          </xsl:choose>
        </fo:block>
      </fo:table-cell>
      <fo:table-cell padding-bottom="3em">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>

  <xsl:template match="question | answer">
    <fo:block margin-bottom="1em">
      <fo:inline font-weight="bold" margin-right="10em">
        <xsl:choose>
          <xsl:when test="self::question"><xsl:text>Q</xsl:text></xsl:when>
          <xsl:when test="self::answer"><xsl:text>A</xsl:text></xsl:when>
        </xsl:choose>: 
      </fo:inline>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="icon">
    <fo:block absolute-position="absolute">
      <fo:external-graphic>
        <xsl:call-template name="copy-attributes"/>
      </fo:external-graphic>
    </fo:block>
  </xsl:template>

  <xsl:template match="copy-of">
    <xsl:apply-templates select="//*[@ref = current()/@select]"/>
  </xsl:template>

  <xsl:template match="fo">
    <xsl:copy-of select="*"/>
  </xsl:template>

  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>