MediaWiki:Gadget-Easy LST.js

/** * Easy Labelled Section Transclusion Syntax * * Configurations: *    enabled:    Set to false to disable this gadget on the current page * * Configure it like this, from your main JS: * * { *  var easySectSyntaxConfig = { *   enabled: true, *  }; * *   mw.hook("easy_section_syntax.config").fire(easySectSyntaxConfig); * } */

"use strict";

// IIFE used when including as a user script (to allow debug or config) // Default gadget use will get an IIFE wrapper as well (function($, mw) {

var gadget_name = "easy_section_syntax";

var EasySS = { enabled: true, debug: true, configured: false, };

function log(msg) { if (EasySS.debug) { console.log(gadget_name + ": ", msg); } }

function error(msg) { console.error(gadget_name + ": ", msg); }

/*  * Read config from user. This function fired by hook */ function apply_config(cfg) { console.log("Configuring " + gadget_name);

// Bail if the config looks like junk if (!cfg || typeof cfg !== "object") { console.error("Invalid " + gadget_name + " config", cfg); return; }

if (typeof cfg.enabled === "boolean") { EasySS.enabled = cfg.enabled; }

if (typeof cfg.debug === "boolean") { EasySS.debug = cfg.debug; }

EasySS.configured = true; }

/*  * Convert ## syntax to syntax */ function restore_lst {

log("Restoring section markers from ##-syntax");

var editbox = $("#wpTextbox1");

if (!editbox) { error("Couldn't find editbox"); return; }

var search = /##[\s]*(.*?)[\s]*##[\s]*\n/; var a = editbox.val.split(search); var s = a[0]; var m = parseInt(a.length / 2); for (var i = 0; i < m; i++) { var title = a[i * 2 + 1]; // Ensure we'll don't get twice quote. title = title.replace(/^"(.*)"$/, "$1"); title = title.replace(/^'(.*)'$/, "$1"); var content = a[i * 2 + 2]; if (title && content.substring(0, 2) == "{|") { content = "\n" + content; }     if (title) { s = s + "" + content + "\n"; } else { s = s + content; }     /* if( i < m-1 ) s = s + "\n"; */ }   editbox.val(s); }

/**  * easy lst: hide section markers */ function easy_section_syntax {

log("Converting section markers to ##-syntax");

var editbox = $("#wpTextbox1");

if (!editbox) { error("Couldn't find editbox"); return; }

var search = //; var a = editbox.val.split(search); var s = a[0]; var ok = true; for (var i = 0; i < parseInt(a.length / 2); i++) { var title = a[i * 2 + 1]; var content = a[i * 2 + 2]; var r2 = /^([\s\S]*?)(\n|)[\s]*([\s\S]*?)$/; var m2 = content.match(r2); if (m2) { title = title.replace(/^"(.*)"$/, "$1"); title = title.replace(/^'(.*)'$/, "$1"); if (s && s.charAt(s.length - 1) != "\n" &&         s.charAt(s.length - 1) != "|") { s = s + "\n"; }       s = s + "## " + title + " ##\n" + m2[1]; if (m2[4]) { if (m2[4] != "\n") { if (s && s.charAt(s.length - 1) != "\n") { s = s + "\n"; }           s = s + "####\n" + m2[4]; }       }      } else { ok = false; error("Error" + title); }   }    if (ok) { // val function is important, as just setting .value on a raw // element doesn't work in CodeMirror editbox.val(s); } }

/*  * Run setup, and convert to ## syntax for LST */ function on_load { easy_section_syntax;

// Install the on-save hook on all edit buttons $('.editButtons').click(restore_lst); }

/**  * Install a load hook in the editor * @param {Function} callback: function to call when editor text is loaded */ function install_load_hook(callback) { if ($.inArray(mw.config.get("wgAction"), ["edit", "submit"]) !== -1) { mw.loader.using("ext.proofreadpage.page", function {       // mimic code in the extension, there is a conditionnal deps on ext.wikiEditor.        if (mw.user.options.get("usebetatoolbar") && $.inArray("ext.wikiEditor", mw.loader.getModuleNames) > -1) {         var load_deps = ["ext.wikiEditor"];          if (mw.user.options.get("codemirror-syntax-highlight") == 1) {            load_deps.push("ext.CodeMirror.lib");          }          mw.loader.using(load_deps, function { callback; });       } else {          callback;        }      }); } }

function easy_sect_syn_setup {

// Get user config, if any mw.hook(gadget_name + ".config").add(apply_config);

// LST only in Page NS and if user has it enabled if (!EasySS.enabled || mw.config.get("wgCanonicalNamespace") !== "Page") { return; }

install_load_hook(on_load); } $(easy_sect_syn_setup);

}(jQuery, mediaWiki));