Jump to content

User:Dinoguy1000/scripts/fullwidth2ascii.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/***********************************************************************
 *                                                                     *
 * [[en:User:Dinoguy1000/scripts/fullwidth2ascii.js]]                  *
 *                                                                     *
 * Replaces all instances of ASCII fullwidth characters with           *
 * their original ASCII counterparts, according to the list            *
 * at [[en:User:Dinoguy1000/Cheatsheet#Fullwidth character table]].    *
 *                                                                     *
 * Framework shamelessly ripped from [[en:User:Drilnoth/codefixer.js]] *
 *                                                                     *
 * Spinoff stuff (try to keep everything in sync):                     *
 *  - [[Wikipedia:AutoEd/fullwidth.js]]                                *
 *  - [[User:TheFarix/AWB/scripts/FullwidthReplacer]]                  *
 *                                                                     *
 ***********************************************************************/
 
function fullwidthReplacer() { //MAIN FUNCTION describes list of fixes
    var txt = document.editform.wpTextbox1;
 
    //replace
    //Common punctuation
    txt.value = txt.value.replace(/ /g, ' '); //fullwidth space
    //txt.value = txt.value.replace(/・/g, '·'); //fullwidth interpunct
    txt.value = txt.value.replace(/。/g, '.'); //replace ideographic period with a regular period (from TheFarix's AWB cleanup module)
    //txt.value = txt.value.replace(/・・・/g, '...'); //three interpuncts normally mean an ellipsis
    //txt.value = txt.value.replace(/···/g, '...'); //catch already converted triple interpuncts - it's after the interpunct rule so we only need one go-through to catch partial replacements
    txt.value = txt.value.replace(/…/g, '...'); //it's a little ballsy, I guess, but we're already doing lots of replacements, so... (see also [[WP:ELLIPSES]])
    txt.value = txt.value.replace(/!/g, '!');
    txt.value = txt.value.replace(/(/g, '(');
    txt.value = txt.value.replace(/)/g, ')');
    txt.value = txt.value.replace(/?/g, '?');
 
    //Numbers
    txt.value = txt.value.replace(/0/g, '0');
    txt.value = txt.value.replace(/1/g, '1');
    txt.value = txt.value.replace(/2/g, '2');
    txt.value = txt.value.replace(/3/g, '3');
    txt.value = txt.value.replace(/4/g, '4');
    txt.value = txt.value.replace(/5/g, '5');
    txt.value = txt.value.replace(/6/g, '6');
    txt.value = txt.value.replace(/7/g, '7');
    txt.value = txt.value.replace(/8/g, '8');
    txt.value = txt.value.replace(/9/g, '9');
 
    //Latin letters, uppercase
    txt.value = txt.value.replace(/A/g, 'Α');
    txt.value = txt.value.replace(/B/g, 'B');
    txt.value = txt.value.replace(/C/g, 'C');
    txt.value = txt.value.replace(/D/g, 'D');
    txt.value = txt.value.replace(/E/g, 'E');
    txt.value = txt.value.replace(/F/g, 'F');
    txt.value = txt.value.replace(/G/g, 'G');
    txt.value = txt.value.replace(/H/g, 'H');
    txt.value = txt.value.replace(/I/g, 'I');
    txt.value = txt.value.replace(/J/g, 'J');
    txt.value = txt.value.replace(/K/g, 'K');
    txt.value = txt.value.replace(/L/g, 'L');
    txt.value = txt.value.replace(/M/g, 'M');
    txt.value = txt.value.replace(/N/g, 'N');
    txt.value = txt.value.replace(/O/g, 'O');
    txt.value = txt.value.replace(/P/g, 'P');
    txt.value = txt.value.replace(/Q/g, 'Q');
    txt.value = txt.value.replace(/R/g, 'R');
    txt.value = txt.value.replace(/S/g, 'S');
    txt.value = txt.value.replace(/T/g, 'T');
    txt.value = txt.value.replace(/U/g, 'U');
    txt.value = txt.value.replace(/V/g, 'V');
    txt.value = txt.value.replace(/W/g, 'W');
    txt.value = txt.value.replace(/X/g, 'X');
    txt.value = txt.value.replace(/Y/g, 'Y');
    txt.value = txt.value.replace(/Z/g, 'Z');
 
    //Latin letters, lowercase
    txt.value = txt.value.replace(/a/g, 'a');
    txt.value = txt.value.replace(/b/g, 'b');
    txt.value = txt.value.replace(/c/g, 'c');
    txt.value = txt.value.replace(/d/g, 'd');
    txt.value = txt.value.replace(/e/g, 'e');
    txt.value = txt.value.replace(/f/g, 'f');
    txt.value = txt.value.replace(/g/g, 'g');
    txt.value = txt.value.replace(/h/g, 'h');
    txt.value = txt.value.replace(/i/g, 'i');
    txt.value = txt.value.replace(/j/g, 'j');
    txt.value = txt.value.replace(/k/g, 'k');
    txt.value = txt.value.replace(/l/g, 'l');
    txt.value = txt.value.replace(/m/g, 'm');
    txt.value = txt.value.replace(/n/g, 'n');
    txt.value = txt.value.replace(/o/g, 'o');
    txt.value = txt.value.replace(/p/g, 'p');
    txt.value = txt.value.replace(/q/g, 'q');
    txt.value = txt.value.replace(/r/g, 'r');
    txt.value = txt.value.replace(/s/g, 's');
    txt.value = txt.value.replace(/t/g, 't');
    txt.value = txt.value.replace(/u/g, 'u');
    txt.value = txt.value.replace(/v/g, 'v');
    txt.value = txt.value.replace(/w/g, 'w');
    txt.value = txt.value.replace(/x/g, 'x');
    txt.value = txt.value.replace(/y/g, 'y');
    txt.value = txt.value.replace(/z/g, 'z');
 
    //Other punctuation
    txt.value = txt.value.replace(/"/g, '"');
    txt.value = txt.value.replace(/#/g, '#');
    txt.value = txt.value.replace(/$/g, '$');
    txt.value = txt.value.replace(/%/g, '%');
    txt.value = txt.value.replace(/&/g, '&');
    txt.value = txt.value.replace(/'/g, '\'');
    txt.value = txt.value.replace(/*/g, '*');
    txt.value = txt.value.replace(/+/g, '+');
    txt.value = txt.value.replace(/,/g, ', ');
    //txt.value = txt.value.replace(/、/g, ', '); //replace ideographic comma with a regular comma (from TheFarix's AWB cleanup module)
    txt.value = txt.value.replace(/-/g, '-');
    txt.value = txt.value.replace(/./g, '. ');
    txt.value = txt.value.replace(///g, '\/');
    txt.value = txt.value.replace(/:/g, ': ');
    txt.value = txt.value.replace(/;/g, '; ');
    txt.value = txt.value.replace(/</g, '<');
    txt.value = txt.value.replace(/=/g, '='); //this replacement could break template usage, use {{=}} as an alternative inside templates
    txt.value = txt.value.replace(/>/g, '>');
    txt.value = txt.value.replace(/@/g, '@');
    txt.value = txt.value.replace(/[/g, '['); //this replacement could break wikimarkup usage
    txt.value = txt.value.replace(/\/g, '\\');
    txt.value = txt.value.replace(/]/g, ']'); //this replacement could break wikimarkup usage
    txt.value = txt.value.replace(/^/g, '^');
    txt.value = txt.value.replace(/_/g, '_');
    txt.value = txt.value.replace(/`/g, '`');
    txt.value = txt.value.replace(/{/g, '{'); //this replacement could break wikimarkup usage, use {{(}} as an alternative
    txt.value = txt.value.replace(/|/g, '|'); //this replacement could break template usage, use {{!}} as an alternative inside templates
    txt.value = txt.value.replace(/}/g, '}'); //this replacement could break wikimarkup usage, use {{)}} as an alternative
    txt.value = txt.value.replace(/~/g, '〜'); //ASCII tilde is, apparently, treated as a diacritic in some fonts, so this should be a better replacement
    txt.value = txt.value.replace(/¢/g, '¢');
    txt.value = txt.value.replace(/£/g, '£');
    txt.value = txt.value.replace(/¬/g, '¬');
    txt.value = txt.value.replace(/¦/g, '¦');
    txt.value = txt.value.replace(/¥/g, '¥');
    txt.value = txt.value.replace(/₩/g, '₩');
    txt.value = txt.value.replace(/│/g, '│');
    txt.value = txt.value.replace(/←/g, '←');
    txt.value = txt.value.replace(/↑/g, '↑');
    txt.value = txt.value.replace(/→/g, '→');
    txt.value = txt.value.replace(/↓/g, '↓');
    txt.value = txt.value.replace(/■/g, '■');
    txt.value = txt.value.replace(/○/g, '○');
    txt.value = txt.value.replace(/『/g, '「'); //some Japanese quote replacement (from TheFarix's AWB cleanup module)
    txt.value = txt.value.replace(/』/g, '」'); //some Japanese quote replacement (from TheFarix's AWB cleanup module)
}
 
function fullwidthReplacerStartInEdit() { //Initiates fullwidthReplacer if you are already in edit mode
    fullwidthReplacer();
 
    // Add a tag to the summary box
    var txt = document.editform.wpSummary;
    var summary = "replaced fullwidth chars with ASCII equivalents ([[User:Dinoguy1000/scripts/fullwidth2ascii.js|FullwidthReplacer]])";
	if (txt.value.indexOf(summary) == -1) {
		if (txt.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
			txt.value += "; ";
		}
		txt.value += summary;
	}
 
    document.editform.wpDiff.click()
}
 
if(queryString('fullwidthReplacerStartFromView')) $(function() { fullwidthReplacerStartFromView(); }) //Part of next function
function fullwidthReplacerStartFromView() { //Initiates FullwidthReplacer if you are viewing the article without currently editing it
  var txt = document.getElementById('wpTextbox1');
  if(!txt) return;
  fullwidthReplacer();
  document.getElementById('wpSummary').value += 'Replaced fullwidth chars with ASCII equivalents using [[User:Dinoguy1000/scripts/fullwidth2ascii.js|FullwidthReplacer]]';
    document.editform.wpDiff.click()
}
 
function queryString(p) { //Allows URI to be properly decoded so that fullwidthReplacerStartFromView() works properly
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try {
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}
 
$(function () { //Adds "replace fullwidth" tabs to page.
    if(mw.config.get("wgNamespaceNumber") == 0 || mw.config.get("wgNamespaceNumber") == 2) {
        if(document.forms.editform) {
            mw.util.addPortletLink('p-cactions', 'javascript:fullwidthReplacerStartInEdit()', 'replace fullwidth', 'ca-fullwidthreplaceredit', 'Replaces fullwidth ASCII characters with their ASCII equivalents', '', document.getElementById('ca-talk'));
        }
        if(mw.config.get("wgAction") == "view"){
             var url = mw.config.get("wgServer") + mw.config.get("wgScript") + '?title=' + encodeURIComponent(mw.config.get("wgPageName")) + '&action=edit';
             mw.util.addPortletLink('p-cactions', url + '&fullwidthReplacerStartFromView=true', 'replace fullwidth', 'ca-fullwidthreplacerview', 'Replaces fullwidth ASCII characters with their ASCII equivalents'), '', document.getElementById('ca-talk');
        }
    }
});