/* Variable depth automatic section numbering implemented by automatic * document scanning, looking for headers. * * This code is derived from an example in O'Reilly, "JavaScript: The * Definitive Guide" by David Flanagan, ISBN-10: 0-596-10199-6, ISBN-13: * 978-0-596-10199-2, 5th edition, section 15.9, p336. * * History * ======= * * 13-Jun-2007 (ADH): Imported into Wiki structure. * 21-Mar-2011 (ADH): Imported into Instiki. */ /* This module defines a single maketoc() function and registers an onload * event handler so the function is automatically run when the document * finishes loading. When it runs, maketoc() first looks for document * elements with IDs of "toc" and "under_toc". If there are no such elements * maketoc() does nothing. Otherwise, maketoc() traverses all child nodes * of "under_toc" to find all
tags since headings are not // supposed to appear inside paragraphs. (We could also skip // lists,
tags, etc., butis the most common one.) // XHTML documents tend to return lower case elements, HTML // documents tend to return upper case elements. if (m.tagName == "p" || m.tagName == "P") continue; // optimization // If we didn't skip the child node, check whether it is a heading. // If so, add it to the array. Otherwise, recurse on it. // Note that the DOM is interface-based not class-based so we // cannot simply test whether (m instanceof HTMLHeadingElement). if (m.tagName.length==2 && (m.tagName.charAt(0)=="h" || m.tagName.charAt(0)=="H")) sects.push(m); else findSections(m, sects); } } } // Default text for the Contents section heading maketoc.headingText = "Contents"; // This is the default text of links back to the TOC maketoc.backlinkText = "Contents"; // Register maketoc() to run automatically when the document finishes loading if (window.addEventListener) window.addEventListener("load", maketoc, false); else if (window.attachEvent) window.attachEvent("onload", maketoc);