Skip to content

Instantly share code, notes, and snippets.

@keithboone
Last active March 29, 2020 23:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithboone/b2933062562f7446320195810a8896df to your computer and use it in GitHub Desktop.
Save keithboone/b2933062562f7446320195810a8896df to your computer and use it in GitHub Desktop.
Generate a Single Page document containing all TOC Pages in a FHIR IG
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:html="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xs"
version="2.0">
<!-- This stylesheet should be pointed to the toc.html page
created by your IG build. It will process that table of contents
entries in that file to generate a single page html file which
can subsequently be used to generate a pdf.
-->
<xsl:template match="/">
<xsl:call-template name='readtoc'/>
</xsl:template>
<!-- this template identifies the links,and passes them off to the process-toc template for handling -->
<xsl:template name="readtoc">
<html>
<body>
<!-- Put the page header at the top of the document -->
<xsl:copy-of select="//html:div[@id='segment-header']"/>
<!-- And the history note -->
<xsl:copy-of select="//html:p[@id='publish-box']"/>
<!-- Then copy the pages in the TOC in order -->
<xsl:apply-templates select="//html:div[@id='segment-content']//html:a" mode="process-toc"/>
<!-- Put the page footer at the bottom of the document -->
<xsl:copy-of select="//html:div[@id='segment-footer']"/>
</body>
</html>
</xsl:template>
<!-- read the filee into a document. This is why you need TIDY, because the XHTML
isn't QUITE XHTML (though it is remarkably close). -->
<xsl:template match="html:a" mode="process-toc">
<xsl:variable name="doc" select="document(@href)"/>
<xsl:apply-templates select="$doc//html:div[@id='segment-content']" mode="copy"/>
</xsl:template>
<!-- do nothing with the publish box in copy mode we already handled it -->
<xsl:template match="*[@id='publish-box']" mode="copy"/>
<!-- Try to fix up @hrefs in links. Eventually, this should be applied
to other stuff to, but this is simply a starter -->
<xsl:template match="@href" mode="copy">
<xsl:choose>
<xsl:when test="contains(.,':') or starts-with(.,'/')">
<xsl:copy/>
</xsl:when>
<xsl:when test="contains(., '#')">
<xsl:attribute name="href">
<xsl:value-of select='concat("#", substring-after(.,"#"))'/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href">
<xsl:value-of select='"#"'/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- And the copy template just passes everything else on through -->
<xsl:template match="@*|node()" mode="copy">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="copy"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment