<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  version="1.0">

  <xsl:output
    method="xml"
    indent="yes"/>

  <xsl:param name="fonts">
    <name family="ProseAntique" style="normal" weight="normal"/>
    <address family="Eterna" style="normal" weight="normal"/>
    <label family="Gazette" style="normal" weight="normal"/>
    <heading family="Kane" style="normal" weight="bold"/>
    <sub-heading family="TopHat" style="italic" weight="normal"/>
    <text family="GaramandClassic" style="normal" weight="300"/>
    <bold family="GaramandClassic" style="normal" weight="bold"/>
    <extra family="AncientScript" style="normal" weight="normal"/>
    <!-- extra family="VanDijk" style="normal" weight="normal"/ -->
  </xsl:param>
  
  <xsl:template name="font">
    <xsl:param name="font">
      <xsl:copy-of select="$fonts/text"/>
    </xsl:param>
    <xsl:attribute name="font-family">
      <xsl:value-of select="$font/@family"/>
    </xsl:attribute>
    <xsl:attribute name="font-weight">
      <xsl:value-of select="$font/@weight"/>
    </xsl:attribute>
    <xsl:attribute name="font-style">
      <xsl:value-of select="$font/@style"/>
    </xsl:attribute>
  </xsl:template>


  <xsl:template match="/">
    <fo:root>
      <xsl:call-template name="layout-master-set"/>
      <xsl:call-template name="page-sequence"/>
    </fo:root>
  </xsl:template>
  

  <xsl:template name="layout-master-set">
    <fo:layout-master-set>
      <fo:simple-page-master master-name="CV-cover"
	page-height="29.1cm" page-width="21cm"
	margin-top="2cm" margin-bottom="2cm"
	margin-left="2cm" margin-right="2cm">
	<fo:region-body/>
      </fo:simple-page-master>
      
      <fo:simple-page-master master-name="CV-extra"
	page-height="29.1cm" page-width="21cm"
	margin-top="1.5cm" margin-bottom="2cm"
	margin-left="2cm" margin-right="2cm">
	<fo:region-body margin-top="1.5cm"/>
	<fo:region-before region-name="header" extent="1.5cm"/>
      </fo:simple-page-master>
      
      <fo:page-sequence-master master-name="CV-pages">
	<fo:repeatable-page-master-alternatives>
	  <fo:conditional-page-master-reference
	    master-reference="CV-cover"
	    page-position="first"/>
	  <fo:conditional-page-master-reference
	    master-reference="CV-extra"
	    page-position="rest"/>
	</fo:repeatable-page-master-alternatives>
      </fo:page-sequence-master>
    </fo:layout-master-set>
  </xsl:template>


  <xsl:template name="page-sequence">
    <fo:page-sequence master-reference="CV-pages">
      <xsl:call-template name="header"/>

      <fo:flow flow-name="xsl-region-body">
	<xsl:call-template name="address"/>

	<xsl:call-template name="simple-section">
	  <xsl:with-param name="heading">Objective</xsl:with-param>
	  <xsl:with-param name="body" select="/resume/objective"/>
	</xsl:call-template>

	<xsl:call-template name="simple-section">
	  <xsl:with-param name="heading">Summary</xsl:with-param>
	  <xsl:with-param name="body" select="/resume/summary"/>
	</xsl:call-template>

	<xsl:call-template name="nested-section">
	  <xsl:with-param name="heading">Technical Skills</xsl:with-param>
	  <xsl:with-param name="body" select="/resume/technical-skills"/>
	</xsl:call-template>

	<xsl:call-template name="accomplishments"/>

	<xsl:call-template name="calendar-section">
	  <xsl:with-param name="heading">Work Experience</xsl:with-param>
	  <xsl:with-param name="body" select="/resume/experience"/>
	</xsl:call-template>

	<xsl:call-template name="calendar-section">
	  <xsl:with-param name="heading">Education</xsl:with-param>
	  <xsl:with-param name="body" select="/resume/education"/>
	</xsl:call-template>

	<fo:block
	  space-before="8pc" font-size="9pt"
	  text-align="center" vertical-align="bottom">
	  <xsl:call-template name="font">
	    <xsl:with-param name="font" select="$fonts/extra"/>
	  </xsl:call-template>
	  This document was generated using W3C-compliant XML, XSLT,
	  and XSL:FO. The sources can be downloaded from
	  http://cyber-rush.org/drr/CV
	</fo:block>

      </fo:flow>
    </fo:page-sequence>
  </xsl:template>


  <xsl:template name="header">
    <fo:static-content flow-name="header">
      <fo:block
	keep-together.within-line="always"
	text-align="justify"
	font-size="12pt">
	<fo:inline>
	  <xsl:call-template name="font">
	    <xsl:with-param name="font" select="$fonts/name"/>
	  </xsl:call-template>
	  <xsl:value-of select="/resume/who/name"/>
	</fo:inline>
	<fo:leader
	  leader-length.optimum="90%"
	  leader-pattern="rule"
	  rule-thickness="1pt"
	  vertical-align="2pt"/>
	<fo:inline text-align="right">
	  <xsl:call-template name="font">
	    <xsl:with-param name="font" select="$fonts/address"/>
	  </xsl:call-template>
	  <fo:inline>
	    <xsl:call-template name="font">
	      <xsl:with-param name="font" select="$fonts/label"/>
	    </xsl:call-template><!--
	    -->mailto: </fo:inline>
	  <fo:inline>
	    <xsl:value-of select="/resume/who/email"/>
	  </fo:inline>
	</fo:inline>
      </fo:block>
    </fo:static-content>
  </xsl:template>


  <xsl:template name="address">
    <fo:block font-size="12pt"> 
      <xsl:call-template name="font">
	<xsl:with-param name="font" select="$fonts/address"/>
      </xsl:call-template>
      <fo:block text-align="center" font-size="16pt">
	<xsl:call-template name="font">
	  <xsl:with-param name="font" select="$fonts/name"/>
	</xsl:call-template>
	<xsl:value-of select="/resume/who/name"/>
      </fo:block>
      <xsl:for-each select="/resume/who/address">
	<fo:block text-align="center">
	  <xsl:value-of select="."/>
	</fo:block>
      </xsl:for-each>
      <xsl:call-template name="labelled">
	<xsl:with-param name="label">email:</xsl:with-param>
	<xsl:with-param name="text">
	  <xsl:value-of select="/resume/who/email"/>
	</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="labelled">
	<xsl:with-param name="label">mobile:</xsl:with-param>
	<xsl:with-param name="text">
	  <xsl:value-of select="/resume/who/mobile"/>
	</xsl:with-param>
      </xsl:call-template>
      <xsl:call-template name="labelled">
	<xsl:with-param name="label">phone:</xsl:with-param>
	<xsl:with-param name="text">
	  <xsl:value-of select="/resume/who/phone"/>
	</xsl:with-param>
      </xsl:call-template>
    </fo:block>
  </xsl:template>


  <xsl:template name="labelled">
    <xsl:param name="label">label:</xsl:param>
    <xsl:param name="text">text</xsl:param>
    <fo:block text-align="center">
      <fo:inline>
	<xsl:call-template name="font">
	  <xsl:with-param name="font" select="$fonts/label"/>
	</xsl:call-template>
	<xsl:value-of select="$label"/>
      </fo:inline>
      <fo:inline>
	<xsl:value-of select="$text"/>
      </fo:inline>
    </fo:block>
  </xsl:template>


  <xsl:template name="heading">
    <xsl:param name="text">heading</xsl:param>
    <fo:block
      text-align="justify"
      keep-together.within-line="always"
      font-size="14pt">
      <xsl:call-template name="font">
	<xsl:with-param name="font" select="$fonts/heading"/>
      </xsl:call-template>
      <fo:inline text-align="left">
	<xsl:value-of select="$text"/>
      </fo:inline>
      <fo:leader
	text-align="justify"
	vertical-align="0.27em"
	leader-pattern="rule"
	leader-length.optimum="90%"
	rule-thickness="0.9pt"/>
    </fo:block>
  </xsl:template>
  

  <xsl:template name="sub-heading">
    <xsl:param name="text">sub-heading</xsl:param>
      <fo:block font-size="14pt">
      <xsl:call-template name="font">
	<xsl:with-param name="font" select="$fonts/sub-heading"/>
      </xsl:call-template>
      <xsl:value-of select="$text"/>
    </fo:block>
  </xsl:template>


  <xsl:template name="simple-section">
    <xsl:param name="heading">heading</xsl:param>
    <xsl:param name="body">body</xsl:param>
    <fo:block space-before="1pc">
      <xsl:call-template name="heading">
	<xsl:with-param name="text">
	  <xsl:value-of select="$heading"/>
	</xsl:with-param>
      </xsl:call-template>
      <fo:block font-size="10pt"
        text-align="justify"
        space-before="0.5pc">
	<xsl:call-template name="font">
	  <xsl:with-param name="font" select="$fonts/text"/>
	</xsl:call-template>
	<xsl:value-of select="$body"/>
      </fo:block>
    </fo:block>
  </xsl:template>


  <xsl:template name="nested-section">
    <xsl:param name="heading">heading</xsl:param>
    <xsl:param name="body">body</xsl:param>
    <fo:block space-before="1pc">
      <xsl:call-template name="heading">
	<xsl:with-param name="text">
	  <xsl:value-of select="$heading"/>
	</xsl:with-param>
      </xsl:call-template>
      
      <xsl:for-each select="$body/*">
	<xsl:call-template name="sub-heading">
	  <xsl:with-param name="text">
	    <xsl:value-of select="@heading"/>
	  </xsl:with-param>
	</xsl:call-template>
	<fo:block
          space-before="0.7pc"
          font-size="10pt"
	  start-indent="3pc" text-align="justify">
	  <xsl:call-template name="font">
	    <xsl:with-param name="font" select="$fonts/text"/>
	  </xsl:call-template>
	  <xsl:apply-templates select="."/>
	</fo:block>
      </xsl:for-each>
    </fo:block>
  </xsl:template>
  

  <xsl:template name="calendar-section">
    <xsl:param name="heading">heading</xsl:param>
    <xsl:param name="body">body</xsl:param>
    <fo:block space-before="1pc"/>
    <xsl:call-template name="heading">
      <xsl:with-param name="text"><xsl:value-of select="$heading"/></xsl:with-param>
    </xsl:call-template>
    <fo:block
      font-weight="300" font-size="10pt"
      text-align="justify"
      space-before="0.3pc">
      <xsl:call-template name="font">
	<xsl:with-param name="font" select="$fonts/text"/>
      </xsl:call-template>

      <fo:table table-layout="fixed" inline-progression-dimension="100%">
	<fo:table-column column-width="3pc"/>
	<fo:table-column/>
	<fo:table-body>
	  <xsl:for-each select="$body/activity">
	    <fo:table-row>
	      <fo:table-cell>
		<fo:block
		  margin-right="0.5em"
		  text-align="right">
		  <xsl:call-template name="font">
		    <xsl:with-param name="font" select="$fonts/label"/>
		  </xsl:call-template>
		  <xsl:choose>
		    <xsl:when test="@id">
		      [<xsl:value-of select="@id"/>]
		    </xsl:when>
		    <xsl:when test="@at">
		      (<xsl:value-of select="@at"/>)
		    </xsl:when>
		    <xsl:otherwise>*</xsl:otherwise>
		  </xsl:choose>
		</fo:block>
	      </fo:table-cell>
	      <fo:table-cell padding-after="0.1pc">
		<fo:block text-align="justify">
		  <fo:inline text-align="left">
		    <xsl:value-of select="@desc"/>
		  </fo:inline>
		  <fo:leader
		    leader-length.minimum="5em"
		    leader-length.optimum="80%"
		    leader-length.maximum="100%"
		    leader-pattern="dots"/>
		  <fo:inline text-align="right">
		    <xsl:for-each select="*">
		      [<xsl:value-of select="."/>]
		    </xsl:for-each>
		  </fo:inline>
		</fo:block>
	      </fo:table-cell>
	    </fo:table-row>
	  </xsl:for-each>
	</fo:table-body>
      </fo:table>

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


  <xsl:template name="accomplishments">
    <fo:block space-before="1pc"/>
    <xsl:call-template name="heading">
      <xsl:with-param name="text">Accomplishments</xsl:with-param>
    </xsl:call-template>

    <fo:block space-before="0.5pc"/>

    <xsl:for-each select="/resume/accomplishments/*">
      <xsl:call-template name="sub-heading">
	<xsl:with-param name="text">
	  <xsl:value-of select="@heading"/>
	</xsl:with-param>
      </xsl:call-template>
      <fo:block
	space-before="1pc" font-size="10pt">
	<xsl:call-template name="font">
	  <xsl:with-param name="font" select="$fonts/address"/>
	</xsl:call-template>

	<fo:table
          table-layout="fixed"
          inline-progression-dimension="100%">
	  <fo:table-column column-width="3pc"/>
	  <fo:table-column/>
	  <fo:table-body
	    >
	    <xsl:for-each select="*">
	      <fo:table-row
		>

		<!-- Job label formatting code -->
		<fo:table-cell display-align="before">
		  <fo:block
		    margin-right="0.5em"
		    text-align="right">
		    <xsl:call-template name="font">
		      <xsl:with-param name="font" select="$fonts/label"/>
		    </xsl:call-template>
		    <xsl:choose>
		      <xsl:when test="@at">
			[<xsl:value-of select="@at"/>]
		      </xsl:when>
		      <xsl:otherwise>*</xsl:otherwise>
		    </xsl:choose>
		  </fo:block>
		</fo:table-cell>

		<!-- Job description -->
		<fo:table-cell padding-after="0.6pc">
		  <fo:block text-align="justify">
		    <xsl:call-template name="font">
		      <xsl:with-param name="font" select="$fonts/text"/>
		    </xsl:call-template>
		    <xsl:value-of select="."/>
		  </fo:block>
		</fo:table-cell>
	      </fo:table-row>
	    </xsl:for-each>
	  </fo:table-body>
	</fo:table>

      </fo:block>
    </xsl:for-each>
  </xsl:template>


  <xsl:template match="item">
    <fo:inline>
      <xsl:value-of select="."/>
      <!-- xsl:message>folllowing items <xsl:value-of select="count(following-sibling::node())"/></xsl:message -->
      <xsl:choose>
	<xsl:when test="count(following-sibling::node()) &gt; 1">, </xsl:when>
      </xsl:choose>
    </fo:inline>
  </xsl:template>


</xsl:stylesheet>
