Make documentation table of contents anchors human readable

If we have embedded an anchor in our source text that gives a human
readable name to a section header, the generated table of contents
shown at the top of the document should use that anchor rather than
a sequentially generated one.  This makes the anchor more stable
for long term linking, in case sections are added or removed.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2009-08-20 10:43:30 -07:00
parent f28652d97c
commit 8f6cb555ee
2 changed files with 119 additions and 2 deletions

View File

@ -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 $@+ $@

View File

@ -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);
}
}