]> git.proxmox.com Git - pve-docs.git/blobdiff - asciidoc/asciidoc.js
asciidoc.js: use mediawiki mw.hook
[pve-docs.git] / asciidoc / asciidoc.js
index 5882248492cc64fc781c106dcd784da1bee14662..93c6ee245d62fedd49c1478124404f4fa3605c0d 100644 (file)
@@ -7,9 +7,43 @@
 
 var asciidoc = {
 
+    // toc generator
+    toc: function ($content) {
+       var tocholder = $content.find('#toc');
+
+       if (!tocholder) {
+           return;
+       }
+
+       tocholder.html('');
+       tocholder.hide();
+
+       var html = "<div id=\"toctitle\"><h2>Contents</h2></div><ul>";
+
+       var n = 0;
+       $content.find("div.sect1").each(function(){
+           var h = jQuery(this).find("h2").first();
+           var id = h.attr("id");
+           if (id != null) {
+               n++;
+               html += "<li class=\"toclevel-1\">" +
+                   "<a href=\"#" + id + "\">" +
+                   "<span class=\"toctext\">" + h.html() +
+                   "</span></a></li>";
+           }
+       });
+
+       html += "</ul>";
+
+       if (n > 3) {
+           tocholder.html(html);
+           tocholder.show();
+       }
+    },
+
     // footnote generator
-    footnotes: function () {
-       var noteholder = $("#footnotes");
+    footnotes: function ($content) {
+       var noteholder = $content.find('#footnotes');
        if (!noteholder) {
            return;
        }
@@ -21,9 +55,9 @@ var asciidoc = {
        var n = 0;
        var inner_html = '';
 
-       $("#asciidoccontent span.footnote").each(function(){
+       $content.find("span.footnote").each(function(){
            n++;
-           var span = $(this);
+           var span = jQuery(this);
            var note = span.attr("data-note");
            var id = span.attr("id");
            if (!note) {
@@ -39,16 +73,16 @@ var asciidoc = {
             "<div class='footnote' id='_footnote_" + n + "'>" +
                "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
                n + "</a>. " + note + "</div>";
-           
+
            if (id != null) { refs["#"+id] = n; }
        });
 
        if (inner_html) { noteholder.html("<hr>" + inner_html); }
-    
+
        if (n != 0) {
            // process footnoterefs.
-           $("#asciidoccontent span.footnoteref").each(function(){
-               var span = $(this);
+           $content.find("span.footnoteref").each(function(){
+               var span = jQuery(this);
                var href = span.find("a").first().attr("href");
                href = href.match(/#.*/)[0];  // in case it return full URL.
                n = refs[href];
@@ -59,7 +93,10 @@ var asciidoc = {
     }
 };
 
-$(document).ready(function(){
-    asciidoc.footnotes();
+// add init to mediawiki resource loader queue
+(window.RLQ=window.RLQ||[]).push(function(){
+    mw.hook('wikipage.content').add(function($content) {
+       asciidoc.toc($content);
+       asciidoc.footnotes($content);
+    });
 });
-