wiki.pl: Port some fixes from upstream
[Orgmuse.git] / plinks.js
blob150821b39b5e912fa64157496790fa95baae660c
1 /* Copyright 2005  Alex Schroeder <alex@emacswiki.org>
2    based on http://simon.incutio.com/archive/2004/05/30/plinks#p-13
3    Copyright 2004  Simon Willison
4 */
6 function plinkHighlight() {
7     if (/#[0-9]+$/.test(document.location)) {
8         // The user arrived via a plink
9         var plink_id = document.location.href.split('#')[1];
10         var para = document.getElementById("p" + plink_id);
11         para.className = 'plink';
12     }
15 function addpLinks() {
16     /* Only show plinks on ordinary pages: They either use path_info
17      * or keywords in the URL, not parameters. */
18     if (/=/.test(document.location.href)) {
19         return;
20     }
21     // find the content div: why is there no xpath?
22     var elem = document.getElementsByTagName('div');
23     var content;
24     div:
25     for (var i = 0; i < elem.length; i++) {
26         var classes = elem[i].getAttribute('class').split(" ");
27         for (var j = 0; j < classes.length; j++) {
28             if (classes[j] == 'content') {
29                 content = elem[i];
30                 break div;
31             }
32         }
33     }
34     // identify all p and li items
35     var items = new Array;
36     elem = content.getElementsByTagName('p');
37     for (var i = 0; i < elem.length; i++) {
38         items.push(elem[i]);
39     }
40     elem = content.getElementsByTagName('li');
41     for (var i = 0; i < elem.length; i++) {
42         items.push(elem[i]);
43     }
44     // add named anchors at the beginning and a link to that anchor at
45     // the end of all items
46     for (var i = 0; i < items.length; i++) {
47         var current = items[i];
48         current.setAttribute("id", "p" + i);
49         var anchor = document.createElement('a');
50         anchor.name = i;
51         current.insertBefore(anchor, current.firstChild);
52         var plink = document.createElement('a');
53         plink.href = document.location.href.split('#')[0] + '#' + i;
54         plink.className = 'plink';
55         plink.appendChild(document.createTextNode(' #'));
56         current.appendChild(plink);
57     }
60 function addLoadEvent(func) {
61     var oldonload = window.onload;
62     if (typeof window.onload != 'function') {
63         window.onload = func;
64     } else {
65         window.onload = function() {
66             oldonload();
67             func();
68         }
69     }
72 addLoadEvent(addpLinks);
73 addLoadEvent(plinkHighlight);