<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/pool/xslt_ht.xslt" type="application/xml"?>
<xsl:stylesheet
  xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
  xmlns:db = "http://docbook.org/ns/docbook"
  xmlns:d = "http://herbaer.de/xmlns/20051201/doc"
  xmlns:ht = "http://www.w3.org/1999/xhtml"
  xmlns:xl = "http://www.w3.org/1999/xlink"
  xmlns:ic = "http://herbaer.de/xmlns/20121016/inclcomp"
  xmlns:co = "http://herbaer.de/xmlns/20121016/components"
  xmlns = "http://docbook.org/ns/docbook"
  exclude-result-prefixes = "d db ic co ht xl"
  version = "1.0"
>

<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>index_comp.xslt</title>
  <subtitle>Komponenten (Lesebeiträge) einfügen</subtitle>
  <date>2012-10-16</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para>
Benannte Vorlagen <d:literal>txt.filename</d:literal> u. a.
</d:para>
<xsl:include href="/pool/txt.xslt"/>

<d:para>Präfix für Lese-Include-Pfade</d:para>
<xsl:param name="p_incprf" select="'/media/herbaer/kleider/web/components/'"/>

<d:para>Präfix für lokale Datei-Pfade</d:para>
<xsl:param name="p_localprf" select="'/media/herbaer/kleider/web/docroot/'"/>

<d:para>
Das Präfix der lokalen Datei-Pfade wird durch das Server-Präfix ersetzt
</d:para>
<xsl:param name="p_srvprf" select="''"/>

<d:para>Die (relative) URL des Website-Dokuments</d:para>
<xsl:param name="p_selfurl" select="'/index.xhtml'"/>

<d:para>
Dateiname der Index-Datei in Verzeichnissen
</d:para>
<xsl:param name="p_indexfile" select="'index.xhtml'"/>

<d:para>
Kennung der Sprache der Komponenten
</d:para>
<xsl:param name="p_lang">
  <xsl:variable name="l">
    <xsl:choose>
      <xsl:when test="/*/@xml:lang">
        <xsl:value-of select="/*/@xml:lang"/>
      </xsl:when>
      <xsl:when test="/*/@lang">
        <xsl:value-of select="/*/@lang"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>de</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:choose>
    <xsl:when test="contains ($l, '-')">
      <xsl:value-of select="substring-before ($l, '-')"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$l"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:param>

<d:para>
Verzeichnis-Trenner und Name der Index-Datei
</d:para>
<xsl:variable name="g_indexsfx" select="concat ('/', $p_indexfile)"/>

<xsl:output method="xml" indent="yes" encoding="utf-8"/>

<d:para>Die Wurzel</d:para>
<xsl:template match="/">
  <xsl:apply-templates select="*"/>
</xsl:template>

<d:para>Lesebeiträge einfügen</d:para>
<xsl:template match="ic:include">
  <xsl:apply-templates select="document(concat ($p_incprf, @href))/co:components/co:*">
    <xsl:with-param name="idbase" select="@idbase"/>
    <xsl:with-param name="sort" select="@sort"/>
  </xsl:apply-templates>
</xsl:template>

<d:para>Ein Verzeichnis mit Lesebeiträgen</d:para>
<xsl:template match="co:dir">
  <xsl:param name="idbase"/>
  <xsl:param name="sort"/>
  <xsl:variable name="effsort">
    <xsl:choose>
      <xsl:when test="@sort">
        <xsl:value-of select="@sort"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$sort"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="nid">
    <xsl:apply-templates select="." mode="id">
      <xsl:with-param name="idbase" select="$idbase"/>
    </xsl:apply-templates>
  </xsl:variable>
  <xsl:variable name="ixfile" select="co:file [ contains (., $g_indexsfx) ]"/>
  <section>
    <xsl:attribute name="xml:id">
      <xsl:value-of select="$nid"/>
    </xsl:attribute>
    <xsl:choose>
      <xsl:when test="$ixfile">
        <xsl:apply-templates select="$ixfile" mode="dirindex"/>
      </xsl:when>
      <xsl:otherwise>
        <title>
          <xsl:apply-templates select="." mode="title"/>
        </title>
        <simplelist>
          <xsl:choose>
            <xsl:when test="$effsort = 'revers'">
              <xsl:apply-templates select="co:dir | co:file" mode="listitem">
                <xsl:sort select="position()" data-type="number" order="descending"/>
                <xsl:with-param name="idbase" select="$nid"/>
              </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="co:dir | co:file" mode="listitem">
                <xsl:with-param name="idbase" select="$nid"/>
              </xsl:apply-templates>
            </xsl:otherwise>
          </xsl:choose>
        </simplelist>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="$effsort = 'revers'">
        <xsl:apply-templates select="co:dir | co:file">
          <xsl:sort select="position()" data-type="number" order="descending"/>
          <xsl:with-param name="idbase" select="$nid"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="co:dir | co:file">
          <xsl:with-param name="idbase" select="$nid"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </section>
</xsl:template>

<d:para>
Listeneintrag zu einer Datei oder einem Verzeichnis
</d:para>
<xsl:template match="co:dir | co:file" mode="listitem">
  <xsl:param name="idbase"/>
  <xsl:variable name="id">
    <xsl:apply-templates select="." mode="id">
      <xsl:with-param name="idbase" select="$idbase"/>
    </xsl:apply-templates>
  </xsl:variable>
  <member>
    <link xl:href="{$p_selfurl}#{$id}">
      <xsl:apply-templates select="." mode="title"/>
    </link>
  </member>
</xsl:template>

<d:para>
Der Titel zu einem Verzeichnis
</d:para>
<xsl:template match="co:dir" mode="title">
  <xsl:variable name="ixfile" select="co:file [ contains (., $g_indexsfx) ]"/>
  <xsl:choose>
    <xsl:when test="$ixfile">
      <xsl:apply-templates select="$ixfile" mode="title_nodate"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>Verzeichnis </xsl:text>
      <xsl:call-template name="txt.filename">
        <xsl:with-param name="txt" select="@name"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Der Titel zu einer Datei
</d:para>
<xsl:template match="co:file" mode="title">
  <xsl:value-of select="document (concat($p_localprf, ., '.', $p_lang, '.'))/ht:html/ht:head/ht:title"
  />
</xsl:template>

<d:para>
Der Titel zu einer Datei ohne Datum
</d:para>
<xsl:template match="co:file" mode="title_nodate">
  <xsl:variable
    name = "t"
    select =
    "document (concat($p_localprf, ., '.', $p_lang, '.'))/ht:html/ht:head/ht:title"
  />
  <xsl:variable name="tt" select="translate ($t, '123456789', '000000000')"/>
  <xsl:choose>
    <xsl:when test="contains ($tt, '(00.00.0000)')">
      <xsl:variable name="p" select="string-length (substring-before ($tt, '(00.00.0000)'))"
      />
      <xsl:value-of select="normalize-space (concat (substring ($t, 1, $p), substring ($t, $p + 13)))"
      />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$t"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>ID zu einem Verzeichnis</d:para>
<xsl:template match="co:dir" mode="id">
  <xsl:param name="idbase"/>
  <xsl:value-of select="concat ($idbase, '.')"/>
  <xsl:call-template name="txt.beforefirstchar">
    <xsl:with-param name="txt">
      <xsl:call-template name="txt.filename">
        <xsl:with-param name="txt" select="@name"/>
      </xsl:call-template>
    </xsl:with-param>
    <xsl:with-param name="delim" select="'_.'"/>
  </xsl:call-template>
</xsl:template>

<d:para>ID zu einer Datei</d:para>
<xsl:template match="co:file" mode="id">
  <xsl:param name="idbase"/>
  <xsl:value-of select="concat ($idbase, '.')"/>
  <xsl:call-template name="txt.beforefirstchar">
    <xsl:with-param name="txt">
      <xsl:call-template name="txt.filename"/>
    </xsl:with-param>
    <xsl:with-param name="delim" select="'_.'"/>
  </xsl:call-template>
</xsl:template>

<d:para>Ein Lesebeitrag</d:para>
<xsl:template match="co:file">
  <xsl:param name="idbase"/>
  <xsl:if test="not (contains (., $g_indexsfx))">
    <section>
      <xsl:attribute name="xml:id">
        <xsl:apply-templates select="." mode="id">
          <xsl:with-param name="idbase" select="$idbase"/>
        </xsl:apply-templates>
      </xsl:attribute>
      <xsl:apply-templates select="." mode="inner"/>
    </section>
  </xsl:if>
</xsl:template>

<d:para>Lesebeitrag ohne das umschließende section-Element</d:para>
<xsl:template match="co:file" mode="inner">
  <title>
    <xsl:apply-templates select="." mode="title"/>
  </title>
  <link role="embed" xl:href="{concat ($p_srvprf, .)}"/>
</xsl:template>

<d:para>Verzeichnis-Index: Überschrift ohne Datum</d:para>
<xsl:template match="co:file" mode="dirindex">
  <title>
    <xsl:apply-templates select="." mode="title_nodate"/>
  </title>
  <link role="embed" xl:href="{concat ($p_srvprf, .)}"/>
</xsl:template>

<xsl:template match="db:section[@role = 'treeview']//db:link">
  <xsl:copy>
    <xsl:if test="not (@role)">
      <xsl:attribute name="role">embed</xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="@* | * | text()"/>
  </xsl:copy>
</xsl:template>

<d:para>Andere Elemente und Attribute werden kopiert.</d:para>
<xsl:template match="* | @* | text()">
  <xsl:copy>
    <xsl:apply-templates select="* | @* | text()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
