]> git.proxmox.com Git - pve-docs.git/blob - asciidoc/mediawiki-asciidoc.js
fixup: s/devies/devices/
[pve-docs.git] / asciidoc / mediawiki-asciidoc.js
1 //////////////////////////////////////////////////////////////////////////
2 // asciidoc JS helper for Proxmox VE mediawiki pages
3 //
4 // code based on original asciidoc.js, but re-written using jQuery
5 //
6 //////////////////////////////////////////////////////////////////////////
7
8 var asciidoc = {
9
10 // toc generator
11 toc: function ($content) {
12 var tocholder = $content.find('#toc');
13
14 if (!tocholder) {
15 return;
16 }
17
18 tocholder.html('');
19 tocholder.hide();
20
21 var html = "<div id=\"toctitle\"><h2>Contents</h2></div><ul>";
22
23 var n = 0;
24 $content.find("div.sect1").each(function(){
25 var h = jQuery(this).find("h2").first();
26 var id = h.attr("id");
27 if (id != null) {
28 n++;
29 html += "<li class=\"toclevel-1\">" +
30 "<a href=\"#" + id + "\">" +
31 "<span class=\"toctext\">" + h.html() +
32 "</span></a></li>";
33 }
34 });
35
36 html += "</ul>";
37
38 if (n > 3) {
39 tocholder.html(html);
40 tocholder.show();
41 }
42 },
43
44 // footnote generator
45 footnotes: function ($content) {
46 var noteholder = $content.find('#footnotes');
47 if (!noteholder) {
48 return;
49 }
50
51 noteholder.html('');
52
53 // Rebuild footnote entries.
54 var refs = {};
55 var n = 0;
56 var inner_html = '';
57
58 $content.find("span.footnote").each(function(){
59 n++;
60 var span = jQuery(this);
61 var note = span.attr("data-note");
62 var id = span.attr("id");
63 if (!note) {
64 // Use [\s\S] in place of . so multi-line matches work.
65 // Because JavaScript has no s (dotall) regex flag.
66 note = span.html().match(/\s*\[([\s\S]*)]\s*/)[1];
67 span.html("[<a id='_footnoteref_" + n + "' href='#_footnote_" +
68 n + "' title='View footnote' class='footnote'>" + n +
69 "</a>]");
70 span.attr("data-note", note);
71 }
72 inner_html +=
73 "<div class='footnote' id='_footnote_" + n + "'>" +
74 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
75 n + "</a>. " + note + "</div>";
76
77 if (id != null) { refs["#"+id] = n; }
78 });
79
80 if (inner_html) { noteholder.html("<hr>" + inner_html); }
81
82 if (n != 0) {
83 // process footnoterefs.
84 $content.find("span.footnoteref").each(function(){
85 var span = jQuery(this);
86 var href = span.find("a").first().attr("href");
87 href = href.match(/#.*/)[0]; // in case it return full URL.
88 n = refs[href];
89 span.html("[<a href='#_footnote_" + n +
90 "' title='View footnote' class='footnote'>" + n + "</a>]");
91 });
92 }
93 }
94 };
95
96 // add init to mediawiki resource loader queue
97 (window.RLQ=window.RLQ||[]).push(function(){
98 mw.hook('wikipage.content').add(function($content) {
99 asciidoc.toc($content);
100 asciidoc.footnotes($content);
101 });
102 });