<?xml version="1.0" encoding="utf8"?>
<!--

$Id: fo-printable.xsl,v 2.18 2004/10/13 23:00:14 jmates Exp $

The author disclaims all copyrights and releases this document into
the public domain.

XSLT templates and definitions for conversion of eolas format
into XSL-FO suitable for printing.

TODO: footnotes or something for URL so not hidden.

-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:fox="http://xml.apache.org/fop/extensions" version="1.0">
  <xsl:import href="fo-shared.xsl"/>
<!--  <xsl:strip-space elements="*"/> -->
  <xsl:attribute-set name="data" xsl:use-attribute-sets="computer">
    <xsl:attribute name="font-size">9pt</xsl:attribute>
    <xsl:attribute name="space-after">4pt</xsl:attribute>
    <xsl:attribute name="space-before">4pt</xsl:attribute>
    <xsl:attribute name="padding-left">2mm</xsl:attribute>
    <xsl:attribute name="padding-right">2mm</xsl:attribute>
    <xsl:attribute name="padding-top">2mm</xsl:attribute>
    <xsl:attribute name="padding-bottom">2mm</xsl:attribute>
    <xsl:attribute name="border">0.25px solid #000000</xsl:attribute>
    <xsl:attribute name="background-color">#FFFFFF</xsl:attribute>
<!-- force data blocks to left for maximum display space -->
    <xsl:attribute name="start-indent">0mm</xsl:attribute>
  </xsl:attribute-set>
  <xsl:attribute-set name="data-shell" xsl:use-attribute-sets="computer">
    <xsl:attribute name="font-size">9pt</xsl:attribute>
    <xsl:attribute name="padding-left">2mm</xsl:attribute>
    <xsl:attribute name="padding-right">2mm</xsl:attribute>
    <xsl:attribute name="padding-top">2mm</xsl:attribute>
    <xsl:attribute name="padding-bottom">2mm</xsl:attribute>
    <xsl:attribute name="border">0.25px solid #000000</xsl:attribute>
    <xsl:attribute name="background-color">#FFFFFF</xsl:attribute>
<!-- force data blocks to left for maximum display space -->
    <xsl:attribute name="start-indent">0mm</xsl:attribute>
  </xsl:attribute-set>
<!-- TODO figure out less klugy way of getting footnotes to not inherit -->
  <xsl:attribute-set name="footnote" xsl:use-attribute-sets="general">
    <xsl:attribute name="font-size">9pt</xsl:attribute>
    <xsl:attribute name="font-weight">normal</xsl:attribute>
    <xsl:attribute name="color">#000000</xsl:attribute>
    <xsl:attribute name="text-align">left</xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
<!--
        <fo:simple-page-master margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in" page-width="8.5in" page-height="11in" master-name="page">
          <fo:region-body margin-top="5mm"/>
          <fo:region-before extent="10mm" region-name="xsl-region-before"/>
        </fo:simple-page-master>

        <fo:page-sequence-master master-name="main-pages">
          <fo:repeatable-page-master-reference master-reference="page"/>
        </fo:page-sequence-master>
-->
        <fo:simple-page-master master-name="odd" page-height="11in" page-width="8.5in" margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in">
          <fo:region-body margin-top="5mm"/>
          <fo:region-before extent="10mm" region-name="odd-before"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="even" page-height="11in" page-width="8.5in" margin-right="1in" margin-left="1in" margin-bottom="1in" margin-top="1in">
          <fo:region-body margin-top="5mm"/>
          <fo:region-before extent="10mm" region-name="even-before"/>
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="main-pages">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference master-reference="odd" page-position="any" odd-or-even="odd"/>
            <fo:conditional-page-master-reference master-reference="even" page-position="any" odd-or-even="even"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
      </fo:layout-master-set>
      <xsl:apply-templates/>
    </fo:root>
  </xsl:template>
<!-- page numbers and section names in header by default -->
<!-- NOTE RenderX engine doesn't like the flat list kluge I am doing here! -->
  <xsl:template name="standard-header">
    <fo:static-content flow-name="odd-before">
      <fo:list-block>
        <fo:list-item>
          <fo:list-item-label>
            <fo:block text-align="left" xsl:use-attribute-sets="standard-header">
<!-- parent section for reference -->
              <fo:retrieve-marker retrieve-class-name="section" retrieve-boundary="page" retrieve-position="first-starting-within-page"/>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body>
            <fo:block xsl:use-attribute-sets="standard-header" text-align="right">
<!-- page numbers -->
              <fo:page-number/>
              <xsl:text> / </xsl:text>
              <fo:page-number-citation ref-id="finis"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </fo:static-content>
    <fo:static-content flow-name="even-before">
      <fo:list-block>
        <fo:list-item>
          <fo:list-item-label>
            <fo:block text-align="left" xsl:use-attribute-sets="standard-header">
<!-- page numbers -->
              <fo:page-number/>
              <xsl:text> / </xsl:text>
              <fo:page-number-citation ref-id="finis"/>
            </fo:block>
          </fo:list-item-label>
          <fo:list-item-body>
            <fo:block xsl:use-attribute-sets="standard-header" text-align="right">
<!-- parent section for reference -->
              <fo:retrieve-marker retrieve-class-name="section" retrieve-boundary="page" retrieve-position="first-starting-within-page"/>
            </fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </fo:static-content>
  </xsl:template>
<!-- eolas root element -->
  <xsl:template match="eolas">
    <fox:outline internal-destination="{generate-id(summary/title)}">
      <fox:label>
        <xsl:value-of select="summary/title"/>
      </fox:label>
    </fox:outline>
    <xsl:apply-templates select="section" mode="fox-outline"/>
    <fo:page-sequence master-reference="main-pages">
      <xsl:call-template name="standard-header"/>
      <fo:flow flow-name="xsl-region-body">
        <xsl:apply-templates select="summary"/>
<!-- need linear view of subsection tree to avoid nesting issues -->
        <xsl:for-each select="descendant::section">
          <xsl:call-template name="section"/>
        </xsl:for-each>
<!-- empty trailing block for total page count -->
        <fo:block id="finis"/>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>
  <xsl:template match="summary/title">
    <fo:block xsl:use-attribute-sets="general summary" font-size="24pt" font-weight="bold" id="{generate-id()}">
      <xsl:value-of select="."/>
      <xsl:if test="../source/@xlink:href">
        <xsl:call-template name="xlink-href-footnote">
          <xsl:with-param name="xlink-href">
            <xsl:value-of select="../source/@xlink:href"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:if>
    </fo:block>
  </xsl:template>
<!-- source URL as footnote to title -->
  <xsl:template match="summary/source"/>
  <xsl:template match="summary/source" mode="not-summary">
    <fo:block xsl:use-attribute-sets="general">
      <xsl:choose>
        <xsl:when test="text()">
          <xsl:value-of select="."/>
          <xsl:call-template name="xlink-href-footnote"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@xlink:href"/>
        </xsl:otherwise>
      </xsl:choose>
    </fo:block>
  </xsl:template>
  <xsl:template match="summary/version">
<!--    <xsl:if test="version"> -->
    <fo:block xsl:use-attribute-sets="summary computer" font-size="8pt">
      <xsl:value-of select="."/>
    </fo:block>
    <fo:block xsl:use-attribute-sets="summary computer" font-size="8pt">
      <xsl:text>$Id: fo-printable.xsl,v 2.18 2004/10/13 23:00:14 jmates Exp $</xsl:text>
    </fo:block>
<!--    </xsl:if> -->
  </xsl:template>
  <xsl:template match="summary">
<!-- KLUGE table to align summary stuff on single page until I can figure out a more elegant means of doing so -->
    <fo:table inline-progression-dimension="6.5in" table-layout="fixed" border="none 0.25mm black">
      <fo:table-column column-number="1" column-width="proportional-column-width(100)"/>
      <fo:table-body>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:apply-templates select="title"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:apply-templates select="description"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:if test="author">
              <fo:block xsl:use-attribute-sets="general summary">
                <xsl:for-each select="author">
                  <xsl:sort select="first-name"/>
                  <xsl:sort select="last-name"/>
                  <xsl:value-of select="first-name"/>
                  <xsl:if test="last-name">
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="last-name"/>
                  </xsl:if>
                  <xsl:if test="@xlink:href">
                    <xsl:call-template name="xlink-href-footnote"/>
                  </xsl:if>
                  <xsl:if test="not(position()=last())">
                    <xsl:text>, </xsl:text>
                  </xsl:if>
                </xsl:for-each>
              </fo:block>
            </xsl:if>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:if test="contributor">
              <fo:block xsl:use-attribute-sets="general summary">
                <xsl:text>(</xsl:text>
                <xsl:for-each select="contributor">
                  <xsl:sort select="first-name"/>
                  <xsl:sort select="last-name"/>
                  <xsl:value-of select="first-name"/>
                  <xsl:if test="last-name">
                    <xsl:text> </xsl:text>
                    <xsl:value-of select="last-name"/>
                  </xsl:if>
                  <xsl:if test="@xlink:href">
                    <xsl:call-template name="xlink-href-footnote"/>
                  </xsl:if>
                  <xsl:if test="not(position()=last())">
                    <xsl:text>, </xsl:text>
                  </xsl:if>
                </xsl:for-each>
                <xsl:text>)</xsl:text>
              </fo:block>
            </xsl:if>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.25in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:apply-templates select="source"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:apply-templates select="date"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.5in">
          <fo:table-cell border="none 0.25mm black">
            <xsl:apply-templates select="rights"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.75in">
          <fo:table-cell border-bottom="solid 0.25mm black">
            <xsl:apply-templates select="version"/>
          </fo:table-cell>
        </fo:table-row>
        <fo:table-row height="0.25in">
          <fo:table-cell>
            <fo:block/>
          </fo:table-cell>
        </fo:table-row>
      </fo:table-body>
    </fo:table>
  </xsl:template>
<!-- placeholder to keep default import template from complaining and
sections from being regenerated by recursive apply-templates calls -->
  <xsl:template match="section"/>
  <xsl:template name="section">
    <xsl:choose>
      <xsl:when test="(count(ancestor-or-self::section) + count(preceding::section)) mod 2 = 0">
        <fo:table break-before="page" id="{generate-id()}" inline-progression-dimension="6.5in" table-layout="fixed">
          <fo:table-column column-number="1" column-width="proportional-column-width     (100)"/>
          <fo:table-body>
            <fo:table-row height="4in">
              <fo:table-cell border-bottom="solid 0.25mm black">
                <xsl:apply-templates/>
              </fo:table-cell>
            </fo:table-row>
<!-- KLUGE spacer... -->
            <fo:table-row height="0.25in">
              <fo:table-cell>
                <fo:block/>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </xsl:when>
      <xsl:otherwise>
        <fo:table id="{generate-id()}" inline-progression-dimension="6.5in" table-layout="fixed" border="none 0.25mm black">
          <fo:table-column column-number="1" column-width="proportional-column-width(100)"/>
          <fo:table-body>
            <fo:table-row height="4.25in">
              <fo:table-cell border="none 0.25mm black">
                <xsl:apply-templates/>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="code">
    <fo:inline xsl:use-attribute-sets="computer">
      <xsl:apply-templates/>
    </fo:inline>
    <xsl:if test="@xlink:href">
      <xsl:call-template name="xlink-href-footnote"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="file|file-glob">
    <fo:inline color="#007a00" xsl:use-attribute-sets="computer">
      <xsl:apply-templates/>
    </fo:inline>
    <xsl:if test="@xlink:href">
      <xsl:call-template name="xlink-href-footnote"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="cmd|cmd-arg">
    <fo:inline color="#990033" xsl:use-attribute-sets="computer">
      <xsl:apply-templates/>
    </fo:inline>
    <xsl:if test="@xlink:href">
      <xsl:call-template name="xlink-href-footnote"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="em">
    <fo:inline font-style="italic" font-weight="bold">
      <xsl:apply-templates/>
    </fo:inline>
    <xsl:if test="@xlink:href">
      <xsl:call-template name="xlink-href-footnote"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="gui">
    <fo:inline color="#CC3300">
      <xsl:apply-templates/>
    </fo:inline>
    <xsl:if test="@xlink:href">
      <xsl:call-template name="xlink-href-footnote"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="acronym">
    <xsl:variable name="currentAbbr">
      <xsl:value-of select="abbr"/>
    </xsl:variable>
    <xsl:choose>
<!-- funny test is to determine whether this is the first acronym of a particular kind so we can treat it differently -->
      <xsl:when test="generate-id(//acronym[child::abbr=$currentAbbr]) = generate-id()">
        <xsl:value-of select="name"/>
        <xsl:text> (</xsl:text>
        <xsl:value-of select="abbr"/>
        <xsl:text>)</xsl:text>
        <xsl:if test="@xlink:href">
          <xsl:call-template name="xlink-href-footnote"/>
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="abbr"/>
        <xsl:if test="@xlink:href">
          <xsl:call-template name="xlink-href-footnote"/>
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="man">
    <fo:inline xsl:use-attribute-sets="computer">
      <xsl:value-of select="."/>
      <xsl:text>(</xsl:text>
      <xsl:value-of select="@section"/>
      <xsl:text>)</xsl:text>
    </fo:inline>
    <xsl:choose>
      <xsl:when test="@xlink:href">
        <xsl:call-template name="xlink-href-footnote"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="xlink-href-footnote">
          <xsl:with-param name="xlink-href">
            <xsl:text>http://www.FreeBSD.org/cgi/man.cgi?query=</xsl:text>
            <xsl:value-of select="."/>
            <xsl:if test="@section">
              <xsl:text>&amp;sektion=</xsl:text>
              <xsl:value-of select="@section"/>
            </xsl:if>
            <xsl:if test="@system">
              <xsl:text>&amp;manpath=</xsl:text>
              <xsl:value-of select="@system"/>
            </xsl:if>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="perl-module">
    <fo:inline xsl:use-attribute-sets="computer">
      <xsl:value-of select="."/>
    </fo:inline>
    <xsl:choose>
      <xsl:when test="@xlink:href">
        <xsl:call-template name="xlink-href-footnote"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="xlink-href-footnote">
          <xsl:with-param name="xlink-href">
<!--            <xsl:text>http://www.perldoc.com/cpan/</xsl:text> -->
<!-- change ::'s into /'s -->
<!--            <xsl:value-of select="translate(normalize-space(translate(text(), ':', ' ')), ' ', '/')"/>
            <xsl:text>.html</xsl:text> -->
            <xsl:text>http://search.cpan.org/search?mode=module&amp;query=</xsl:text>
            <xsl:value-of select="text()"/>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="perl-pod">
    <fo:inline xsl:use-attribute-sets="computer">
      <xsl:value-of select="."/>
    </fo:inline>
    <xsl:choose>
      <xsl:when test="@xlink:href">
        <xsl:call-template name="xlink-href-footnote"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="xlink-href-footnote">
          <xsl:with-param name="xlink-href">
            <xsl:text>http://www.perldoc.com/perl5.8.0/pod/</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>.html</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="rfc">
    <fo:inline xsl:use-attribute-sets="computer">
      <xsl:text>[RFC </xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>]</xsl:text>
    </fo:inline>
    <xsl:choose>
      <xsl:when test="@xlink:href">
        <xsl:call-template name="xlink-href-footnote"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="xlink-href-footnote">
          <xsl:with-param name="xlink-href">
            <xsl:text>http://www.rfc-editor.org/rfc/rfc</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>.txt</xsl:text>
          </xsl:with-param>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="link">
    <xsl:apply-templates/>
    <xsl:call-template name="xlink-href-footnote"/>
  </xsl:template>
<!-- special routine to generate footnotes from xlink:href urls -->
  <xsl:template name="xlink-href-footnote">
    <xsl:param name="xlink-href"/>
    <xsl:variable name="xlinktmp">
      <xsl:choose>
        <xsl:when test="@xlink:href">
          <xsl:value-of select="@xlink:href"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$xlink-href"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <fo:footnote>
      <fo:inline vertical-align="super" xsl:use-attribute-sets="footnote">
        <xsl:number level="any" count="eolas|*[@xlink:href]|man|perl-module|perl-pod|rfc" format="1"/>
      </fo:inline>
      <fo:footnote-body>
        <fo:list-block provisional-distance-between-starts="20pt" provisional-label-separation="5pt">
          <fo:list-item>
            <fo:list-item-label end-indent="label-end()">
<!-- TODO is there a "half-super" vertical-align?? -->
              <fo:block start-indent="0mm" vertical-align="baseline" line-height="1em" xsl:use-attribute-sets="footnote">
                <xsl:number level="any" count="eolas|*[@xlink:href]|man|perl-module|perl-pod|rfc" format="1"/>
              </fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="body-start()">
              <fo:wrapper start-indent="7mm" xsl:use-attribute-sets="footnote">
                <fo:block line-height="1em">
                  <xsl:value-of select="$xlinktmp"/>
                </fo:block>
              </fo:wrapper>
            </fo:list-item-body>
          </fo:list-item>
        </fo:list-block>
      </fo:footnote-body>
    </fo:footnote>
  </xsl:template>
</xsl:stylesheet>
