<?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:hsh = "http://herbaer.de/xmlns/20121015/hash"
  xmlns:xl = "http://www.w3.org/1999/xlink"
  xmlns = "http://www.w3.org/1999/xhtml"
  exclude-result-prefixes = "d db hsh xl"
  version = "1.0"
>
<xsl:import href="../../../docbook_xsl/xhtml5/docbook.xsl"/>

<d:info xmlns="http://herbaer.de/xmlns/20051201/doc">
  <title>comp_dbk_ht.xslt</title>
  <subtitle>Einfache XHTML-Darstellung der Lesebeiträge</subtitle>
  <date>2013-02-18</date>
  <author>
    <personname>
      <firstname>Herbert</firstname>
      <surname>Schiemann</surname>
    </personname>
    <email>h.schiemann@herbaer.de</email>
  </author>
</d:info>

<d:para>Präfix des Pfades der "Stil-Dateien": CSS, Javascript, Icons</d:para>
<xsl:param name="p_styleprefix" select="'/style/'"/>

<d:section xmlns="http://herbaer.de/xmlns/20051201/doc">
  <d:para>Pfad der CSS-Datei</d:para>
  <d:para>
Wenn die Zeichenkette leer ist,
wird kein Verweis (<d:tag class="element">link</d:tag>-Element)
auf ein CSS-Stylesheet eingefügt.
  </d:para>
</d:section>
<xsl:param name="p_css" select="'embedded.css'"/>

<d:section xmlns="http://herbaer.de/xmlns/20051201/doc">
  <d:para>Pfad der Javascript-Datei</d:para>
  <d:para>
Wenn die Zeichenkette leer ist, wird kein Verweis auf eine Javasrcipt-Datei
(<d:tag class="element">script</d:tag>-Element) eingefügt.
  </d:para>
</d:section>
<xsl:param name="p_js" select="''"/>

<d:section xmlns="http://herbaer.de/xmlns/20051201/doc">
  <d:para>Pfad der Shortcut-Icon-Datei</d:para>
  <d:para>
Wenn die Zeichenkette leer ist,
wird kein Verweis (<d:tag class="element">link</d:tag>-Element)
auf ein Shortcut-Icon eingefügt
  </d:para>
</d:section>
<xsl:param name="p_icon" select="'shortcut_icon.png'"/>

<d:para>Level der Haupt-Überschrift</d:para>
<xsl:param name="p_level" select="3"/>

<d:para>Der Titel des erzeugten XHTML-Dokuments</d:para>
<xsl:param name="p_title">
  <xsl:apply-templates select="/db:*/db:info" mode="title"/>
</xsl:param>

<d:para>Titel nicht als Hauptüberschrift?</d:para>
<xsl:param name="p_hide_title">
  <xsl:choose>
    <xsl:when test="/db:*/db:info/db:title/@role = 'hidden'">1</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:param>

<d:para>
Format des Datums im Titel.
Mögliche Werte sind:
<d:simplelist>
<d:member>simple: 03.01.2012</d:member>
<d:member>montext: 03. Januar 2012</d:member>
<d:member>none: Datum wird im Titel nicht angezeigt</d:member>
</d:simplelist>
</d:para>
<xsl:param name="p_titledatefmt" select="'simple'"/>

<d:para>
Format des Datums (<d:code>phrase[@role='date']</d:code>) im Text
<d:simplelist>
<d:member>simple: 03.01.2012</d:member>
<d:member>montext: 03. Januar 2012</d:member>
</d:simplelist>
</d:para>
<xsl:param name="p_datefmt" select="'montext'"/>

<d:para>
Bei <d:parameter>p_foldthreshold</d:parameter> oder mehr Abschnitten
werden die Abschnitte &#x201e;faltbar&#x201d;.
Der Wert 0 bedeutet keine Faltung.
</d:para>
<xsl:param name="p_foldthreshold" select="4"/>

<d:para>
Anpassung der Docbook-Stylesheets:
Verweisziele im neuen Fenster/Tab öffnen.
</d:para>
<xsl:param name="ulink.target">_blank</xsl:param>

<d:para>Die Monatsnamen</d:para>
<xsl:variable name="g_monate" select="document('')/xsl:stylesheet/hsh:hash"/>

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

<d:para>
Der Titel des Dokuments
</d:para>
<xsl:template match="db:info" mode="title">
  <xsl:value-of select="db:title"/>
  <xsl:apply-templates select="db:date[1]"/>
</xsl:template>

<d:para>
Datum mit oder ohne Monatsnamen in Klammern
</d:para>
<xsl:template match="db:info/db:date">
  <xsl:choose>
    <xsl:when test="$p_titledatefmt = 'none'"/>
    <xsl:when test="$p_titledatefmt = 'montext'">
      <xsl:variable name="mon" select="substring (., 6, 2)"/>
      <xsl:value-of
        select = "
          concat (
          ' (',
          substring (., 9, 2), '. ',
          $g_monate/hsh:value[@key = $mon], ' ',
          substring (., 1, 4),
          ')'
          )
        "
      />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of
        select = "
          concat (
          ' (',
          substring (., 9, 2), '.', substring (., 6, 2), '.', substring (., 1, 4), ')' )
        "
      />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Angabe der Sprache
</d:para>
<xsl:template name="lang">
  <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:template>

<d:para>Der HTML-Rahmen</d:para>
<xsl:template match="/">
  <html>
    <xsl:attribute name="xml:lang">
      <xsl:call-template name="lang"/>
    </xsl:attribute>
    <head>
      <title><xsl:value-of select="$p_title"/></title>
      <xsl:apply-templates select="db:article/db:info/db:keywordset" mode="html.header"/>
      <xsl:if test="string-length ($p_css) &gt; 0">
        <xsl:element name="link">
          <xsl:attribute name="href">
            <xsl:value-of select="concat ($p_styleprefix, $p_css)"/>
          </xsl:attribute>
          <xsl:attribute name="rel">stylesheet</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:if test="string-length ($p_icon) &gt; 0">
        <xsl:element name="link">
          <xsl:attribute name="href">
            <xsl:value-of select="concat ($p_styleprefix, $p_icon)"/>
          </xsl:attribute>
          <xsl:attribute name="rel">icon</xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:if test="string-length ($p_js) &gt; 0 and .//@xl:href">
        <xsl:element name="script">
          <xsl:attribute name="src">
            <xsl:value-of select="concat ($p_styleprefix, $p_js)"/>
          </xsl:attribute>
        </xsl:element>
      </xsl:if>
      <xsl:if test="$p_foldthreshold &gt; 0 and count(*/db:section) &gt;= $p_foldthreshold"
      >
        <xsl:element name="link">
          <xsl:attribute name="href">
            <xsl:value-of select="concat ($p_styleprefix, 'fold.css')"/>
          </xsl:attribute>
          <xsl:attribute name="rel">stylesheet</xsl:attribute>
        </xsl:element>
        <xsl:element name="script">
          <xsl:attribute name="src">
            <xsl:value-of select="concat ($p_styleprefix, 'fold.js')"/>
          </xsl:attribute>
        </xsl:element>
        <xsl:element name="script">
          <xsl:text>
onload=function(){
new Folder().apply_doc_parent_child(document,"section","h</xsl:text>
          <xsl:value-of select="$p_level + 1"/>
          <xsl:text>");};</xsl:text>
        </xsl:element>
      </xsl:if>
    </head>
    <xsl:apply-templates/>
  </html>
</xsl:template>

<d:para>Der Rumpf</d:para>
<xsl:template match="db:article">
  <body>
    <xsl:if test="$p_hide_title = 0">
      <xsl:element name="h{$p_level}">
        <xsl:value-of select="$p_title"/>
      </xsl:element>
    </xsl:if>
    <xsl:apply-templates select="db:* [local-name() != 'title']"/>
  </body>
</xsl:template>

<d:para>Überschriften</d:para>
<xsl:template match="db:title">
  <xsl:variable name="level" select="count (ancestor::db:section) + $p_level"/>
  <xsl:variable name="hn">
    <xsl:choose>
      <xsl:when test="$level &lt; 6">
        <xsl:value-of select="concat ('h', $level)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'h6'"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:element name="{$hn}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<d:para>
Die importierte Vorlage für Titel wird ignoriert.
</d:para>
<xsl:template name="section.title"/>

<d:para>Inline-Elemente</d:para>
<xsl:template
  match = "
    db:personname | db:honorific | db:firstname | db:surname
    | db:street | db:postcode | db:city | db:phone
  "
>
  <xsl:apply-templates/>
</xsl:template>

<d:para>
Datum mit oder ohne Monatsnamen in Klammern
</d:para>
<xsl:template match="db:phrase[@role='date']">
  <xsl:choose>
    <xsl:when test="$p_datefmt = 'montext'">
      <xsl:variable name="mon" select="substring (., 6, 2)"/>
      <xsl:value-of
        select = "
          concat (
          substring (., 9, 2), '. ',
          $g_monate/hsh:value[@key = $mon], ' ',
          substring (., 1, 4)
          )
        "
      />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of
        select = "
          concat (
          substring (., 9, 2), '.', substring (., 6, 2), '.', substring (., 1, 4) )
        "
      />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<d:para>
Die Monatsnamen
</d:para>
<hsh:hash xmlns="http://herbaer.de/xmlns/20121015/hash">
  <value key="01">Januar</value>
  <value key="02">Februar</value>
  <value key="03">März</value>
  <value key="04">April</value>
  <value key="05">Mai</value>
  <value key="06">Juni</value>
  <value key="07">Juli</value>
  <value key="08">August</value>
  <value key="09">September</value>
  <value key="10">Oktober</value>
  <value key="11">November</value>
  <value key="12">Dezember</value>
</hsh:hash>
</xsl:stylesheet>
