Accept attributes in inline image syntax
[multimarkdown] / XSLT / xhtml-toc.xslt
1 <?xml version='1.0' encoding='utf-8'?>
2
3 <!-- XHTML-to-XHTML converter by Fletcher Penney
4         specifically designed for use with MultiMarkdown created XHTML
5         
6         Adds a Table of Contents to the top of the XHTML document,
7         and adds linkbacks from h1 and h2's.
8         
9         Also, an example of the sorts of things that can be done to customize
10         the XHTML output of MultiMarkdown.
11         
12         MultiMarkdown Version 2.0.b5
13         
14         $Id: xhtml-toc.xslt 479 2008-01-12 23:04:13Z fletcher $
15
16         TODO: If a section has no children, a "<ol></ol>" is generated, which is invalid
17 -->
18
19 <!-- 
20 # Copyright (C) 2007-2008  Fletcher T. Penney <fletcher@fletcherpenney.net>
21 #
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.
26 #
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.
31 #
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
37 -->
38
39         
40 <xsl:stylesheet
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"
45         version="1.0">
46
47         <xsl:variable name="newline">
48 <xsl:text>
49 </xsl:text>
50         </xsl:variable>
51         
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"/>
53
54         <!-- the identity template, based on http://www.xmlplease.com/xhtmlxhtml -->
55         <xsl:template match="@*|node()">
56                 <xsl:copy>
57                         <xsl:apply-templates select="@*|node()"/>
58                 </xsl:copy>
59         </xsl:template>
60
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">
64                 <xsl:copy>
65                         <xsl:value-of select="$newline"/>
66                         <h1>Table of Contents</h1>
67                         <xsl:value-of select="$newline"/>
68                         <ol>
69                                 <xsl:apply-templates select="xhtml:h1" mode="ToC"/>
70                                 <xsl:value-of select="$newline"/>
71                         </ol>
72                         <xsl:apply-templates select="@*|node()"/>
73                 </xsl:copy>
74         </xsl:template>
75         
76         
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"/>
82                 </xsl:variable>
83                 <xsl:variable name="myId">
84                         <xsl:value-of select="generate-id(.)"/>
85                 </xsl:variable>
86                 <li>
87                         <a id="ToC-{$link}" href="#{$link}">
88                                 <xsl:apply-templates select="node()"/>
89                         </a>
90                         <xsl:if test="following::xhtml:h2[1][preceding::xhtml:h1[1]]">
91                                 <xsl:value-of select="$newline"/>
92                                 <ol>
93                                         <xsl:apply-templates select="following::xhtml:h2[preceding::xhtml:h1[1][generate-id() = $myId]]" mode="ToC"/>
94                                         <xsl:value-of select="$newline"/>
95                                 </ol>
96                                 <xsl:value-of select="$newline"/>
97                         </xsl:if>
98                 </li>
99         </xsl:template>
100
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"/>
105                 </xsl:variable>
106                 <li>
107                         <a id="ToC-{$link}" href="#{$link}">
108                                 <xsl:apply-templates select="node()"/>
109                         </a>
110                 </li>
111         </xsl:template>
112         
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"/>
117                 </xsl:variable>
118                 <xsl:copy>
119                         <xsl:apply-templates select="@*|node()"/>
120                         <a href="#ToC-{$link}">&#160;&#8617;</a>
121                 </xsl:copy>
122         </xsl:template>
123
124         <xsl:template match="xhtml:h2">
125                 <xsl:variable name="link">
126                         <xsl:value-of select="@id"/>
127                 </xsl:variable>
128                 <xsl:copy>
129                         <xsl:apply-templates select="@*|node()"/>
130                         <a href="#ToC-{$link}">&#160;&#8617;</a>
131                 </xsl:copy>
132         </xsl:template>
133         
134 </xsl:stylesheet>