<?xml version="1.0" encoding="iso-8859-2"?>
<!-- $Id: pmla2fo.xsl,v 1.1.1.1 2003/01/02 21:00:55 tomek Exp $ -->
<!-- Szablon formatujący uproszczony dokument typu pmla do PD   -->
<!-- (c) t. przechlewski, 2002 (tomasz@gnu.univ.gda.pl)         -->
<!-- Zezwala się na wykorzystanie na warunkach licencji GPL     -->
<!-- patrz: http://www.gnu.org/licenses/gpl.html                -->
<!-- $Id: pmla2fo.xsl,v 1.1.1.1 2003/01/02 21:00:55 tomek Exp $ -->

<!DOCTYPE xsl:stylesheet [ <!ENTITY bull   "&#x2022;"> ]>

<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 indent='no' method="xml"/>

<!-- ======= różnego rodzaju parametry: ============================ -->
<xsl:param name="font-family-name">Times Roman</xsl:param>
<xsl:param name="title-color">#0060A0</xsl:param>
<xsl:param name="link-color">#0040C0</xsl:param>
<xsl:param name="internal-link-color">#006400</xsl:param>
<xsl:param name="cite-link-color">#006400</xsl:param>

<xsl:attribute-set name="title-attrs">
  <xsl:attribute 
   name="color"><xsl:value-of select="$title-color"/></xsl:attribute>
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="keep-together.within-column">always</xsl:attribute>
  <xsl:attribute name="keep-with-next.within-column">always</xsl:attribute>
  <xsl:attribute name="text-align">start</xsl:attribute>
  <xsl:attribute name="space-after.optimum">6pt</xsl:attribute>
</xsl:attribute-set>

<!-- ============ article ========================================== -->
  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
      font-family="{$font-family-name}"
      font-size="10pt"
      text-align="justify"
      line-height="normal" 
      language="pl">
      <fo:layout-master-set>
<!-- definiujemy trzy makiety: tytulowa, lewa i prawa .... -->
  <fo:simple-page-master master-name="tytulowa"
        page-width="210mm" page-height="297mm" >
    <fo:region-body 
        margin-left="30mm" margin-right="20mm" margin-top="20mm"
        margin-bottom="30mm" padding-top="4mm" />
    <fo:region-after region-name="tytulowa-p.dolna" 
         extent="30mm" padding-before="6mm" 
         padding-start="30mm" padding-end="20mm" />
    <fo:region-before region-name="tytulowa-p.gorna" extent="20mm" />
  </fo:simple-page-master>
  <!-- ...................................-->
  <fo:simple-page-master master-name="lewa" 
        page-width="210mm" page-height="297mm" >
    <fo:region-body margin-left="20mm" margin-right="30mm"
        border-top-width="1pt" border-top-color="black" border-top-style="solid"
        margin-top="20mm" margin-bottom="30mm" padding-top="4mm" />
    <fo:region-after region-name="lewa-p.dolna" extent="30mm" />
    <fo:region-before region-name="lewa-p.gorna" extent="20mm"
        padding-before="14mm" padding-start="20mm" padding-end="30mm" />
  </fo:simple-page-master>
  <!-- ...................................-->
  <fo:simple-page-master master-name="prawa"
        page-width="210mm" page-height="297mm" >
    <fo:region-body margin-left="30mm" margin-right="20mm" margin-top="20mm"
        border-top-width="1pt" border-top-color="black"
        border-top-style="solid"
        margin-bottom="30mm" padding-top="4mm" />
    <fo:region-after region-name="prawa-p.dolna" extent="30mm" />
    <fo:region-before  region-name="prawa-p.gorna"  extent="20mm"
        padding-before="14mm" padding-start="30mm" padding-end="20mm" />
  </fo:simple-page-master>

<!-- okreslamy uklad dwustronny: .... -->
  <fo:page-sequence-master master-name="d-stronny">
    <fo:repeatable-page-master-alternatives>
      <fo:conditional-page-master-reference master-reference="tytulowa" 
          page-position="first" />
      <fo:conditional-page-master-reference master-reference="prawa" 
          odd-or-even="odd" />
      <fo:conditional-page-master-reference master-reference="lewa"
          odd-or-even="even" />
    </fo:repeatable-page-master-alternatives>
  </fo:page-sequence-master>

<!-- okreslamy uklad jednostronny (z wyrozniona str. tyt.): .... -->
  <fo:page-sequence-master master-name="j-stronny">
    <fo:repeatable-page-master-alternatives>
      <fo:conditional-page-master-reference 
          master-reference="tytulowa" page-position="first" />
      <fo:conditional-page-master-reference master-reference="prawa" />
    </fo:repeatable-page-master-alternatives>
  </fo:page-sequence-master>
</fo:layout-master-set>

 <fo:page-sequence master-reference="d-stronny">
  <fo:static-content flow-name="prawa-p.dolna"></fo:static-content>
  <fo:static-content flow-name="lewa-p.dolna"></fo:static-content>
  <fo:static-content flow-name="prawa-p.gorna">
    <fo:block font-size="9pt" text-align-last="justify" 
        background-color="yellow">
       <!-- ### -->
       <fo:leader leader-pattern="use-content" /> 
       <fo:page-number /> 
       </fo:block> </fo:static-content>
  <fo:static-content flow-name="lewa-p.gorna"> 
  <fo:block font-size="9pt" text-align-last="justify" 
      background-color="yellow">
       <!-- ### -->
       <fo:page-number /> 
       <fo:leader leader-pattern="use-content" /> 
  </fo:block> </fo:static-content>
  <fo:static-content flow-name="tytulowa-p.dolna" > 
   <fo:block font-size="9pt" text-align="center">
      <fo:page-number /> </fo:block> </fo:static-content>

  <fo:static-content flow-name="tytulowa-p.gorna"></fo:static-content>
  <fo:static-content flow-name="tytulowa-start"></fo:static-content>
  <fo:static-content flow-name="tytulowa-end"></fo:static-content>

  <fo:flow flow-name="xsl-region-body">
    <!-- <xsl:apply-templates/> -->
    <xsl:call-template name="article-start"/>
  </fo:flow>
 </fo:page-sequence>
</fo:root>
</xsl:template>

<!-- ========== tytulatura: =================================== -->
  <xsl:template name="article-start">
     <fo:block font-size="14pt" xsl:use-attribute-sets="title-attrs"
         space-after.optimum="12pt" hyphenate="false">
       <xsl:apply-templates select="/article/title" mode="tpage"/>
     </fo:block>
     <fo:block space-after.optimum="0pt" hyphenate="false">
        <xsl:apply-templates select="/article/author" mode="tpage" />
     </fo:block>
     <fo:block space-after.optimum="0pt" 
         font-family="monospace" hyphenate="false">
        <xsl:apply-templates select="/article/email" mode="tpage" />
     </fo:block>
     <!-- ;;; spisek treściwy: ;;; -->
     <xsl:call-template name="toc"/>
     <xsl:apply-templates/>
  </xsl:template>

 <xsl:template match="article/title | article/author |
    article/email" mode="tpage" >
     <xsl:apply-templates/>
  </xsl:template>
 <xsl:template match="article/title | article/author |
    article/email"/>

<!-- ========== spis treści =================================== -->
  <xsl:template name="toc">
      <fo:block xsl:use-attribute-sets="title-attrs">
        Spis treści
      </fo:block>
      <xsl:apply-templates select="//section/title |
          //subsection/title | //subsubsection/title |
          //abstract" mode="toc"/>
  </xsl:template>

<xsl:template match="section/title" mode="toc">
   <fo:block>
    <fo:basic-link internal-destination="{generate-id(.)}">
    <xsl:number level="multiple" 
         count="section | subsection | subsubsection" format="1.1.1."/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
    //<fo:page-number-citation ref-id="{generate-id()}"/>
    </fo:basic-link>
   </fo:block>
</xsl:template>

<xsl:template match="subsection/title" mode="toc">
   <fo:block>
   <fo:basic-link internal-destination="{generate-id(.)}">
    <xsl:number level="multiple" 
         count="section | subsection | subsubsection" format="1.1.1."/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates />
    //<fo:page-number-citation ref-id="{generate-id()}" />
    </fo:basic-link>
   </fo:block>
</xsl:template>

<xsl:template match="subsubsection/title" mode="toc">
   <fo:block>
    <fo:basic-link internal-destination="{generate-id(.)}">
    <xsl:number level="multiple" 
         count="section | subsection | subsubsection" format="1.1.1."/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates />
    //<fo:page-number-citation ref-id="{generate-id()}"/>
    </fo:basic-link>
   </fo:block>
</xsl:template>

<xsl:template match="abstract" mode="toc">
 <fo:block> 
   <fo:basic-link internal-destination="{generate-id(.)}">
    Streszczenie // <fo:page-number-citation ref-id="{generate-id()}"/>
    </fo:basic-link> </fo:block>
</xsl:template>

<!-- =============== śródtytuły: ================================= -->
<xsl:template match="section/title">
   <fo:block id="{generate-id()}">
    <fo:marker marker-class-name="sec"> 
      <xsl:value-of select="."/>
    </fo:marker>
    <fo:block font-size="14pt"
      xsl:use-attribute-sets="title-attrs"
      space-before.optimum="18pt"
      space-before.conditionality="retain"
      space-after.optimum="6pt">
     <xsl:number level="multiple" 
          count="section | subsection | subsubsection" format="1.1.1."/>
     <xsl:text> </xsl:text>
     <xsl:apply-templates/>        
    </fo:block>
   </fo:block> 
</xsl:template>

<xsl:template match="subsection/title">
 <fo:block id="{generate-id()}" font-size="12pt"
    xsl:use-attribute-sets="title-attrs" space-before.optimum="12pt"
    space-before.conditionality="retain" space-after.optimum="6pt" >
    <xsl:number level="multiple" 
         count="section | subsection | subsubsection" format="1.1.1."/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
   </fo:block>
</xsl:template>

 <xsl:template match="subsubsection/title">
  <fo:block font-size="10pt"
    xsl:use-attribute-sets="title-attrs" space-before.optimum="9pt"
    space-before.conditionality="retain" space-after.optimum="3pt" >
    <xsl:number level="multiple" 
         count="section | subsection | subsubsection" format="1.1.1."/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
   </fo:block>
 </xsl:template>

 <xsl:template match="abstract">
   <fo:block id="{generate-id()}" font-size="12pt"
     xsl:use-attribute-sets="title-attrs" space-before.optimum="12pt"
     space-before.conditionality="retain" space-after.optimum="6pt" >
     Streszczenie
   </fo:block>
   <xsl:apply-templates/>
 </xsl:template>

<!-- ======== Podział: ===================================== -->
<xsl:template match="para">
  <fo:block space-after.optimum="0pt" hyphenate="true" >
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="section|subsection|subsubsection">
    <xsl:apply-templates/>
</xsl:template>

<!-- tekst wyrozniony: -->
<xsl:template match="emph">
  <fo:inline font-style="italic">
    <xsl:apply-templates/>
  </fo:inline>
</xsl:template>

<xsl:template match="verb">
  <fo:wrapper white-space-collapse="false"
     font-family="monospace"><xsl:apply-templates/></fo:wrapper>
</xsl:template>

<!-- ===== inne bloki tekstu =============================== -->
<xsl:template match="verbatim">
  <fo:block font-family="monospace" text-align="start"
       space-before="6pt" space-after="6pt" font-size="9.5pt"
       white-space-collapse="false" linefeed-treatment="preserve" 
       hyphenate="false" orphans="3" widows="3">
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

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

<!-- ==== rysunki ========================================== -->
 <xsl:template match="figure">
  <!-- Fop nie obsługuje elementu float -->
  <!-- <fo:float id="{generate-id(.)}"> -->
     <fo:block id="{generate-id(.)}"
         space-before.optimum="12pt" space-after.optimum="18pt" >
     <!-- wstaw <a name jeżeli `figure' ma atrybut `id'      -->
     <!-- <xsl:if test="@id"> <A name="{@id}"/> </xsl:if>    -->
     <xsl:apply-templates select="graphic"/>
     <!-- wstaw ostrzeżenie jeżeli nie ma elementu graphic -->
     <!-- o wartości atrybutu `format' równej png          -->
     <xsl:if test="not(./graphic[@format='PNG' or 
        @format='JPG' or @format='GIF'])">
      <fo:block text-align="center" color="red">**rysunek**</fo:block>
     </xsl:if>
  <!-- </fo:float> -->
  <xsl:apply-templates select="title"/>
  </fo:block>

 </xsl:template>

<xsl:template match="figure/title">
  <fo:block>
    <!-- specyfikacja jest dość mętna w tym miejscu -->
    Rysunek
    <xsl:number format="1." level="any" count="figure" from="/" />
    <xsl:apply-templates/>
  </fo:block>
</xsl:template>

<xsl:template match="graphic[@format='PNG'
     or @format='JPG' or @format='GIF'][position()=1]">
   <fo:block>
   <!-- pierwszy element zawierający PNG/JPG lub GIF -->
   <fo:external-graphic src="url('{@src}')"/>
   </fo:block>
</xsl:template>

<!-- =============== Listy/wyliczenia ============================= -->
<xsl:template match="itemize|enumerate">
  <xsl:variable name="list-type"
                select="name()"/>
  <xsl:variable name="list-level"
                select="count(ancestor-or-self::*[name()=$list-type])"/>

  <fo:list-block provisional-label-separation="3pt"
                 provisional-distance-between-starts="18pt"
                 space-before.optimum="6pt" space-after.optimum="6pt">
    <xsl:apply-templates>
      <xsl:with-param name="list-level" select="$list-level"/>
    </xsl:apply-templates>

  </fo:list-block>
</xsl:template>

 <xsl:template match="description">
    <fo:block space-before.optimum="6pt"
         space-after.optimum="6pt">
      <xsl:apply-templates/>
    </fo:block>
 </xsl:template>

  <xsl:template match="descritem">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="term">
    <fo:block> <xsl:apply-templates/> </fo:block>
  </xsl:template>

  <xsl:template match="descr">
    <fo:block> <xsl:apply-templates/> </fo:block>
  </xsl:template>

<xsl:template match="itemize/item">
<!-- element listy nie numerowanej -->
  <xsl:param name="list-level"/>
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block text-align="start">
        <xsl:choose>
          <xsl:when test="($list-level mod 2) = 1">
            <xsl:text>&bull;</xsl:text>  <!-- disk bullet -->
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>-</xsl:text> <!-- hyphen bullet -->
          </xsl:otherwise>

        </xsl:choose>
      </fo:block>
    </fo:list-item-label>

    <fo:list-item-body start-indent="body-start()">
      <fo:block><xsl:apply-templates/></fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<xsl:template match="enumerate/item">
<!-- element listy numerowanej -->
  <xsl:param name="list-level"/>
  <fo:list-item>
    <fo:list-item-label end-indent="label-end()">
      <fo:block text-align="start">
        <xsl:choose>
          <xsl:when test="($list-level mod 2) = 1"> <!-- numeracja arabska -->
            <xsl:number format="1."/>
          </xsl:when>
          <xsl:otherwise>  <!-- numeracja alfabetyczna -->
            <xsl:number format="a."/>
          </xsl:otherwise>
        </xsl:choose>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block><xsl:apply-templates/></fo:block>
    </fo:list-item-body>
  </fo:list-item>
</xsl:template>

<!-- ============ bibliografia ============================= -->
<!-- Dodać bibliografię do spisu treści                      -->
  <xsl:template match="bibliography">
    <fo:block font-size="14pt"
      xsl:use-attribute-sets="title-attrs"
      space-before.optimum="18pt"
      space-before.conditionality="retain"
      space-after.optimum="6pt">Bibliografia</fo:block>
     <fo:list-block provisional-label-separation="3pt"
        provisional-distance-between-starts="18pt"
        space-before.optimum="6pt" space-after.optimum="6pt">
     <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="bibliography/bibitem">
  <fo:list-item id="{@id}">
    <fo:list-item-label end-indent="label-end()">
      <fo:block text-align="start">
      <xsl:number format="1."/>
      </fo:block>
    </fo:list-item-label>
    <fo:list-item-body start-indent="body-start()">
      <fo:block><xsl:apply-templates/></fo:block>
    </fo:list-item-body>
  </fo:list-item>
  </xsl:template>

  <xsl:template match="bibitem" mode="xref">
  <!-- Ustalenie numeru pozycji bibliograficznej w spisie; szablon  -->
  <!-- wywoływany przez `cite'                                      -->
    <xsl:number value="count(preceding::bibitem) +1" format="1" />
  </xsl:template>

  <xsl:template match="cite">
    <xsl:variable name="text" select="normalize-space(text())"/>
    <xsl:variable name="bibliolab" select="@src"/>
    <fo:inline color="{$cite-link-color}">
      <xsl:text>[</xsl:text>
       <fo:basic-link internal-destination="{@src}">
        <xsl:apply-templates select="//bibitem[@id=$bibliolab]" mode="xref"/>
        <xsl:if test="string-length($text) &gt; 0">
         <xsl:text>, </xsl:text>
         <xsl:value-of select="."/> 
        </xsl:if>
       </fo:basic-link>
      <xsl:text>]</xsl:text>
    </fo:inline>
  </xsl:template>

<!-- ============== przypisy =================================== -->
<!-- Poniższa wersja jest lepsza, ale FOP v 0.20.4 kończy        -->
<!-- działanie z bliżej nieokreślonym błędem                     -->
<!--
<xsl:template match="footnote">
  <fo:footnote>
    <xsl:apply-templates select="." mode="footnote-number"/>
    <fo:footnote-body>
      <fo:list-block 
            provisional-distance-between-starts="15pt"
            provisional-label-separation="0pt" 
            font-family="{$font-family-name}"
            font-size="8pt">
        <fo:list-item>
          <fo:list-item-label end-indent="label-end()">
            <fo:block text-align="start">
              <xsl:apply-templates select="." mode="footnote-number"/>
            </fo:block>
          </fo:list-item-label>

          <fo:list-item-body start-indent="body-start()">
            <fo:block padding-top="2pt"><xsl:apply-templates/></fo:block>
          </fo:list-item-body>
        </fo:list-item>
      </fo:list-block>
    </fo:footnote-body>
  </fo:footnote>
</xsl:template>

<xsl:template match="footnote" mode="footnote-number">
  <fo:inline baseline-shift="super" font-size="7pt"
      white-space-treatment="ignore">
    <xsl:number count="footnote" level="any" format="1"/>
  </fo:inline>
</xsl:template>
-->

<xsl:template match="footnote">
  <fo:footnote>
    <xsl:apply-templates select="." mode="footnote-number"/>
        <!-- padding-top="2pt" -->
    <fo:footnote-body>
      <fo:block font-family="{$font-family-name}"
        font-size="8pt" padding-top="6pt">
         <xsl:apply-templates select="." mode="footnote-number"/>
         <xsl:apply-templates/>
      </fo:block>
    </fo:footnote-body>
  </fo:footnote>
</xsl:template>

<xsl:template match="footnote" mode="footnote-number">
  <fo:inline vertical-align="super" font-size="7pt" >
    <xsl:number count="footnote" level="any" format="1"/>
  </fo:inline>
</xsl:template>

<!-- =============== inne ============================== -->
  <xsl:template match="url">
  <!-- jezeli zawartosc elementu url jest pusta skopiuj  -->
  <!-- atrybut `src' jako wartosc htmlowego elementu <a> -->
    <xsl:variable name="text" select="normalize-space(text())"/>
    <xsl:choose>
     <xsl:when test="string-length($text) &lt; 1">
       <fo:basic-link external-destination="url('{@src}')">
        <fo:wrapper color="{$link-color}"
           font-family="monospace">
          <xsl:value-of select="@src"/>
        </fo:wrapper>
       </fo:basic-link>
     </xsl:when>
     <xsl:otherwise>
       <fo:basic-link external-destination="url('{@src}')">
        <fo:wrapper color="{$link-color}"
           font-family="monospace">
           <xsl:value-of select="."/>
        </fo:wrapper>
       </fo:basic-link>
     </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="ref">
    <xsl:variable name="reflabel" select="@src"/>
    <fo:basic-link internal-destination="{generate-id(//*[@id=$reflabel])}">
     <!-- wstaw tytuł elementu do którego jest odesłanie:  -->
     <!-- cytowanie tytułów jest cośkolwiek prostsze       -->
     <fo:wrapper font-style="italic" color="{$internal-link-color}">
      <xsl:value-of select="//*[@id=$reflabel]/title" />
     </fo:wrapper>
    </fo:basic-link>
  </xsl:template>

  <!-- wersja pdf [na razie] nie ma skorowidza: -->
  <xsl:template match="index"/>

<!-- Koniec== -->
</xsl:stylesheet>

