Usor:Hsarrazin/vector.js

E Wikisource

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// bouton Typo //
$.typo = {
  todo: new Array(),
  add: function(fonction) {
    this.todo.push(fonction);
  },
  exec: function() {
    var txt = $('#wpTextbox1').val();
    for(var i in this.todo) {
      txt = this.todo[i](txt);
    }
    $('#wpTextbox1').val(txt);
  },
  editForm: function() {
    if($('#wpTextbox1').length == 0)
      return;
    if(mw.user.options.get('usebetatoolbar')) {
      mw.loader.using('ext.wikiEditor', function() {
        $('#wpTextbox1').wikiEditor('addToToolbar', {
          section: 'advanced',
          group: 'format',
          tools: {
            'typo': {
              label: 'Corrections',
              type: 'button',
              icon: '//upload.wikimedia.org/wikipedia/commons/8/82/Toolbaricon_regular_T.png'
            }
          }
        });
        $('img[rel="typo"]').mouseup(function() {
          $.typo.exec();
        });
      });
    } else {
      $("#toolbar").append('<img id="typo-button" width="23" height="22" border="0" src="//upload.wikimedia.org/wikipedia/commons/9/9f/Button_defaultsort.png" alt="Corrections" title="Corrections" style="cursor: pointer;">');
      $('#typo-button').mouseup(function() {
        $.typo.exec();
      });
    }
  }
}
 
if($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 ) {
$(function() {
  //typo
  $.typo.add(function(str) {
    // espace
    str = str.replace(/(\n\n\n)+/g, "\n");
    str = str.replace(/- \n/g, "");
 
    // fix divers
    str = str.replace(/ +/g," ");
 
    //----- typographie
 
    // espace insécable remplacé par une espace.
    str = str.replace(' ([;:!\?»])', ' $1');
    str = str.replace('« ', '« ');
    str = str.replace(' »', ' »');
 
    str = str.replace(/\n +/g, "\n");
    str = str.replace(/ -/g, " —");
    str = str.replace(/- /g, "— ");
    str = str.replace(/ - /g, " — ");
    str = str.replace(/ -,/g, " —,");
    str = str.replace(/ - ,/g, " —,");
 
    str = str.replace(/[  ]*([;:\!\?])[  ]*/g, " $1 ");
    str = str.replace(/([;:\!\?]) \n/g, "$1\n");
    str = str.replace(/( *),( *)/g, ", ");
    str = str.replace(/(\[\[.*?) :/g, "$1:");
    str = str.replace(/: (.*?\]\])/g, ":$1");
    str = str.replace(/\n : */g, "\n:");
    str = str.replace(/[  ]+\)/g, ")");
 
    str = str.replace(/([^'])'([^'])/g, "$1’$2");
    str = str.replace(/’ /g, "’");
    str = str.replace(/ ’/g, "’");
    str = str.replace(/ ’ /g, "’");
    str = str.replace(/ '',/g, "'',");
    str = str.replace(/ ''\./g, "''.");    
    str = str.replace(/ \./g, "\.");
 
    str = str.replace(/\.\.\./g, "…");
    str = str.replace(/\.\.\.\.+/g, "\.\.\.");
    str = str.replace(/etc\.\.+/g, "etc\.");
 
    str = str.replace(/oeu/g, "œu");
    str = str.replace(/O[Ee]/g, "Œ");
    str = str.replace(/oeil/g, "œil");
 
    str = str.replace(/"([^"].*?)"/g, "«$1»");
    str = str.replace(/( *)«( *)/g, " « ");
    str = str.replace(/( *)»( *)/g, " » ");
    str = str.replace(/» ,/g, "»,");
    str = str.replace(/» \./g, "».");
 
    str = str.replace(/(\f*)’(\f*)/g, "’");
 
    //sauts de lignes intempestifs
    str = str.replace(/\n «/g, "\n«");
 
    // fix catégories
    str = str.replace(/\[\[ ?[Cc]ategory/g, "[[Catégorie");
    str = str.replace(/\[\[ ?[Cc]atégorie ?: ?/g, "[[Catégorie:");
 
    // réparation liens internes
    str = str.replace(/\[\[ ?([^\]]*?) ?\]\]/g, "[[$1]]");
    str = str.replace(/\[\[([^\]]*?)( |_)#([^\]]*?)\]\]/g, "[[$1#$3]]");
 
    // syntaxe wiki
    str = str.replace(/<i>(.*?)<\/i>/gi, "''$1''");
    str = str.replace(/<b>(.*?)<\/b>/gi, "'''$1'''");
    str = str.replace(/<br\/>/gi, "<br />");
    str = str.replace(/<br>/gi, "<br />");
    str = str.replace(/^<br \/>/g, "<nowiki />"); //pour le saut de ligne entre 2 pages
 
    //* puces
    str = str.replace(/^([\*#]+) /gm, "$1");
    str = str.replace(/^([\*#]+)/gm, "$1 ");

    str = str.replace(/(…\. )+/g, "… ");
    str = str.replace(/( …)+/g, "…");
 
    // Défaire certaines modifications, au moins pour &nbsp; qui est fréquemment
    // utilisé. Cet ensemble de modification doit être faite après toutes les
    // autres modifications.
    str = str.replace(/&nbsp ;/g, "&nbsp;");
 
    return $.trim(str);
  });
 
  //simplifier certains liens : [[Dog|dog]] to [[dog]] and [[Dog|dogs]] to [[dog]]s
  $.typo.add(function(str) {
    var m = str.match(/\[\[([^[]*?)\|([^[]*?)\]\]/g);
    if (m) {
      for (var i = 0; i < m.length; i++) {
        var n_arr = m[i].toString().match(/\[\[([^[]*?)\|([^[]*?)\]\]/);
        var n = n_arr[0];
        var a = n_arr[1];
        var b = n_arr[2];
        var temp = $.trim(a).toLowerCase();
        if (b.indexOf(a) == 0 || b.indexOf(temp) == 0) {
          var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w]*?)\]\]/i, "[[$2]]$3");
          str = str.replace(n, k);
        }
      }
    }
    str = str.replace(/\[\[([^\]\|]+)\|([^\]\|]+)\]\]([A-Za-z\'][A-Za-z]*)([\.\,\;\:\"\!\?\s\n])/g, "[[$1|$2$3]]$4");

    return $.trim(str);
  });
 
  $.typo.editForm();
});
}