forked from fletcher/MultiMarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxhtml-toc.xslt
134 lines (115 loc) · 4 KB
/
xhtml-toc.xslt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?xml version='1.0' encoding='utf-8'?>
<!-- XHTML-to-XHTML converter by Fletcher Penney
specifically designed for use with MultiMarkdown created XHTML
Adds a Table of Contents to the top of the XHTML document,
and adds linkbacks from h1 and h2's.
Also, an example of the sorts of things that can be done to customize
the XHTML output of MultiMarkdown.
MultiMarkdown Version 2.0.b6
$Id: xhtml-toc.xslt 499 2008-03-23 13:03:19Z fletcher $
TODO: If a section has no children, a "<ol></ol>" is generated, which is invalid
-->
<!--
# Copyright (C) 2007-2008 Fletcher T. Penney <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
-->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="xhtml xsl"
version="1.0">
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<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"/>
<!-- the identity template, based on http://www.xmlplease.com/xhtmlxhtml -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- adjust the body to add a ToC -->
<!-- TODO: Will need to move this to just before first <h1> to allow for introductory comments -->
<xsl:template match="xhtml:body">
<xsl:copy>
<xsl:value-of select="$newline"/>
<h1>Table of Contents</h1>
<xsl:value-of select="$newline"/>
<ol>
<xsl:apply-templates select="xhtml:h1" mode="ToC"/>
<xsl:value-of select="$newline"/>
</ol>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- create ToC entry -->
<xsl:template match="xhtml:h1" mode="ToC">
<xsl:value-of select="$newline"/>
<xsl:variable name="link">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:variable name="myId">
<xsl:value-of select="generate-id(.)"/>
</xsl:variable>
<li>
<a id="ToC-{$link}" href="#{$link}">
<xsl:apply-templates select="node()"/>
</a>
<xsl:if test="following::xhtml:h2[1][preceding::xhtml:h1[1]]">
<xsl:value-of select="$newline"/>
<ol>
<xsl:apply-templates select="following::xhtml:h2[preceding::xhtml:h1[1][generate-id() = $myId]]" mode="ToC"/>
<xsl:value-of select="$newline"/>
</ol>
<xsl:value-of select="$newline"/>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="xhtml:h2" mode="ToC">
<xsl:value-of select="$newline"/>
<xsl:variable name="link">
<xsl:value-of select="@id"/>
</xsl:variable>
<li>
<a id="ToC-{$link}" href="#{$link}">
<xsl:apply-templates select="node()"/>
</a>
</li>
</xsl:template>
<!-- h1 and h2's should point back to the ToC for easy navigation -->
<xsl:template match="xhtml:h1">
<xsl:variable name="link">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<a href="#ToC-{$link}"> ↩</a>
</xsl:copy>
</xsl:template>
<xsl:template match="xhtml:h2">
<xsl:variable name="link">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<a href="#ToC-{$link}"> ↩</a>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>