MediaWiki:Gadget-correct long s.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.
// History of this file is in [[Utilisateur:Acer11/Trévoux.js]]

 
/**
 * Setup the correct long s:
 */
if ( window.CorrectLongS === undefined ) {
	window.CorrectLongS = {};
}

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


ls.exec_regexp = function(left, right, text)
{
    try {
        var search = new RegExp(left, "g");
        text = text.replace(search, right);
    }
    catch (err) {
        alert('"' + left + '", "' + right + '"  Erreur de syntaxe ?' + ' ' + err);
        return null;
    }

    return text;
};

// Appliq transform typographiq sur portions textes
// sur lesquels il est sûr de travailler. Voir la var splitter.

ls.apply_regexp = function(defs, text)
{
  var lng = defs.length;
  if (lng % 2 !== 0) {
     alert("Le nombre de regexp doit être pair");
     return text;
  }
  for (var i = 0; i < lng; i += 2) {
     var new_text = ls.exec_regexp(defs[i + 0], defs[i + 1], text);
     if (new_text === null)
         break;
     text = new_text;
  }
  return text;
};

ls.typographie_fragment = function(text) {
  text = ls.apply_regexp(ls.typo_def_long_s_1, text);
  text = ls.apply_regexp(ls.typo_def_long_s_2, text);
  text = ls.apply_regexp(ls.typo_def_long_s_3, text);
  text = ls.apply_regexp(ls.typo_def_long_s_4, text);
  text = ls.apply_regexp(ls.typo_def_long_s_5, text);
  // User regexp is a global, for historical reason ;(
  if (window.typo_def_long_s_user) {
      text = ls.apply_regexp(window.typo_def_long_s_user, text);
  }
  text = ls.apply_regexp(ls.typo_def_long_s_last, text);

  return text;
};

// Same as above but debug version to try to understand which regexp modified some
// text, use it in a console ala CorrectLongS.typographie_fragment_debug("pouffière");
// or CorrectLongS.typographie_debug("text");
ls.apply_regexp_debug = function(defs, data_file, text)
{
  var lng = defs.length;
  if (lng % 2 !== 0) {
     alert("Le nombre de regexp doit être pair");
     return text;
  }
  var match_text = '';
  for (var i = 0; i < lng; i += 2) {
    var new_text = ls.exec_regexp(defs[i + 0], defs[i + 1], text);
    if (new_text === null)
        break;
    if (new_text != text) {
       match_text += text + '\n' + new_text + '\n' + data_file + '(' + (i/2) + ')\n' + defs[i + 0] + '\n' + defs[i + 1] + '\n';
    }
    text = new_text;
  }
  return [ text, match_text ];
};

ls.typographie_fragment_debug = function(text) {
  var match_text = '';
  var ret = ls.apply_regexp_debug(ls.typo_def_long_s_1, "long_s_1", text);
  text = ret[0];
  match_text += ret[1];
  ret = ls.apply_regexp_debug(ls.typo_def_long_s_2, "long_s_2", text);
  text = ret[0];
  match_text += ret[1];
  ret = ls.apply_regexp_debug(ls.typo_def_long_s_3, "long_s_3", text);
  text = ret[0];
  match_text += ret[1];
  ret = ls.apply_regexp_debug(ls.typo_def_long_s_4, "long_s_4", text);
  text = ret[0];
  match_text += ret[1];
  ret = ls.apply_regexp_debug(ls.typo_def_long_s_5, "long_s_5", text);
  text = ret[0];
  match_text += ret[1];
  // User regexp is a global, for historical reason ;(
  if (window.typo_def_long_s_user) {
      ret = ls.apply_regexp_debug(window.typo_def_long_s_user, "long_s_user", text);
      text = ret[0];
      match_text += ret[1];
  }
  ret = ls.apply_regexp_debug(ls.typo_def_long_s_last, "long_s_last", text);
  text = ret[0];
  match_text += ret[1];

  alert(match_text);

  return text;
};

ls.typographie_debug = function(text) {
      var new_text = '';
      var last_match = 0;
      // Split the text in part which are safe to transform, e.g we don't want
      // to apply typographie_fragment on a <math>.*</math> or a &nbsp; etc.
      var splitter = new RegExp("##.*##|<math>.*</math>|<[a-zA-z0-9 =\"']+>|</[a-zA-z0-9 =\"']+>|style=\".*\"|&nbsp;|&mdash;|<!--.*-->|\n:[:]*|\n;[;]*|[[][[].*]]", "gm");
      var result = null;
      while ((result = splitter.exec(text)) !== null) {
		new_text += ls.typographie_fragment_debug(text.slice(last_match, splitter.lastIndex - result[0].length));
		new_text += result;
		last_match = splitter.lastIndex;
      }
      new_text += ls.typographie_fragment_debug(text.slice(last_match));
};

ls.typographie = function() {
  if (1 || mw.config.get('wgNamespaceNumber') == 104 /*|| mw.config.get('wgNamespaceNumber') == 0*/) {
    var editbox = document.getElementById('wpTextbox1');
    if (editbox) {
      var text = editbox.value;
      var new_text = '';
      var last_match = 0;
      // Split the text in part which are safe to transform, e.g we don't want
      // to apply typographie_fragment on a <math>.*</math> or a &nbsp; etc.
      var splitter = new RegExp("##.*##|<math>.*</math>|<[a-zA-z0-9 =\"']+>|</[a-zA-z0-9 =\"']+>|style=\".*\"|&nbsp;|&mdash;|<!--.*-->|\n:[:]*|\n;[;]*|[[][[].*]]", "gm");
      var result = null;
      while ((result = splitter.exec(text)) !== null) {
		new_text += ls.typographie_fragment(text.slice(last_match, splitter.lastIndex - result[0].length));
		new_text += result;
		last_match = splitter.lastIndex;
      }
      new_text += ls.typographie_fragment(text.slice(last_match));
      editbox.value = new_text;
    }
  }
};

ls.editForm = function () {
	$(function() {
      $('#wpTextbox1').wikiEditor('addToToolbar', {
      	section: 'main',
        group: 'insert',
        tools: {
          'wpRep': {
            label: 'Remplacement auto',
            type: 'button',
            icon: '//upload.wikimedia.org/wikipedia/commons/a/af/Button_Fractur_OCR.png',
            action: {
              type: 'callback',
              execute: ls.typographie
            }
          }
        }
      });
  	  $('[rel="wpRep"]').width(36);
  	});
};

if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 ) {
	var modules =  [
		'ext.gadget.correct_long_s_data', 'ext.wikiEditor'
		];
	$.when(
		mw.loader.using( modules ),
		$.ready
	).then(
		ls.editForm
	);
}
 
}( mediaWiki, jQuery, window.CorrectLongS ) );