diff --git a/Documentation/Makefile b/Documentation/Makefile index 89d2e51b53..05da77468b 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -19,6 +19,7 @@ SVN = svn PUB_ROOT = https://gerrit.googlecode.com/svn/documentation LOCAL_ROOT = .published +SCRIPTSDIR := $(shell pwd)/javascript DOC_VERS := $(shell git describe HEAD) DOC_VMM := $(shell sh -c "echo $(DOC_VERS) | perl -pe 's,^v(\d+\.\d+)[\.-].*\$$,\$$1,'") PUB_DIR = $(PUB_ROOT)/$(DOC_VMM) @@ -50,10 +51,11 @@ clean: rm -f *.html rm -rf $(LOCAL_ROOT) -$(DOC_HTML): %.html : %.txt +$(DOC_HTML): %.html : %.txt $(SCRIPTSDIR)/toc.js rm -f $@+ $@ - $(ASCIIDOC) \ + $(ASCIIDOC) --unsafe \ -a toc -a 'revision=$(DOC_VERS)' \ + -a scriptsdir=$(SCRIPTSDIR) \ -b xhtml11 -f asciidoc.conf \ $(ASCIIDOC_EXTRA) -o $@+ $< mv $@+ $@ diff --git a/Documentation/javascript/toc.js b/Documentation/javascript/toc.js new file mode 100644 index 0000000000..36023f83ab --- /dev/null +++ b/Documentation/javascript/toc.js @@ -0,0 +1,115 @@ +/* Author: Mihai Bazon, September 2002 + * http://students.infoiasi.ro/~mishoo + * + * Table Of Content generator + * Version: 0.4.sp + * + * Feel free to use this script under the terms of the GNU General Public + * License, as long as you do not remove or alter this notice. + */ + + /* modified by Troy D. Hanson, September 2006. License: GPL */ + /* modified by Stuart Rackham, October 2006. License: GPL */ + /* modified by Shawn Pearce, August 2009. License: GPL */ + +function getText(el) { + var text = ""; + for (var i = el.firstChild; i != null; i = i.nextSibling) { + if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants. + text += i.data; + else if (i.firstChild != null) + text += getText(i); + } + return text; +} + +function TocEntry(el, text, toclevel) { + this.element = el; + this.text = text; + this.toclevel = toclevel; + this.assigned = false; + + if (el.id != '') { + this.id = el.id; + + } else { + var a = el.firstChild; + if ((a.tagName == "a" || a.tagName == "A") && a.id != "") { + this.id = a.id; + } else { + this.id = ''; + } + } +} + +function tocEntries(el, toclevels) { + var result = new Array; + var re = new RegExp('[hH]([2-'+(toclevels+1)+'])'); + // Function that scans the DOM tree for header elements (the DOM2 + // nodeIterator API would be a better technique but not supported by all + // browsers). + var iterate = function (el) { + for (var i = el.firstChild; i != null; i = i.nextSibling) { + if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { + var mo = re.exec(i.tagName) + if (mo) + result[result.length] = new TocEntry(i, getText(i), mo[1]-1); + iterate(i); + } + } + } + iterate(el); + return result; +} + +// This function does the work. toclevels = 1..4. +function generateToc(toclevels) { + var simple_re = new RegExp('^[a-zA-Z._ -]{1,}$'); + var entries = tocEntries(document.getElementsByTagName("body")[0], toclevels); + var usedIds = new Array(); + + for (var i = 0; i < entries.length; ++i) { + var entry = entries[i]; + if (entry.id != "") + usedIds[entry.id] = entry; + } + + for (var i = 0; i < entries.length; ++i) { + var entry = entries[i]; + if (entry.id != "" || !simple_re.exec(entry.text)) + continue; + + var n = entry.text.replace(/ /g, '_').toLowerCase(); + var e = usedIds[n]; + if (e) { + if (e.assigned) + e.id = ''; + continue; + } + + entry.assigned = true; + entry.id = n; + entry.element.id = entry.id; + usedIds[n] = entry; + } + + for (var i = 0; i < entries.length; ++i) { + var entry = entries[i]; + if (entry.id == '') { + entry.id = "toc" + i; + entry.element.id = entry.id; + } + } + + var toc = document.getElementById("toc"); + for (var i = 0; i < entries.length; ++i) { + var entry = entries[i]; + var a = document.createElement("a"); + a.href = "#" + entry.id; + a.appendChild(document.createTextNode(entry.text)); + var div = document.createElement("div"); + div.appendChild(a); + div.className = "toclevel" + entry.toclevel; + toc.appendChild(div); + } +}