<?xml version="1.0" encoding="utf-8"?>
<!--
A simple XSLT to convert special XHTML document to XSL-FO document
Copyright (C) 2005 Bieler Batiste

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-->

<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:fo="http://www.w3.org/1999/XSL/Format"
	xmlns:xhtml="http://www.w3.org/1999/xhtml">

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

<xsl:param name="page.width">210mm</xsl:param>
<xsl:param name="page.height">297mm</xsl:param>
<xsl:param name="page.margin.top">3cm</xsl:param>
<xsl:param name="page.margin.right">2cm</xsl:param>
<xsl:param name="page.margin.left">3cm</xsl:param>
<xsl:param name="page.margin.bottom">2.5cm</xsl:param>
<xsl:param name="page.body.margin.top">2cm</xsl:param>
<xsl:param name="page.body.margin.right">1.2cm</xsl:param>
<xsl:param name="page.body.margin.bottom">2.5cm</xsl:param>
<xsl:param name="page.body.margin.left">0cm</xsl:param>
<xsl:param name="page.before.extent">2cm</xsl:param>
<xsl:param name="page.after.extent">2cm</xsl:param>
<xsl:param name="page.title.font-size">24pt</xsl:param>
<xsl:param name="page.h1.font-size">20pt</xsl:param>
<xsl:param name="page.h2.font-size">16pt</xsl:param>
<xsl:param name="page.h3.font-size">14pt</xsl:param>
<xsl:param name="page.h4.font-size">12pt</xsl:param>
<xsl:param name="page.body.font-size">10pt</xsl:param>
<xsl:param name="page.code.font-size">9pt</xsl:param>
<xsl:param name="page.header.font-size">8pt</xsl:param>
<xsl:param name="page.footer.font-size">8pt</xsl:param>
<xsl:param name="page.p.margin-left">1cm</xsl:param>

<xsl:template match="/xhtml:html">
	<fo:root>
		<fo:layout-master-set>
			<fo:simple-page-master
					master-name="front"
					page-height="{$page.height}"
					page-width="{$page.width}"
					margin-top="{$page.margin.top}"
					margin-bottom="{$page.margin.bottom}"
					margin-left="{$page.margin.left}"
					margin-right="{$page.margin.right}">
				<fo:region-body />
			</fo:simple-page-master>
			<fo:simple-page-master
					master-name="data"
					page-height="{$page.height}"
					page-width="{$page.width}"
					margin-top="{$page.margin.top}"
					margin-bottom="{$page.margin.bottom}"
					margin-left="{$page.margin.left}"
					margin-right="{$page.margin.right}">
				<fo:region-body
					margin-top="{$page.body.margin.top}"
					margin-right="{$page.body.margin.right}"
					margin-bottom="{$page.body.margin.bottom}"
					margin-left="{$page.body.margin.left}" />
				<fo:region-before
					region-name="header"
					extent="{$page.before.extent}"/>
				<fo:region-after
					region-name="footer"
					extent="{$page.after.extent}"/>
			</fo:simple-page-master>
		</fo:layout-master-set>
		
		<xsl:apply-templates select="xhtml:head" />
		<xsl:apply-templates select="xhtml:body" />
		
	</fo:root>
</xsl:template>

<xsl:template match="xhtml:head">
	<fo:page-sequence master-reference="front">
	
		<fo:flow flow-name="xsl-region-body" >
		<fo:block
			border-after-style="solid"
			border-after-width="1pt"
			border-after-color="#cccccc"
			space-after="100pt">
			<fo:block font-size="{$page.title.font-size}" font-family="sans-serif">
				<xsl:value-of select="xhtml:title" />
			</fo:block>
		</fo:block>
		<fo:block>
			<xsl:apply-templates select="/xhtml:html/xhtml:body/xhtml:div[@id='subtitle']"/>
		</fo:block>
		<fo:block break-before="page"/>
		<fo:block>
			<xsl:apply-templates select="/xhtml:html/xhtml:body/xhtml:div[@id='abstract']"/>
		</fo:block>
		</fo:flow>
	</fo:page-sequence>
</xsl:template>

<xsl:template match="xhtml:div[@id='subtitle']">
	<fo:block text-align="right">
			<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:div[@id='abstract']">
	<fo:block>
			<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:body">

	<fo:page-sequence master-reference="data">
	
		<fo:static-content flow-name="header">
			<fo:block font-size="{$page.header.font-size}">
				<fo:table table-layout="fixed">
				<fo:table-column column-width="220pt"/>
				<fo:table-column column-width="220pt"/>
				<fo:table-body>
					<fo:table-row>
					<fo:table-cell>
						<fo:block>
							<xsl:value-of select="/xhtml:html/xhtml:head/xhtml:title"/>
						</fo:block>
					</fo:table-cell>
					<fo:table-cell>
						<fo:block text-align="right">
							<fo:retrieve-marker retrieve-class-name="header1" 
							retrieve-position="first-starting-within-page"
							retrieve-boundary="page-sequence"
							/>
						</fo:block>
						<!--<fo:block text-align="right">
								<fo:retrieve-marker retrieve-class-name="header2" 
								retrieve-position="first-starting-within-page"
								retrieve-boundary="page-sequence"/>
						</fo:block>-->
					</fo:table-cell>
					</fo:table-row>
				</fo:table-body>
				</fo:table>
			</fo:block>
		</fo:static-content>
		
		<fo:static-content flow-name="footer">
			<fo:block text-align="center" font-size="{$page.footer.font-size}">
				<fo:page-number/> sur
				<fo:page-number-citation ref-id="lastPage"/>
			</fo:block>
		</fo:static-content>
		
		<fo:flow flow-name="xsl-region-body">
			<xsl:call-template name="toc"/>
			 <xsl:apply-templates select="/xhtml:html/xhtml:body/*[count(@id)=0 or (@id!='subtitle' and @id!='abstract')]" />
			<fo:block id="lastPage" font-size="0pt" line-height="0pt" space-after="0pt"/>
		</fo:flow>
		
	</fo:page-sequence>
	
</xsl:template>

<xsl:template match="xhtml:p">
	<fo:block space-after="1em" text-align="justify" font-size="{$page.body.font-size}">
		<xsl:if test="name(..)='body' or name(..)='div'">
			<xsl:attribute name="margin-left">
				<xsl:value-of select="$page.p.margin-left"/>
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:blockquote">
	<fo:block margin-left="{$page.p.margin-left}" font-style="italic" font-family="Times" space-after="1em" font-size="{$page.body.font-size}">
      «<xsl:apply-templates/>»
	</fo:block>
    <fo:block margin-left="{$page.p.margin-left}" text-align="right" font-size="90%" space-after="2em">
        <xsl:text>— </xsl:text>
        <xsl:value-of select="@cite" />
    </fo:block>
</xsl:template>

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

<xsl:template match="xhtml:h1">
	<xsl:if test="name(..)='body'">
	 <fo:block break-before="page"/>
   </xsl:if>
	<fo:block font-size="{$page.header.font-size}">
		<fo:marker marker-class-name="header1">
			<xsl:value-of select="."/>
		</fo:marker>
	</fo:block>
	<fo:block font-size="{$page.h1.font-size}" space-after="1em">
		<xsl:attribute name="id">
		<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
			 <xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:attribute>
      <xsl:if test="name(..)='body'">
          <xsl:number/>.
      </xsl:if>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:h2">
	<fo:block font-size="{$page.header.font-size}">
		<fo:marker marker-class-name="header2">
			<xsl:value-of select="."/>
		</fo:marker>
	</fo:block>
	<fo:block font-size="{$page.h2.font-size}" space-after="1em">
		<xsl:attribute name="id">
			<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
      <xsl:value-of select="count(preceding-sibling::xhtml:h1)"/>.<xsl:value-of select="1+count(preceding-sibling::xhtml:h2)-count(preceding-sibling::xhtml:h1/preceding-sibling::xhtml:h2)"/>.
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:h3">
	<fo:block font-size="{$page.h3.font-size}" space-after="1em">
		<xsl:attribute name="id">
			<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
      <xsl:value-of select="count(preceding-sibling::xhtml:h1)"/>.<xsl:value-of select="count(preceding-sibling::xhtml:h2)-count(preceding-sibling::xhtml:h1/preceding-sibling::xhtml:h2)"/>.<xsl:value-of select="1+count(preceding-sibling::xhtml:h3)-count(preceding-sibling::xhtml:h2/preceding-sibling::xhtml:h3)"/>.
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:h4">
	<fo:block font-size="{$page.h4.font-size}" space-after="1em">
		<xsl:attribute name="id">
			<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:h5">
	<fo:block font-size="{$page.h4.font-size}" space-after="1em">
		<xsl:attribute name="id">
			<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:h6">
	<fo:block font-size="{$page.h4.font-size}" space-after="1em">
		<xsl:attribute name="id">
			<xsl:choose>
			<xsl:when test="@id">
				<xsl:value-of select="@id"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="generate-id()"/>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:attribute>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:dl">
	<fo:block  space-after="1em" font-size="{$page.body.font-size}">
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:dt">
	<fo:block  font-weight="bold" space-before="0.8em" space-after="0.1em">
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:dd">
	<fo:block margin-left="2cm">
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:ul">
	<fo:block space-after="1em" 
		margin-left="inherited-property-value(margin-left)+1.5cm" 
		font-size="{$page.body.font-size}" relative-position = "relative">
		<xsl:apply-templates />
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:ol">
	<fo:block space-after="1em" 
		margin-left="inherited-property-value(margin-left)+1.5cm"
		 font-size="{$page.body.font-size}" relative-position = "relative">
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:li">
	<fo:list-item>
		<fo:list-item-label text-align="end" end-indent="label-end()">
			<fo:block>
				<xsl:choose>
					<xsl:when test="name(..)='ol'">
						<xsl:number/><xsl:text>.</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>•</xsl:text>
					</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="xhtml:pre">
	<fo:block font-family="monospace" white-space-collapse="false"
	 space-after="1em" font-size="{$page.code.font-size}">
		<xsl:if test="name(..)='body'">
			<xsl:attribute name="margin-left">
				<xsl:text>1cm</xsl:text>
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>

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

<xsl:template match="xhtml:table">
	<xsl:param name="td-set" select="xhtml:tr[1]/xhtml:td" />
	<xsl:variable name="col-width" select="round(100 div(count($td-set)))"/>
	<fo:block space-after="1em" font-size="{$page.body.font-size}">
	<fo:table width="350pt" start-indent="50pt">
		<xsl:for-each select="$td-set">
			<fo:table-column width="{$col-width}pt" />
		</xsl:for-each>
		<fo:table-body>
			<xsl:apply-templates/>
		</fo:table-body>
	</fo:table>
	</fo:block>
</xsl:template>

<xsl:template match="xhtml:tr">
	<fo:table-row>
		<xsl:apply-templates/>
	</fo:table-row>
</xsl:template>

<xsl:template match="xhtml:td">
	<fo:table-cell>
		<fo:block>
			<xsl:apply-templates/>
		</fo:block>
	</fo:table-cell>
</xsl:template>

<xsl:template match="xhtml:img">
  <fo:block space-after="2em" space-before="1em" text-align="center">
    <fo:external-graphic src="{@src}" width="{@width}pt"/>
  </fo:block>
</xsl:template>

<xsl:template match="xhtml:a|xhtml:i">
  <fo:inline font-style="italic">
		<xsl:apply-templates/>
  </fo:inline>
</xsl:template>
  
<xsl:template name="toc">
	<fo:block break-before="page"/>
	<fo:block space-after="1em" font-size="{$page.h1.font-size}">
		Table des matières
	</fo:block>
	<xsl:for-each select="xhtml:h1|xhtml:h2|xhtml:h3|xhtml:h4">
		<fo:block text-align-last="justify" font-size="{$page.body.font-size}">
			<xsl:attribute name="start-indent">
				<xsl:choose>
				<xsl:when test="name() = 'h1'">
					<xsl:text>1cm</xsl:text>
				</xsl:when>
				<xsl:when test="name() = 'h2'">
					<xsl:text>2cm</xsl:text>
				</xsl:when>
				<xsl:when test="name() = 'h3'">
					<xsl:text>3cm</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>4cm</xsl:text>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
				<xsl:choose>
				<xsl:when test="name() = 'h1'">
                <xsl:number/>.
				</xsl:when>
				<xsl:when test="name() = 'h2'">
      <xsl:value-of select="count(preceding-sibling::xhtml:h1)"/>.<xsl:value-of select="1+count(preceding-sibling::xhtml:h2)-count(preceding-sibling::xhtml:h1/preceding-sibling::xhtml:h2)"/>.
				</xsl:when>
				<xsl:when test="name() = 'h3'">
      <xsl:value-of select="count(preceding-sibling::xhtml:h1)"/>.<xsl:value-of select="count(preceding-sibling::xhtml:h2)-count(preceding-sibling::xhtml:h1/preceding-sibling::xhtml:h2)"/>.<xsl:value-of select="1+count(preceding-sibling::xhtml:h3)-count(preceding-sibling::xhtml:h2/preceding-sibling::xhtml:h3)"/>.
				</xsl:when>
				<xsl:otherwise>
      <xsl:value-of select="count(preceding-sibling::xhtml:h1)"/>.<xsl:value-of select="count(preceding-sibling::xhtml:h2)-count(preceding-sibling::xhtml:h1/preceding-sibling::xhtml:h2)"/>.<xsl:value-of select="count(preceding-sibling::xhtml:h3)-count(preceding-sibling::xhtml:h2/preceding-sibling::xhtml:h3)"/>.
				</xsl:otherwise>
				</xsl:choose>
			<xsl:value-of select="."/>
			<fo:leader leader-pattern="dots"
			leader-pattern-width="5pt"/>
			<fo:page-number-citation ref-id="@id">
			<xsl:attribute name="ref-id">
				<xsl:choose>
				<xsl:when test="@id">
					<xsl:value-of select="@id"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="generate-id()"/>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			</fo:page-number-citation>
		</fo:block>
	</xsl:for-each>
</xsl:template>

<xsl:template match="xhtml:div">
    <fo:block>
        <xsl:if test="@id='bibliographie'">
            <xsl:call-template name="bibliographie"/>
        </xsl:if>
    </fo:block>
</xsl:template>

<xsl:template name="bibliographie">
    <xsl:for-each select="xhtml:dl">
        <fo:list-item space-after="1em">
            <fo:list-item-label text-align="end" end-indent="label-end()">
                <fo:block>
                    <xsl:text>•</xsl:text>
                </fo:block>
            </fo:list-item-label>
            <fo:list-item-body start-indent="body-start()">
                <fo:block>
                    <fo:inline font-variant="small-caps"><xsl:value-of select="xhtml:dd[@class = 'nom']"/></fo:inline>
                    <xsl:text> (</xsl:text>
                    <xsl:value-of select="xhtml:dd[@class = 'prenom']"/>
                    <xsl:text>), </xsl:text>
                    <fo:inline font-style="italic"><xsl:value-of select="xhtml:dd[@class = 'titre']"/></fo:inline>
                    <xsl:text>, </xsl:text>
                    <xsl:for-each select="xhtml:dd[count(@class) = 0]">
                        <xsl:value-of select="."/>
                        <xsl:if test="position() != last()">
                            <xsl:text>, </xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                    <xsl:apply-templates select="xhtml:dd[@class = 'couverture']/*" />
                </fo:block>
            </fo:list-item-body>
        </fo:list-item>
    </xsl:for-each>
</xsl:template>
  
</xsl:stylesheet>