User:Dinoguy1000/scripts/fullwidth2ascii.js

/*********************************************************************** *                                                                    * * 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. * *                                                                    * * Framework shamelessly ripped from en:User:Drilnoth/codefixer.js * *                                                                    * * Spinoff stuff (try to keep everything in sync):                     * * - 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(/０/g, '0'); txt.value = txt.value.replace(/１/g, '1'); txt.value = txt.value.replace(/２/g, '2'); txt.value = txt.value.replace(/３/g, '3'); txt.value = txt.value.replace(/４/g, '4'); txt.value = txt.value.replace(/５/g, '5'); txt.value = txt.value.replace(/６/g, '6'); txt.value = txt.value.replace(/７/g, '7'); txt.value = txt.value.replace(/８/g, '8'); txt.value = txt.value.replace(/９/g, '9'); //Latin letters, uppercase txt.value = txt.value.replace(/Ａ/g, 'Α'); txt.value = txt.value.replace(/Ｂ/g, 'B'); txt.value = txt.value.replace(/Ｃ/g, 'C'); txt.value = txt.value.replace(/Ｄ/g, 'D'); txt.value = txt.value.replace(/Ｅ/g, 'E'); txt.value = txt.value.replace(/Ｆ/g, 'F'); txt.value = txt.value.replace(/Ｇ/g, 'G'); txt.value = txt.value.replace(/Ｈ/g, 'H'); txt.value = txt.value.replace(/Ｉ/g, 'I'); txt.value = txt.value.replace(/Ｊ/g, 'J'); txt.value = txt.value.replace(/Ｋ/g, 'K'); txt.value = txt.value.replace(/Ｌ/g, 'L'); txt.value = txt.value.replace(/Ｍ/g, 'M'); txt.value = txt.value.replace(/Ｎ/g, 'N'); txt.value = txt.value.replace(/Ｏ/g, 'O'); txt.value = txt.value.replace(/Ｐ/g, 'P'); txt.value = txt.value.replace(/Ｑ/g, 'Q'); txt.value = txt.value.replace(/Ｒ/g, 'R'); txt.value = txt.value.replace(/Ｓ/g, 'S'); txt.value = txt.value.replace(/Ｔ/g, 'T'); txt.value = txt.value.replace(/Ｕ/g, 'U'); txt.value = txt.value.replace(/Ｖ/g, 'V'); txt.value = txt.value.replace(/Ｗ/g, 'W'); txt.value = txt.value.replace(/Ｘ/g, 'X'); txt.value = txt.value.replace(/Ｙ/g, 'Y'); txt.value = txt.value.replace(/Ｚ/g, 'Z'); //Latin letters, lowercase txt.value = txt.value.replace(/ａ/g, 'a'); txt.value = txt.value.replace(/ｂ/g, 'b'); txt.value = txt.value.replace(/ｃ/g, 'c'); txt.value = txt.value.replace(/ｄ/g, 'd'); txt.value = txt.value.replace(/ｅ/g, 'e'); txt.value = txt.value.replace(/ｆ/g, 'f'); txt.value = txt.value.replace(/ｇ/g, 'g'); txt.value = txt.value.replace(/ｈ/g, 'h'); txt.value = txt.value.replace(/ｉ/g, 'i'); txt.value = txt.value.replace(/ｊ/g, 'j'); txt.value = txt.value.replace(/ｋ/g, 'k'); txt.value = txt.value.replace(/ｌ/g, 'l'); txt.value = txt.value.replace(/ｍ/g, 'm'); txt.value = txt.value.replace(/ｎ/g, 'n'); txt.value = txt.value.replace(/ｏ/g, 'o'); txt.value = txt.value.replace(/ｐ/g, 'p'); txt.value = txt.value.replace(/ｑ/g, 'q'); txt.value = txt.value.replace(/ｒ/g, 'r'); txt.value = txt.value.replace(/ｓ/g, 's'); txt.value = txt.value.replace(/ｔ/g, 't'); txt.value = txt.value.replace(/ｕ/g, 'u'); txt.value = txt.value.replace(/ｖ/g, 'v'); txt.value = txt.value.replace(/ｗ/g, 'w'); txt.value = txt.value.replace(/ｘ/g, 'x'); txt.value = txt.value.replace(/ｙ/g, 'y'); txt.value = txt.value.replace(/ｚ/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 (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 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'); }   } });