/** Generated TOC
Stuart Langridge, July 2007
Generate a table of contents, based on headings in the page.
To place the TOC on the page, add
to the page where you want the TOC to appear. If this element
is not present, the TOC will not appear.
The TOC defaults to displaying all headings that are contained within
the same element as it itself is contained in (or all headings on the
page if you did not provide a generated-toc container). To override this,
provide a "highest heading" value by adding class="generate_from_h3"
(or h2, h4, etc) to the container. (If unspecified, this will display all
headings, as if class="generate_from_h1" was specified.)
The TOC defaults to operating only on headings contained within the same
element as it itself, i.e., in a page like this:
The "quux" heading will not appear in the TOC. To override this,
add class="generate_for_page" to the container, which will process
all headings on the page wheresoever they may be.
*/
generated_toc = {
generate: function() {
// Identify our TOC element, and what it applies to
generate_from = '0';
generate_for = 'unset';
tocparent = document.getElementById('generated-toc');
if (tocparent) {
// there is a div class="generated-toc" in the document
// dictating where the TOC should appear
classes = tocparent.className.split(/\s+/);
for (var i=0; i= parseInt(generate_from); i--) {
headings_to_treat["h" + i] = '';
}
// get headings. We can't say
// getElementsByTagName("h1" or "h2" or "h3"), etc, so get all
// elements and filter them ourselves
// need to use .all here because IE doesn't support gEBTN('*')
nodes = top_node.all ? top_node.all : top_node.getElementsByTagName('*');
// put all the headings we care about in headings
headings = [];
for (var i=0; i cur_head_lvl) {
// this heading is at a lower level than the last one;
// create additional nested lists to put it at the right level
// get the *last* LI in the current list, and add our new UL to it
var last_listitem_el = null;
for (var j=0; j]+>/g, '');
},
findFirstHeader: function(node) {
// a recursive function which returns the first header it finds inside
// node, or null if there are no functions inside node.
var nn = node.nodeName.toLowerCase();
if (nn.match(/^h[1-6]$/)) {
// this node is itself a header; return our name
return nn;
} else {
for (var i=0; i