Utilisateur:Flo/outils.js

Un article de Wikipédia, l'encyclopédie libre.

Note : Après avoir publié la page, vous devrez forcer son rechargement pour voir les changements : Mozilla / Konqueror / Firefox : Shift-Ctrl-R, Internet Explorer / Opera : Ctrl-F5, Safari : Cmd-R.

function specialChars() {
	if($("specialcharsets")) {var special = $("specialcharsets")} else {return}
 
	var P = $$("p",special);
	for each(p in P) {
		if(p.title == "Math") var math = p;
		// if(p.title) P[p.title] = p; else continue;
	}
 
	while (math.firstChild) {
		math.removeChild(math.firstChild);
	}
 
	span = nouvelÉlément("span");// Table des caractères Unicode/U2200
	span.appendChild( nouveauTexte("≠ ≤ ≥ < > ≡ ≈ ≅ ∝ \u2212 × ÷ ± ⊥ ⊕ ⊗ ∗ … ½ ⅓ ¼ ¾ ⅟ ¹ ² ³ ° ‰ ℓ ∆ Δ ∂ ∫ ∇ ∑ ∞ ∏ √ ← → ↔ ↦ ⇐ ⇒ ⇔ ⌈ ⌉ ⌊ ⌋ ⋳ ∥ ¬ ∧ ∨ ∃ ∀ ∈ ∉ ∋ ∅ ⊆ ⊇ ⊃ ⊂ ⊄ ∪ ∩ ℵ \u211d \u2102 \u2115 \u2124 \u22C5 \u00B7 \u2022") );
	math.appendChild(span);
	initializeCharSubset(math);
 
	chooseCharSubset(1);
 
	if( $("toolbar") ) w = $("toolbar"); else w = $("mw-editForm");
	if(w) w.parentNode.insertBefore( $("edittoolsinfo").parentNode ,w );
 
	var p = $$("p", $("edittoolsinfo").parentNode )[0];
	if(p) p.appendChild( nouveauLien("javascript:regexpReplace(/\\n/g,'\\n\\t')","\\t") );
 
        var a = document.createElement("a");
        a.href = "javascript:insertTags('<math>','</math>','')";
        a.accessKey = "§";
        a.appendChild( document.createTextNode("<math/>") );
        if(p) p.appendChild( a );
}
 
// Tiré de [[User:Zelda/regexp.js]]
 
// Tableau des boutons
var regexpButtons = [];
 
/**
 * Ajout d'un bouton à la toolbar d'édition
 * inspirée de insertButton()
 * @param title : titre de la fonction
 * @param image : source de l'image à afficher
 * @param code: javascript à exécuter lors du click sur le bouton
 */
function ajouteBouton(image, titre, code, codeAlt, codeMaj) {
	if(!image) image = "http://upload.wikimedia.org/wikipedia/commons/e/ec/Button_base.png";
	regexpButtons[regexpButtons.length] =
		{titre: titre, image: image, code: code, codeMaj: codeMaj, codeAlt: codeAlt};
}
 
/**
 * Ajoute les boutons à la toolbar
 */
function ajouteBoutons() {
	if ( !(toolbar = $("toolbar")) ) return false;
	var div = nouvelÉlément("div");
	div.setAttribute("style","float: right");
 
	for each(var button in regexpButtons) {
		var img = nouvelÉlément("img");
		img.className = "mw-toolbar-editbutton";
		img.width = 23;
		img.height = 22;
		img.src = button.image;
		img.border = 0;
		img.style.cursor = "pointer";
		img.title = img.alt = button.titre;
		img.code = button.code;
		img.codeMaj = button.codeMaj;
		img.codeAlt = button.codeAlt;
		img.onclick = function(e){
				if( e.shiftKey ) eval(this.codeMaj);
			else 	if( e.altKey ) eval(this.codeAlt);
			else	eval(this.code);
			return false;
		} 
 
		div.appendChild(img);
	}
	toolbar.appendChild(div);
}
 
function theSelection(area) {
	if(area.selectionStart === null) return false;
	var startPos = area.selectionStart;
	var endPos = area.selectionEnd;
	if (endPos <= startPos) {
		startPos = 0;
		endPos = area.value.length;
	}
	var theSelection = (area.value).substring(startPos, endPos);
 
	return {start:startPos, end:endPos, value:theSelection};
}
 
function regexpReplace(regexp, replace) {
	if(!regexp) return false;
	var txtarea = $("wpTextbox1");
	var selection = theSelection(txtarea);
	if(!selection) return false;
	var scrollTop = txtarea.scrollTop;
	var replacedSelection = selection.value.replace(regexp, replace);
	var startPos = selection.start;
	var endPos = selection.end;
 
	txtarea.value = txtarea.value.substring(0, startPos) + replacedSelection +
		txtarea.value.substring(endPos, txtarea.value.length);
 
	txtarea.setSelectionRange(startPos, startPos + replacedSelection.length);
	txtarea.scrollTop = scrollTop;
	txtarea.focus();
}
 
window.lastPattern = "";
window.lastFlags = "ig";
window.lastReplace = "";
window.left1 = "\\left"; window.right1 = "\\right"; window.def1 = "";
function expression(pattern,flags) {
	if( pattern != null ) lastPattern = pattern;
	if( flags != null ) lastFlags = flags;
	if(
		(newPattern = prompt( 'Expression rationnelle :', lastPattern )) == null
	||
		(newFlags = prompt( 'Option(s)\nCombinaison de m (multiline), i (case insensitive) et g (global)', lastFlags )) == null
	) {
		return false;
	} else {
		window.lastPattern = newPattern;
		window.lastFlags = newFlags;
		return new RegExp(newPattern, newFlags);
	}
}
 
function remplacer(laSélection) {
	var motif,reg;
	if( laSélection ) { 
		var txtarea = $("wpTextbox1");
		motif = theSelection(txtarea).value;
		txtarea.setSelectionRange(0,txtarea.length);
	} else motif = null;
 
	if(
		(reg = expression(motif)) != false 
	&& 	(lastReplace = prompt('Remplacer par :',lastReplace)) !== null
	) 
		regexpReplace(reg, lastReplace);
	else
		alert("Erreur");
}
 
function nouveauxBoutons() {
	addCustomButton('http://upload.wikimedia.org/wikipedia/commons/3/3e/Sharp_button.png',
		'Code','<code>','</code>','','');
	//addCustomButton('http://upload.wikimedia.org/wikipedia/commons/b/b5/Button_mini_stub.png',
	//	'Bloc emphase','{{bloc emphase|','}}','','');
	addCustomButton('http://upload.wikimedia.org/wikipedia/commons/5/52/Button_grec.png',
		'Math','{{math|§=','}}','','');
 		//http://upload.wikimedia.org/wikipedia/commons/f/f5/Button_l_ln.png
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/5/59/Button_replace.png", 
		"Remplacer","remplacer(false)","remplacer(true)");
 
	ajouteBouton("http://fr.wikipedia.org/skins-1.5/common/images/button_math.png", 
		"Remplacer dans/hors <math/>",
"lastPattern = '(?=(?:[^<]|<(?!math>|\\/math>))*<\\/math>)'; remplacer()",
"lastPattern = '(?!(?:[^<]|<(?!math>|\\/math>))*<\\/math>)'; remplacer()");
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/7/72/Button_span_2.png", 
		"Remplacer une balise", "lastPattern = '<sub>((?:[^<]|<(?!/sub>))*)</sub>'; remplacer();");
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/1/12/Button_find.png",
		"Chercher/compter", "chercher(false)", "chercher(true)");
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/0/03/Button_paragraphe_1.png",
		"Insérer 1",
		"insertTags(left1,right1,def1)",
		"left1 = prompt('Gauche :',left1); right1 = prompt('Droite :',right1); def1 = prompt('Défaut :',def1)");
 
	ajouteBouton('http://upload.wikimedia.org/wikipedia/commons/5/5f/Button_center.png',
		'Centrer/avec saut de ligne',
		"insertTags('<center>','</center>')","insertTags('\\n<center>','</center>\\n')");
 
	ajouteBouton('http://upload.wikimedia.org/wikipedia/commons/2/26/Button_latinas.png',
		'Guillemets/Nettoyer la ponctuation', 'insertTags("« "," »","")','ponctuation()');
		// http://upload.wikimedia.org/wikipedia/commons/4/4d/Button_quotes.png
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/8/8e/Button_shifting.png",
		"Indenter/Désindenter",
		"regexpReplace(/\\n/g,'\\n:');", "regexpReplace(/\\n:/g,'\\n')");
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/6/66/Button_mort.png", 
		"Centrer les équations",
"regexpReplace(/\\n(?::\\s*)?(<math>(?:[^<]|<(?!\\/math>))*<\\/math>[^\\n]*)/g, '\\n<center>$1</center>');");
		// http://upload.wikimedia.org/wikipedia/commons/f/ff/Button_arrow_right.png
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/4/4e/Button_romain.png",
		"Différentielles",
		"insertTags('\x5C\x5Cmathrm','','');",
		"regexpReplace(/([^a-z\\\\])(d)(?=(?:[^<]|<(?!math>|\\/math>))*<\\/math>)/ig, '$1\x5C\x5Cmathrm $2'); regexpReplace(/\\\\mathrm\\s*\\\\mathrm/ig,'\x5C\x5Cmathrm');");
		// http://upload.wikimedia.org/wikipedia/commons/2/2e/Button_broom.png
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/c/c7/Button_polytonique.png",
		"Italique2TeX (un/plusieurs caractères)",
 		"regexpReplace(/([^'])''([^'])''([^'])/ig,'$1<math>$2</math>$3')",
 		"regexpReplace(/''(([^']|[^']'[^'])*)''/ig,'<math>$1</math>')");
 
	ajouteBouton("http://upload.wikimedia.org/wikipedia/commons/6/6a/Button_sup_letter.png",
		"HTML2TeX", "HTML2TeX(false)", "HTML2TeX(true)");
}
 
function mathButton() {
	var math = $("mw-editbutton-math"); if(!math) return;
	var a = nouvelÉlément("a");
	a.accessKey = "§";
	var img = nouvelÉlément("img");
	img.src = math.src;
	a.href = "javascript:insertTags('<math>','</math>','')";
	a.appendChild(img);
	math.style.display = "none";
	math.parentNode.insertBefore(a,math);
}
addOnloadHook(mathButton);
 
function chercher(disp) {
	var area = $('wpTextbox1');
	var sel = theSelection(area); var tab = [];
	if( (tab = sel.value.match(expression())) === null ) {
		alert("Le motif ne concorde pas !");
		return false;
	}
	var index = sel.value.indexOf(tab[0]);
	area.focus();
	area.setSelectionRange(sel.start + index, sel.start + index + tab[0].length); 
	if(disp) alert( tab.length + " occurrences" );
	return true;
}
 
function hiérarchie() {
	regexpReplace( /\n==== *([^=]+) *==== ?\n/g , "\n===== $1 =====\n" );
	regexpReplace( /\n=== *([^=]+) *=== ?\n/g , "\n==== $1 ====\n" );
	regexpReplace( /\n== *([^=]+) *== ?\n/g , "\n=== $1 ===\n" );
	regexpReplace( /\n= *([^=]+) *= ?\\n/g , "\n== $1 ==\n" );
}
 
 
function ponctuation() {
	regexpReplace( / - (?!(?:[^<]|<(?!math>|\/math>))*<\/math>)/g, " — ");
	regexpReplace( /([ ,;.:])"([^"]+)"/g, "$1« $2 »");
	regexpReplace( /([^\s])(:(?!(?:[^\[])*\])|;)/g, "$1 $2");
	regexpReplace( /,([^\s])(?!(?:[^<]|<(?!math>|\/math>))*<\/math>)/g, ", $1");
}
 
function HTML2TeX(frac) {
	var unicode = "– α β γ Γ δ Δ ε ζ η θ ι κ λ μ ν ξ π ρ σ Σ τ υ φ ϕ χ ψ Ψ ω Ω ∇ ² ³ ∫ ∬ ∭ ∂ ⋅ ∞".split(/ /g);
	var tex = "- \\alpha \\beta \\gamma \\Gamma \\delta \\Delta \\epsilon \\zeta \\eta \\theta \\iota \\kappa \\lambda \\mu \\nu \\xi \\pi \\rho \\sigma \\Sigma \\tau \\upsilon \\varphi \\phi \\chi \\psi \\Psi \\omega \\Omega \\nabla ^2 ^3 \\int \\iint \\iiint \\partial \\cdot \\infty".split(/ /g);
	for( var i in unicode ) {
		regexpReplace(new RegExp(unicode[i] + "(?=(?:[^<]|<(?!math>|/math>))*</math>)","g") , tex[i]);
	}
	regexpReplace(/<sub>((?:[^<]|<(?!\/sub>))*)<\/sub>/ig , "_{$1}");
	regexpReplace(/<sup>((?:[^<]|<(?!\/sup>))*)<\/sup>/ig , "^{$1}");
	if(frac){
 	regexpReplace(/\(([^\(\/]+)\/([^\)\/]+)\)(?=(?:[^<]|<(?!math>|\/math>))*<\/math>)/ig, "\\frac{$1}{$2}" );
	regexpReplace(/\(([^\(\)]+)\)\^{-1}/g,"\\frac{1}{$1}");
	}
	regexpReplace(/''(?=(?:[^<]|<(?!math>|\/math>))*<\/math>)/ig, "" );
	regexpReplace(/{(.)}/ig,"$1");
}