-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipython_toc.js
69 lines (51 loc) · 1.54 KB
/
ipython_toc.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
// Modified by: Andrew Wong
// Original author/source: kmahelona.github.io
// Converts integer to roman numeral
function romanize(num) {
var lookup = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
roman = '',
i;
for ( i in lookup ) {
while ( num >= lookup[i] ) {
roman += i;
num -= lookup[i];
}
}
return roman;
}
// Builds a <ul> Table of Contents from all <headers> in DOM
function createTOC(){
var toc = "";
var level = 0;
var levels = {}
$('#toc').html('');
$(":header").each(function(i){
if (this.id=='tocheading'){return;}
titleText = this.innerHTML;
openLevel = this.tagName[1];
if (levels[openLevel]){
levels[openLevel] += 1;
} else{
levels[openLevel] = 1;
}
if (openLevel > level) {
toc += (new Array(openLevel - level + 1)).join('<ul class="toc">');
} else if (openLevel < level) {
toc += (new Array(level - openLevel + 1)).join("</ul>");
for (i=level;i>openLevel;i--){levels[i]=0;}
}
level = parseInt(openLevel);
if (this.id==''){this.id = this.innerHTML.replace(/ /g,"-")}
var anchor = this.id;
toc += '<li><a href="#' + anchor + '">' + romanize(levels[openLevel].toString()) + '. ' + titleText
+ '</a></li>';
});
if (level) {
toc += (new Array(level + 1)).join("</ul>");
}
$('#toc').append(toc);
};
// Executes the createToc function
setTimeout(function(){createTOC();},100);
// Rebuild to TOC every minute
setInterval(function(){createTOC();},60000);