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

<xsl:variable name="base-font">10</xsl:variable>

<xsl:attribute-set name="head">
  <xsl:attribute name="font-weight">bold</xsl:attribute>
  <xsl:attribute name="font-variant">small-caps</xsl:attribute>
  <xsl:attribute name="keep-with-next">always</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="head1" use-attribute-sets="head">
  <xsl:attribute name="font-size">200%</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="head2" use-attribute-sets="head">
  <xsl:attribute name="font-size">150%</xsl:attribute>
  <xsl:attribute name="border-top">1.0pt solid black</xsl:attribute>
  <xsl:attribute name="border-bottom">0.5pt solid black</xsl:attribute>
  <xsl:attribute name="padding-bottom">-2pt</xsl:attribute>
  <xsl:attribute name="margin-bottom">6pt</xsl:attribute>
</xsl:attribute-set>

<xsl:attribute-set name="head3" use-attribute-sets="head">
  <xsl:attribute name="font-size">100%</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/resume">
  <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" font-family="Times">
    <xsl:attribute name="font-size"><xsl:value-of select="concat($base-font, 'pt')"/></xsl:attribute>
    <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-height="11in" page-width="8.5in">
        <fo:region-body
          margin-left="0.75in"
          margin-right="0.75in"
          margin-top="0.5in"
          margin-bottom="0.75in"/>
        <fo:region-after extent="0.5in" margin-left="0.75in" />
      </fo:simple-page-master>
    </fo:layout-master-set>

    <fo:declarations>
      <x:xmpmeta xmlns:x="adobe:ns:meta/">
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
          <rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
            <dc:title>
              Resume - <xsl:value-of select="person/name"/>
            </dc:title>
            <dc:creator><xsl:value-of select="person/name"/></dc:creator>
          </rdf:Description>
        </rdf:RDF>
      </x:xmpmeta>
    </fo:declarations>

    <fo:page-sequence master-reference="my-page">

    <fo:static-content flow-name="xsl-region-after" font-size="66%">
      <fo:table width="7in" table-layout="fixed" margin="0 0.75in">
        <fo:table-column column-width="2in" />
        <fo:table-column column-width="3in" />
        <fo:table-column column-width="2in" />
        <fo:table-body>
          <fo:table-row>
            <fo:table-cell margin="0">
              <fo:block text-align="left">
                <xsl:value-of select="person/url" />
              </fo:block>
            </fo:table-cell>
            <fo:table-cell>
              <fo:block text-align="center" margin="0">
                <xsl:value-of select="person/name"/>
                <xsl:for-each select="person/designations/designation">, <xsl:value-of select="."/></xsl:for-each>
              </fo:block>
            </fo:table-cell>
            <fo:table-cell margin="0">
              <fo:block text-align="right">
                Page <fo:page-number/> of <fo:page-number-citation ref-id="terminator" />
              </fo:block>
            </fo:table-cell>
          </fo:table-row>
        </fo:table-body>
      </fo:table>
    </fo:static-content>
      <fo:flow flow-name="xsl-region-body">
        <fo:block text-align="center"
                  margin-bottom="12pt">
          <fo:block xsl:use-attribute-sets="head1">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string" select="person/name"/>
            </xsl:call-template>
            <xsl:for-each select="person/designations/designation">
              <xsl:text>, </xsl:text>
              <xsl:call-template name="fake-small-caps">
                <xsl:with-param name="string" select="."/>
              </xsl:call-template>
            </xsl:for-each>
          </fo:block>
          <fo:block>
            <xsl:value-of select="person/address/street" />
            &#8226;
            <xsl:value-of select="person/address/city" />
            <xsl:if test="person/address/province">, <xsl:value-of select="person/address/province" /></xsl:if>
            &#8226;
            <xsl:value-of select="person/address/postalcode" />
          </fo:block>
          <xsl:choose>
            <xsl:when test="person/phone and person/email">
              <fo:block>
                <xsl:value-of select="person/phone" />
                &#8226;
                <xsl:value-of select="person/email" />
              </fo:block>
            </xsl:when>
            <xsl:when test="person/phone or person/email">
              <fo:block>
                <xsl:value-of select="person/phone" />
                <xsl:value-of select="person/email" />
              </fo:block>
            </xsl:when>
          </xsl:choose>
        </fo:block>

        <xsl:if test="summary">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Summary
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:block margin-bottom="10pt">
            <xsl:apply-templates select="summary" />
          </fo:block>
        </xsl:if>

        <xsl:if test="objective">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Objective
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:block margin-bottom="10pt">
            <xsl:apply-templates select="objective" />
          </fo:block>
        </xsl:if>

        <xsl:if test="education">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Education
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="education/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="experience">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Work Experience
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="experience/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="publications">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Publications
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <xsl:for-each select="publications/publication">
            <fo:block margin-bottom="6pt"><xsl:apply-templates select="." /></fo:block>
          </xsl:for-each>
        </xsl:if>

        <xsl:if test="accreditations">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Accreditations
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="accreditations/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="awards">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Awards &amp; Scholarships
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="awards/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="volunteer">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Volunteer Activities
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="volunteer/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="interests">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Other Interests
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="75%" />
            <fo:table-column column-width="25%" />
            <fo:table-body>
              <xsl:for-each select="interests/item">
                <xsl:apply-templates select="." />
              </xsl:for-each>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="skills">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                Skill Set
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <fo:table table-layout="fixed" width="100%">
            <fo:table-column column-width="50%" />
            <fo:table-column column-width="50%" />
            <fo:table-body>
              <fo:table-row>
                <fo:table-cell
                    keep-together.within-column="always"
                    padding-right="6pt"
                    padding-after="1em">
                  <xsl:for-each select="skills/item[(position() mod 2 = 1)]">
                    <fo:block keep-with-next="always"
                        text-indent="-2em"
                        start-indent="2em">
                      <fo:inline font-weight="bold">
                        <xsl:value-of select="title" />
                        <xsl:text>: </xsl:text>
                      </fo:inline>
                      <xsl:value-of select="description" />
                    </fo:block>
                  </xsl:for-each>
                </fo:table-cell>
                <fo:table-cell
                    keep-together.within-column="always"
                    padding-right="6pt"
                    padding-after="1em">
                  <xsl:for-each select="skills/item[(position() mod 2 = 0)]">
                    <fo:block keep-with-next="always"
                        text-indent="-2em"
                        start-indent="2em">
                      <fo:inline font-weight="bold">
                        <xsl:value-of select="title" />
                        <xsl:text>: </xsl:text>
                      </fo:inline>
                      <xsl:value-of select="description" />
                    </fo:block>
                  </xsl:for-each>
                </fo:table-cell>
              </fo:table-row>
            </fo:table-body>
          </fo:table>
        </xsl:if>

        <xsl:if test="references">
          <fo:block xsl:use-attribute-sets="head2">
            <xsl:call-template name="fake-small-caps">
              <xsl:with-param name="string">
                References
              </xsl:with-param>
            </xsl:call-template>
          </fo:block>
          <xsl:choose>
            <xsl:when test="count(references/item) > 0">
              <fo:table table-layout="fixed" width="100%">
                <fo:table-column column-width="75%" />
                <fo:table-column column-width="25%" />
                <fo:table-body>
                  <xsl:for-each select="references/item">
                    <xsl:apply-templates select="." />
                  </xsl:for-each>
                </fo:table-body>
              </fo:table>
            </xsl:when>
            <xsl:otherwise>
              <fo:block>
                References are available upon request.
              </fo:block>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
        <fo:block id="terminator" />
      </fo:flow>
    </fo:page-sequence>
  </fo:root>
</xsl:template>

<xsl:template match="address">
  <fo:block wrap-option="no-wrap" keep-together.within-column="always">
    <fo:block><xsl:value-of select="street" /></fo:block>
    <fo:block>
      <xsl:value-of select="city" />
      <xsl:if test="province">, <xsl:value-of select="province" /></xsl:if>
    </fo:block>
    <fo:block>
      <xsl:value-of select="postalcode" />
    </fo:block>
  </fo:block>
</xsl:template>

<xsl:template match="item" xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:table-row>
    <fo:table-cell
        number-rows-spanned="2"
        keep-together.within-column="always"
        padding-after="1em"
        padding-right="12pt">
      <fo:block xsl:use-attribute-sets="head3">
        <xsl:value-of select="title" />
      </fo:block>
      <xsl:if test="location/url and not(location/name)">
        <fo:block keep-with-next="always">
          <fo:basic-link>
            <xsl:attribute name="external-destination">url(<xsl:value-of select="location/url" />)</xsl:attribute>
            <xsl:value-of select="location/url" />
          </fo:basic-link>
        </fo:block>
      </xsl:if>
      <xsl:if test="location/name">
        <fo:block keep-with-next="always">
          <xsl:value-of select="location/name" />
        </fo:block>
        <xsl:if test="location/url">
          <fo:block keep-with-next="always">
            <fo:basic-link>
              <xsl:attribute name="external-destination">url(<xsl:value-of select="location/url" />)</xsl:attribute>
              <xsl:value-of select="location/url" />
            </fo:basic-link>
          </fo:block>
        </xsl:if>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="description/list">
          <fo:list-block
              provisional-distance-between-starts="12pt"
              provisional-label-separation="12pt"
              margin-left="3pt"
              keep-together.within-column="always">
            <xsl:for-each select="description/list/li">
              <fo:list-item>
                <fo:list-item-label end-indent="label-end()">
                  <fo:block>&#x2022;</fo:block>
                </fo:list-item-label>
                <fo:list-item-body start-indent="body-start()">
                  <fo:block><xsl:apply-templates select="." /></fo:block>
                </fo:list-item-body>
              </fo:list-item>
            </xsl:for-each>
          </fo:list-block>
        </xsl:when>
        <xsl:otherwise>
          <fo:block><xsl:apply-templates select="description" /></fo:block>
        </xsl:otherwise>
      </xsl:choose>
    </fo:table-cell>
    <fo:table-cell>
      <fo:block font-weight="bold">
        <xsl:choose>
          <xsl:when test="date/start">
            <xsl:value-of select="date/start" />
            &#8211;
            <xsl:value-of select="date/end" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="date" />
          </xsl:otherwise>
        </xsl:choose>
      </fo:block>
    </fo:table-cell>
  </fo:table-row>
  <fo:table-row>
    <fo:table-cell padding-after="1em">
      <xsl:choose>
        <xsl:when test="location/address">
          <xsl:apply-templates select="location/address" />
        </xsl:when>
        <xsl:otherwise>
          <fo:block></fo:block>
        </xsl:otherwise>
      </xsl:choose>
    </fo:table-cell>
  </fo:table-row>
</xsl:template>

<xsl:template name="fake-small-caps">
  <xsl:param name="string" />

  <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
  <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

  <xsl:if test="string-length($string) > 0">
    <xsl:variable name="f" select="substring($string, 1, 1)" />
    <xsl:variable name="r" select="substring($string, 2)" />

    <xsl:variable name="ft" select="translate($f, $lcletters, $ucletters)" />

    <xsl:choose>
      <xsl:when test="$f = $ft">
        <xsl:value-of select="$f" />
      </xsl:when>
      <xsl:otherwise>
        <fo:inline font-size="75%">
          <xsl:value-of select="$ft" />
        </fo:inline>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:call-template name="fake-small-caps">
      <xsl:with-param name="string" select="$r" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template match="b">
  <fo:inline font-weight="bold">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

<xsl:template match="i">
  <fo:inline font-style="italic">
    <xsl:apply-templates select="*|text()"/>
  </fo:inline>
</xsl:template>

</xsl:stylesheet>

