1 var asciidoc = { // Namespace.
3 /////////////////////////////////////////////////////////////////////
4 // Table Of Contents generator
5 /////////////////////////////////////////////////////////////////////
7 /* Author: Mihai Bazon, September 2002
8 * http://students.infoiasi.ro/~mishoo
10 * Table Of Content generator
13 * Feel free to use this script under the terms of the GNU General Public
14 * License, as long as you do not remove or alter this notice.
17 /* modified by Troy D. Hanson, September 2006. License: GPL */
18 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
21 toc: function (toclevels) {
23 function getText(el) {
25 for (var i = el.firstChild; i != null; i = i.nextSibling) {
26 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
28 else if (i.firstChild != null)
34 function TocEntry(el, text, toclevel) {
37 this.toclevel = toclevel;
40 function tocEntries(el, toclevels) {
41 var result = new Array;
42 var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');
43 // Function that scans the DOM tree for header elements (the DOM2
44 // nodeIterator API would be a better technique but not supported by all
46 var iterate = function (el) {
47 for (var i = el.firstChild; i != null; i = i.nextSibling) {
48 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
49 var mo = re.exec(i.tagName);
50 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
51 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
61 var toc = document.getElementById("toc");
62 var entries = tocEntries(document.getElementById("content"), toclevels);
63 for (var i = 0; i < entries.length; ++i) {
64 var entry = entries[i];
65 if (entry.element.id == "")
66 entry.element.id = "_toc_" + i;
67 var a = document.createElement("a");
68 a.href = "#" + entry.element.id;
69 a.appendChild(document.createTextNode(entry.text));
70 var div = document.createElement("div");
72 div.className = "toclevel" + entry.toclevel;
75 if (entries.length == 0)
76 toc.parentNode.removeChild(toc);
80 /////////////////////////////////////////////////////////////////////
81 // Footnotes generator
82 /////////////////////////////////////////////////////////////////////
84 /* Based on footnote generation code from:
85 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
88 footnotes: function () {
89 var cont = document.getElementById("content");
90 var noteholder = document.getElementById("footnotes");
91 var spans = cont.getElementsByTagName("span");
94 for (i=0; i<spans.length; i++) {
95 if (spans[i].className == "footnote") {
97 // Use [\s\S] in place of . so multi-line matches work.
98 // Because JavaScript has no s (dotall) regex flag.
99 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
100 noteholder.innerHTML +=
101 "<div class='footnote' id='_footnote_" + n + "'>" +
102 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
103 n + "</a>. " + note + "</div>";
105 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
106 "' title='View footnote' class='footnote'>" + n + "</a>]";
107 var id =spans[i].getAttribute("id");
108 if (id != null) refs["#"+id] = n;
112 noteholder.parentNode.removeChild(noteholder);
114 // Process footnoterefs.
115 for (i=0; i<spans.length; i++) {
116 if (spans[i].className == "footnoteref") {
117 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
118 href = href.match(/#.*/)[0]; // Because IE return full URL.
121 "[<a href='#_footnote_" + n +
122 "' title='View footnote' class='footnote'>" + n + "</a>]";