This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
forked from speced/respec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.js
162 lines (152 loc) · 4.73 KB
/
structure.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Module core/structure
// Handles producing the ToC and numbering sections across the document.
// LIMITATION:
// At this point we don't support having more than 26 appendices.
// CONFIGURATION:
// - noTOC: if set to true, no TOC is generated and sections are not numbered
// - tocIntroductory: if set to true, the introductory material is listed in the TOC
// - lang: can change the generated text (supported: en, fr)
// - maxTocLevel: only generate a TOC so many levels deep
var secMap = {};
var appendixMode = false;
var lastNonAppendix = 0;
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function makeTOCAtLevel($parent, doc, current, level, conf) {
var $secs = $parent.children(
conf.tocIntroductory ? "section" : "section:not(.introductory)"
);
if ($secs.length === 0) {
return null;
}
var $ol = $("<ol class='toc'></ol>");
for (var i = 0; i < $secs.length; i++) {
var $sec = $($secs[i], doc);
var isIntro = $sec.hasClass("introductory");
var noToc = $sec.hasClass("notoc");
if (!$sec.children().length || noToc) {
continue;
}
var h = $sec.children()[0], ln = h.localName.toLowerCase();
if (
ln !== "h2" &&
ln !== "h3" &&
ln !== "h4" &&
ln !== "h5" &&
ln !== "h6"
) {
continue;
}
var title = h.textContent,
$kidsHolder = $("<div></div>").append($(h).contents().clone());
$kidsHolder
.find("a")
.renameElement("span")
.attr("class", "formerLink")
.removeAttr("href");
$kidsHolder.find("dfn").renameElement("span").removeAttr("id");
var id = h.id ? h.id : $sec.makeID(null, title);
if (!isIntro) {
current[current.length - 1]++;
}
var secnos = current.slice();
if ($sec.hasClass("appendix") && current.length === 1 && !appendixMode) {
lastNonAppendix = current[0];
appendixMode = true;
}
if (appendixMode) {
secnos[0] = alphabet.charAt(current[0] - lastNonAppendix);
}
var secno = secnos.join("."), isTopLevel = secnos.length == 1;
if (isTopLevel) {
secno = secno + ".";
// if this is a top level item, insert
// an OddPage comment so html2ps will correctly
// paginate the output
$(h).before(document.createComment("OddPage"));
}
var $span = $("<span class='secno'></span>").text(secno + " ");
if (!isIntro) {
$(h).prepend($span);
}
secMap[id] =
(isIntro ? "" : "<span class='secno'>" + secno + "</span> ") +
"<span class='sec-title'>" +
title +
"</span>";
var $a = $("<a/>")
.attr({ href: "#" + id, class: "tocxref" })
.append(isIntro ? "" : $span.clone())
.append($kidsHolder.contents());
var $item = $("<li class='tocline'/>").append($a);
if (conf.maxTocLevel === 0 || level <= conf.maxTocLevel) $ol.append($item);
current.push(0);
var $sub = makeTOCAtLevel($sec, doc, current, level + 1, conf);
if ($sub) {
$item.append($sub);
}
current.pop();
}
return $ol;
}
export function run(conf, doc, cb) {
if ("tocIntroductory" in conf === false) {
conf.tocIntroductory = false;
}
if ("maxTocLevel" in conf === false) {
conf.maxTocLevel = 0;
}
var $secs = $("section:not(.introductory)", doc)
.find("h1:first, h2:first, h3:first, h4:first, h5:first, h6:first")
.toArray()
.filter(elem => elem.closest("section.introductory") === null);
$secs = $($secs);
if (!$secs.length) {
return cb();
}
$secs.each(function() {
var depth = $(this).parents("section").length + 1;
if (depth > 6) depth = 6;
var h = "h" + depth;
if (this.localName.toLowerCase() != h) $(this).renameElement(h);
});
// makeTOC
if (!conf.noTOC) {
var $ol = makeTOCAtLevel($("body", doc), doc, [0], 1, conf);
if (!$ol) return;
var nav = doc.createElement("nav");
nav.id = "toc";
nav.innerHTML = "<h2 class='introductory'>" + conf.l10n.toc + "</h2>";
nav.appendChild($ol[0]);
var $ref = $("#toc", doc);
var replace = false;
if ($ref.length) {
replace = true;
}
if (!$ref.length) {
$ref = $("#sotd", doc);
}
if (!$ref.length) {
$ref = $("#abstract", doc);
}
if (replace) {
$ref.replaceWith(nav);
} else {
$ref.after(nav);
}
var $link = $(
"<p role='navigation' id='back-to-top'><a href='#toc'><abbr title='Back to Top'>↑</abbr></a></p>"
);
$("body").append($link);
}
// Update all anchors with empty content that reference a section ID
$("a[href^='#']:not(.tocxref)", doc).each(function() {
var $a = $(this);
if ($a.html() !== "") return;
var id = $a.attr("href").slice(1);
if (secMap[id]) {
$a.addClass("sec-ref");
$a.html(($a.hasClass("sectionRef") ? "section " : "") + secMap[id]);
}
});
cb();
}