OLD | NEW |
| (Empty) |
1 function nextLevel(nodeList, startIndex, hlevel, prefix, tocString) | |
2 { | |
3 var hIndex = 1; | |
4 var i = startIndex; | |
5 | |
6 while (i < nodeList.length) { | |
7 var currentNode = nodeList[i]; | |
8 | |
9 if (currentNode.tagName != "H"+hlevel) | |
10 break; | |
11 | |
12 if (currentNode.className == "no-toc") { | |
13 ++i; | |
14 continue; | |
15 } | |
16 | |
17 var sectionString = prefix+hIndex; | |
18 | |
19 // Update the TOC | |
20 var text = currentNode.innerHTML; | |
21 // Strip off names specified via <a name="..."></a> | |
22 var tocText = text.replace(/<a name=[\'\"][^\'\"]*[\'\"]>([^<]*)<\/a>/g,
"$1"); | |
23 tocString.s += "<li class='toc-h"+hlevel+"'><a href='#"+sectionString+"'
><span class='secno'>"+sectionString+"</span>"+tocText+"</a></li>\n"; | |
24 | |
25 // Modify the header | |
26 currentNode.innerHTML = "<span class=secno>"+sectionString+"</span> "+te
xt; | |
27 currentNode.id = sectionString; | |
28 | |
29 // traverse children | |
30 i = nextLevel(nodeList, i+1, hlevel+1, sectionString+".", tocString); | |
31 hIndex++; | |
32 } | |
33 | |
34 return i; | |
35 } | |
36 | |
37 function generateTOC(toc) | |
38 { | |
39 var nodeList = $("h2,h3,h4,h5,h6"); | |
40 var tocString = { s:"<ul class='toc'>\n" }; | |
41 nextLevel(nodeList, 0, 2, "", tocString); | |
42 toc.innerHTML = tocString.s; | |
43 | |
44 // Now position the document, in case a #xxx directive was given | |
45 var id = window.location.hash.substring(1); | |
46 if (id.length > 0) { | |
47 var target = document.getElementById(id); | |
48 if (target) { | |
49 var rect = target.getBoundingClientRect(); | |
50 setTimeout(function() { window.scrollTo(0, rect.top) }, 0); | |
51 } | |
52 } | |
53 } | |
OLD | NEW |