MediaWiki:Gadget-Encyclo.js

La bibliothèque libre.

Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
/**
 * Setup the Encyclo object:
 */
if ( window.Encyclo === undefined ) {
	window.Encyclo = {};
}

/**
 * The global Encyclo object
 */
( function ( mw, $, enc ) {
'use strict';

enc.do_rozier_link = function (currentFocused, val) {

        var has_sc = false;
        if (/^{{sc\|.*}}$/.test(val)) {
            val = val.replace(/{{sc\|(.*)}}/, "$1");
            has_sc = true;
        }

        var tpl_call = val.replace(/''(.*?)''/g, "$1").toUpperCase();
        if (has_sc === false || tpl_call != val.toUpperCase()) {
            if (has_sc)
               tpl_call += '|{{sc|' + val + '}}';
            else
               tpl_call += '|' + val;
        } 

        currentFocused.textSelection(
	    "encapsulateSelection", {
	        "pre"  : "{{" + 'RozierL' + "|",
                "peri" : tpl_call,
                "post" : "}}",
                "replace" : true
            }
        );
};

enc.supported_link = [
	[ 'Rozier - Cours d’agriculture,', enc.do_rozier_link ]
];

enc.strip_vedette = function (val) {
        val = val.replace(/'''/g, "");
        val = val.replace(/''/g, "");
        val = val.replace(/^[\n ]*(.*)[\n ]*$/, "$1");
        val = val.replace(/[ ]+/g, " ");
        if (/^{{sc\|.*}}$/.test(val)) {
            val = val.replace(/{{sc\|(.*)}}/, "$1");
        }
        return val;
};

enc.do_vedette = function (currentFocused, val) {
        currentFocused.textSelection(
	    "encapsulateSelection", {
	        "pre"  : "## ",
                "peri" : enc.strip_vedette(val),
                "post" : ' ##\n<nowiki/>\n\n\n\n' + val.replace(/^[\n ]*(.*)[\n ]*$/, "$1"),
                "replace" : true
            }
        );
};

enc.supported_vedette = [
	[ 'Rozier - Cours d’agriculture,', enc.do_vedette ]
];

/**** MATH ****/
enc.do_simplify = function (text) {
    var new_text = '';
    var last_match = 0;
    var splitter = /\^{(.)}/g;
    while ((result = splitter.exec(text)) !== null) {
	new_text += text.slice(last_match, splitter.lastIndex - result[0].length);
	new_text += "^" + result[1];
	last_match = splitter.lastIndex;
    }

    new_text = new_text + text.slice(last_match);

    new_text = new_text.replace(/''/g, "");

    return new_text;
};

enc.do_frac = function (text) {
    var new_text = '';
    var last_match = 0;
    var splitter = new RegExp("([^ =]+)/([^ =]+)",  "g");
    while ((result = splitter.exec(text)) !== null) {
	new_text += text.slice(last_match, splitter.lastIndex - result[0].length);
	new_text += "\\frac {" + result[1] + "}{" + result[2] + "}";
	last_match = splitter.lastIndex;
    }

    return new_text + text.slice(last_match);
};

enc.do_exp = function (text) {
    var new_text = '';
    var last_match = 0;
    var splitter = new RegExp("{{e\\|([^}]+)}}|<sup>(.+)</sup>", "g");
    while ((result = splitter.exec(text)) !== null) {
	new_text += text.slice(last_match, splitter.lastIndex - result[0].length);
        if (result[2])
            new_text += "^{" + result[2] + "}";
        else
            new_text += "^{" + result[1] + "}";
	last_match = splitter.lastIndex;
    }

    return new_text + text.slice(last_match);
};

enc.do_post_parse = function (val) {
    var new_text = val.replace(/(=o|= o)$/g, "= 0");
    new_text = new_text.replace(/(::)/g, "\\text { :: }");
    new_text = new_text.replace(/∷/g, "\\text { :: }");

    return new_text;
};

enc.do_math = function (currentFocused, val) {

        val = enc.do_frac(val);
        val = enc.do_exp(val);
        val = enc.do_simplify(val);
        val = enc.do_post_parse(val);

        currentFocused.textSelection(
	    "encapsulateSelection", {
        	"pre"  : "<math>\\scriptstyle ",
                "peri" : val,
                "post" : "</math>",
                "replace" : true
            }
        );
};
/***** END MATH ********/

// Wrapper to call fct() with the selected text
enc.call_fun = function (fct) {
    var currentFocused =  $("#wpTextbox1");
    if (currentFocused && currentFocused.length) {
        var val = currentFocused.textSelection( "getSelection", {} );
        if (!val.length)
            return;

    fct(currentFocused, val);
    }
};

// js scope is a complete mess.
enc.create_func = function (fct) {
       return function() { enc.call_fun(fct); };
};

enc.editForm = function () {
	window.insert_button.editForm( {
		'img_classic' : '//upload.wikimedia.org/wikisource/fr/c/cf/LaTeX.png',
		'img_advanced' : '//upload.wikimedia.org/wikisource/fr/c/cf/LaTeX.png',
		'tooltip' : 'Transforme autant que possible une expression en LaTeX',
		'img_id' : 'wpLaTeX',
		'on_click' : enc.create_func(enc.do_math)
	} );

	for (var i = 0; i < enc.supported_link.length; ++i) {
		if (mw.config.get("wgTitle").indexOf(enc.supported_link[i][0]) === 0) {
			window.insert_button.editForm( {
				'img_classic' : '//upload.wikimedia.org/wikisource/fr/0/06/Encl.png',
				'img_advanced' : '//upload.wikimedia.org/wikisource/fr/0/06/Encl.png',
				'tooltip' : 'Lien encyclo',
				'img_id' : 'wpEncycloLink',
				'on_click' : enc.create_func(enc.supported_link[i][1])
			} );
		 }
	}
	for (var i = 0; i < enc.supported_vedette.length; ++i) {
		if (mw.config.get("wgTitle").indexOf(enc.supported_vedette[i][0]) === 0) {
			window.insert_button.editForm( {
				'img_classic' : '//upload.wikimedia.org/wikipedia/commons/6/66/Ancient_Greek_symbol_icon.png',
				'img_advanced' : '//upload.wikimedia.org/wikipedia/commons/6/66/Ancient_Greek_symbol_icon.png',
				'tooltip' : 'Mise en place vedette',
				'img_id' : 'wpEncycloVedette',
				'on_click' : enc.create_func(enc.supported_vedette[i][1])
			} );
		 }
	}
};

if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 && mw.config.get('wgCanonicalNamespace') === 'Page') {
	$.when(
		mw.loader.using( [ 'ext.gadget.InsertButton' ] )
	).then(
		enc.editForm
	);
}
 
}( mediaWiki, jQuery, window.Encyclo ) );