1 <?xml version='1.0' encoding='utf-8'?>
3 <!-- XHTML-to-XHTML converter by Fletcher Penney
4 specifically designed for use with MultiMarkdown created XHTML
6 Adds a Table of Contents to the top of the XHTML document,
7 and adds linkbacks from h1 and h2's.
9 Also, an example of the sorts of things that can be done to customize
10 the XHTML output of MultiMarkdown.
12 MultiMarkdown Version 2.0.b5
14 $Id: xhtml-toc.xslt 479 2008-01-12 23:04:13Z fletcher $
16 TODO: If a section has no children, a "<ol></ol>" is generated, which is invalid
20 # Copyright (C) 2007-2008 Fletcher T. Penney <fletcher@fletcherpenney.net>
22 # This program is free software; you can redistribute it and/or modify
23 # it under the terms of the GNU General Public License as published by
24 # the Free Software Foundation; either version 2 of the License, or
25 # (at your option) any later version.
27 # This program is distributed in the hope that it will be useful,
28 # but WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 # GNU General Public License for more details.
32 # You should have received a copy of the GNU General Public License
33 # along with this program; if not, write to the
34 # Free Software Foundation, Inc.
35 # 59 Temple Place, Suite 330
36 # Boston, MA 02111-1307 USA
41 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
42 xmlns:xhtml="http://www.w3.org/1999/xhtml"
43 xmlns="http://www.w3.org/1999/xhtml"
44 exclude-result-prefixes="xhtml xsl"
47 <xsl:variable name="newline">
52 <xsl:output method='xml' version="1.0" encoding='utf-8' doctype-public="-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" doctype-system="http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" indent="no"/>
54 <!-- the identity template, based on http://www.xmlplease.com/xhtmlxhtml -->
55 <xsl:template match="@*|node()">
57 <xsl:apply-templates select="@*|node()"/>
61 <!-- adjust the body to add a ToC -->
62 <!-- TODO: Will need to move this to just before first <h1> to allow for introductory comments -->
63 <xsl:template match="xhtml:body">
65 <xsl:value-of select="$newline"/>
66 <h1>Table of Contents</h1>
67 <xsl:value-of select="$newline"/>
69 <xsl:apply-templates select="xhtml:h1" mode="ToC"/>
70 <xsl:value-of select="$newline"/>
72 <xsl:apply-templates select="@*|node()"/>
77 <!-- create ToC entry -->
78 <xsl:template match="xhtml:h1" mode="ToC">
79 <xsl:value-of select="$newline"/>
80 <xsl:variable name="link">
81 <xsl:value-of select="@id"/>
83 <xsl:variable name="myId">
84 <xsl:value-of select="generate-id(.)"/>
87 <a id="ToC-{$link}" href="#{$link}">
88 <xsl:apply-templates select="node()"/>
90 <xsl:if test="following::xhtml:h2[1][preceding::xhtml:h1[1]]">
91 <xsl:value-of select="$newline"/>
93 <xsl:apply-templates select="following::xhtml:h2[preceding::xhtml:h1[1][generate-id() = $myId]]" mode="ToC"/>
94 <xsl:value-of select="$newline"/>
96 <xsl:value-of select="$newline"/>
101 <xsl:template match="xhtml:h2" mode="ToC">
102 <xsl:value-of select="$newline"/>
103 <xsl:variable name="link">
104 <xsl:value-of select="@id"/>
107 <a id="ToC-{$link}" href="#{$link}">
108 <xsl:apply-templates select="node()"/>
113 <!-- h1 and h2's should point back to the ToC for easy navigation -->
114 <xsl:template match="xhtml:h1">
115 <xsl:variable name="link">
116 <xsl:value-of select="@id"/>
119 <xsl:apply-templates select="@*|node()"/>
120 <a href="#ToC-{$link}"> ↩</a>
124 <xsl:template match="xhtml:h2">
125 <xsl:variable name="link">
126 <xsl:value-of select="@id"/>
129 <xsl:apply-templates select="@*|node()"/>
130 <a href="#ToC-{$link}"> ↩</a>