Skip to content

Commit

Permalink
Fixed headers and footer for document with one section
Browse files Browse the repository at this point in the history
  • Loading branch information
ottoville committed Apr 18, 2016
1 parent e82f47b commit 9fd4714
Showing 1 changed file with 84 additions and 90 deletions.
174 changes: 84 additions & 90 deletions pages.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<!--<xsl:if test="$selector/w:r|$selector/w:tr">--> <!-- Filter empty pages where text is marked to continue on next page but there are no more text -->
<div>
<xsl:attribute name="class" select="$sectionnumber" />
<xsl:if test='$firstpageheader'>
<xsl:if test="string-length($firstpageheader)">
<header>
<xsl:attribute name="class">first</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$firstpageheader]/@Target,base-uri()))/*/*">
Expand All @@ -44,7 +44,7 @@
</xsl:apply-templates>
</header>
</xsl:if>
<xsl:if test='$evenheader'>
<xsl:if test="string-length($evenheader)">
<header>
<xsl:attribute name="class">even</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$evenheader]/@Target,base-uri()))/*/*">
Expand All @@ -53,7 +53,7 @@
</xsl:apply-templates>
</header>
</xsl:if>
<xsl:if test='$defaultheader'>
<xsl:if test="string-length($defaultheader)">
<header>
<xsl:attribute name="class">default</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$defaultheader]/@Target,base-uri()))/*/*">
Expand All @@ -75,7 +75,7 @@
</xsl:apply-templates>
<!--</xsl:if> -->
</xsl:for-each>
<xsl:if test='$firstpagefooter'>
<xsl:if test="string-length($firstpagefooter)">
<footer>
<xsl:attribute name="class">first</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$firstpagefooter]/@Target,base-uri()))/*/*">
Expand All @@ -84,7 +84,7 @@
</xsl:apply-templates>
</footer>
</xsl:if>
<xsl:if test='$evenfooter'>
<xsl:if test="string-length($evenfooter)">
<footer>
<xsl:attribute name="class">even</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$evenfooter]/@Target,base-uri()))/*/*">
Expand All @@ -93,7 +93,7 @@
</xsl:apply-templates>
</footer>
</xsl:if>
<xsl:if test='$defaultfooter'>
<xsl:if test="string-length($defaultfooter)">
<footer>
<xsl:attribute name="class">default</xsl:attribute>
<xsl:apply-templates select="document(resolve-uri(document($reldocument)/*/*[@Id=$defaultfooter]/@Target,base-uri()))/*/*">
Expand Down Expand Up @@ -161,15 +161,12 @@
<xsl:variable name="paddingbottom" select="number($sectionselector[1]/w:pgMar/@w:bottom)" />
<xsl:variable name="paddingright" select="number($sectionselector[1]/w:pgMar/@w:right)" />
<xsl:variable name="paddingleft" select="number($sectionselector[1]/w:pgMar/@w:left)" />
<xsl:variable name="prevsection" select="preceding-sibling::*[w:pPr/w:sectPr][1]" />

<xsl:variable name="prevdefaultheader" select="preceding-sibling::*[w:pPr/w:sectPr/w:headerReference[@w:type='default']][1]/w:pPr/w:sectPr[1]/w:headerReference[@w:type='default']/@r:id" />

<xsl:variable name="prevsection" select="preceding-sibling::*[w:pPr/w:sectPr][1]" />
<xsl:variable name="sectionnumber" select="concat('section',position())" />
<xsl:variable name="defaultheader">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:headerReference[@w:type='default']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:headerReference[@w:type='default']/@r:id" />
<xsl:when test="$sectionselector/w:headerReference[@w:type='default']/@r:id">
<xsl:value-of select="$sectionselector/w:headerReference[@w:type='default']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:headerReference[@w:type='default']][1]/w:pPr/w:sectPr[1]/w:headerReference[@w:type='default']/@r:id" />
Expand All @@ -178,8 +175,8 @@
</xsl:variable>
<xsl:variable name="defaultfooter">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:footerReference[@w:type='default']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:footerReference[@w:type='default']/@r:id" />
<xsl:when test="$sectionselector/w:footerReference[@w:type='default']/@r:id">
<xsl:value-of select="$sectionselector/w:footerReference[@w:type='default']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:footerReference[@w:type='default']][1]/w:pPr/w:sectPr[1]/w:footerReference[@w:type='default']/@r:id" />
Expand All @@ -188,8 +185,8 @@
</xsl:variable>
<xsl:variable name="evenheader">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:headerReference[@w:type='even']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:headerReference[@w:type='even']/@r:id" />
<xsl:when test="$sectionselector/w:headerReference[@w:type='even']/@r:id">
<xsl:value-of select="$sectionselector/w:headerReference[@w:type='even']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:headerReference[@w:type='even']][1]/w:pPr/w:sectPr[1]/w:headerReference[@w:type='even']/@r:id" />
Expand All @@ -198,8 +195,8 @@
</xsl:variable>
<xsl:variable name="evenfooter">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:footerReference[@w:type='even']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:footerReference[@w:type='even']/@r:id" />
<xsl:when test="$sectionselector/w:footerReference[@w:type='even']/@r:id">
<xsl:value-of select="$sectionselector/w:footerReference[@w:type='even']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:footerReference[@w:type='even']][1]/w:pPr/w:sectPr[1]/w:footerReference[@w:type='even']/@r:id" />
Expand All @@ -208,8 +205,8 @@
</xsl:variable>
<xsl:variable name="firstpageheader">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:headerReference[@w:type='first']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:headerReference[@w:type='first']/@r:id" />
<xsl:when test="$sectionselector/w:headerReference[@w:type='first']/@r:id">
<xsl:value-of select="$sectionselector/w:headerReference[@w:type='first']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:headerReference[@w:type='first']][1]/w:pPr/w:sectPr[1]/w:headerReference[@w:type='first']/@r:id" />
Expand All @@ -218,86 +215,83 @@
</xsl:variable>
<xsl:variable name="firstpagefooter">
<xsl:choose>
<xsl:when test="w:pPr/w:sectPr[1]/w:footerReference[@w:type='first']/@r:id">
<xsl:value-of select="w:pPr/w:sectPr[1]/w:footerReference[@w:type='first']/@r:id" />
<xsl:when test="$sectionselector/w:footerReference[@w:type='first']/@r:id">
<xsl:value-of select="$sectionselector/w:footerReference[@w:type='first']/@r:id" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="preceding-sibling::*[w:pPr/w:sectPr/w:footerReference[@w:type='first']][1]/w:pPr/w:sectPr[1]/w:footerReference[@w:type='first']/@r:id" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--<section>-->
<hr>
<xsl:attribute name="class" select="$sectionnumber" />
<xsl:attribute name="headerid" select="w:pPr/w:sectPr[1]/w:headerReference[@w:type='even']/@r:id" />
<xsl:if test="w:pPr/w:sectPr[1]/w:titlePg">
<xsl:if test="$sectionselector/w:titlePg">
<xsl:attribute name="data-firstheader">true</xsl:attribute>
</xsl:if>
</hr>
<!-- Apply template to pages that belongs to this section -->
<xsl:choose>
<xsl:when test="not($prevsection)"> <!--first section or document with one section -->
<xsl:for-each select="(preceding-sibling::*|current())[w:r/w:br[@w:type='page']]|(preceding-sibling::*|current())[last()]"> <!-- always last element of page -->
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="preceding-sibling::*|current()" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="prevpage" select="(preceding-sibling::*|current())[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:when test="w:pPr/w:sectPr">
<xsl:for-each select="(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[w:r/w:br[@w:type='page']]|(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[last()]">
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)]" />
<xsl:with-param name="prevpage" select="(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise> <!-- Last section -->
<xsl:for-each select="($prevsection/following-sibling::*)[w:r/w:br[@w:type='page']]|($prevsection/following-sibling::*)[last()]">
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="$prevsection/following-sibling::*" />
<xsl:with-param name="prevpage" select="($prevsection/following-sibling::*)[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<style>
<xsl:value-of select="concat('div.',$sectionnumber,' {')" />
<xsl:value-of select="concat('width:',(number($sectionselector[1]/w:pgSz/@w:w) - $paddingright - $paddingleft) div 20,'pt;')"/>
<xsl:value-of select="concat('min-height:',(number($sectionselector[1]/w:pgSz/@w:h) - $paddingtop - $paddingbottom) div 20,'pt;')"/>
<xsl:value-of select="concat('padding:',$paddingtop div 20,'pt ',$paddingright div 20,'pt ',$paddingbottom div 20,'pt ',$paddingleft div 20,'pt;')"/>
}
</style>
<!--</section>-->
<!-- Apply template to pages that belongs to this section -->
<xsl:choose>
<xsl:when test="not($prevsection)"> <!--first section or document with one section -->
<xsl:for-each select="(preceding-sibling::*|current())[w:r/w:br[@w:type='page']]|(preceding-sibling::*|current())[last()]"> <!-- always last element of page -->
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="preceding-sibling::*|current()" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="prevpage" select="(preceding-sibling::*|current())[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:when test="w:pPr/w:sectPr">
<xsl:for-each select="(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[w:r/w:br[@w:type='page']]|(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[last()]">
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)]" />
<xsl:with-param name="prevpage" select="(current()|./preceding-sibling::*[count(./preceding-sibling::*) &gt; count($prevsection/preceding-sibling::*)])[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:when>
<xsl:otherwise> <!-- Last section -->
<xsl:for-each select="($prevsection/following-sibling::*)[w:r/w:br[@w:type='page']]|($prevsection/following-sibling::*)[last()]">
<xsl:call-template name="pages">
<xsl:with-param name="reldocument" select="$reldocument" />
<xsl:with-param name="themefile" select="$themefile" />
<xsl:with-param name="sectionselector" select="$sectionselector" />
<xsl:with-param name="selector" select="$prevsection/following-sibling::*" />
<xsl:with-param name="prevpage" select="($prevsection/following-sibling::*)[w:r/w:br[@w:type='page']][count(./preceding-sibling::*) &lt; count(current()/preceding-sibling::*)][last()]" />
<xsl:with-param name="defaultheader" select="$defaultheader" />
<xsl:with-param name="defaultfooter" select="$defaultfooter" />
<xsl:with-param name="evenheader" select="$evenheader" />
<xsl:with-param name="evenfooter" select="$evenfooter" />
<xsl:with-param name="firstpageheader" select="$firstpageheader" />
<xsl:with-param name="firstpagefooter" select="$firstpagefooter" />
<xsl:with-param name="sectionnumber" select="$sectionnumber" />
</xsl:call-template>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<style>
<xsl:value-of select="concat('div.',$sectionnumber,' {')" />
<xsl:value-of select="concat('width:',(number($sectionselector[1]/w:pgSz/@w:w) - $paddingright - $paddingleft) div 20,'pt;')"/>
<xsl:value-of select="concat('min-height:',(number($sectionselector[1]/w:pgSz/@w:h) - $paddingtop - $paddingbottom) div 20,'pt;')"/>
<xsl:value-of select="concat('padding:',$paddingtop div 20,'pt ',$paddingright div 20,'pt ',$paddingbottom div 20,'pt ',$paddingleft div 20,'pt;')"/>
}
</style>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 9fd4714

Please sign in to comment.