Jump to content

User:Gerbrant/edit/autoReplace/default.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.
new function()
{

wloRE = "\\[\\[";
wlcRE = "\\]\\]";
monthRE = "([Jj]anuary|[Ff]ebruary|[Mm]arch|[Aa]pril|[Mm]ay|[Jj]une|[Jj]uly|[Aa]ugust|[Ss]eptember|[Oo]ctober|[Nn]ovember|[Dd]ecember)";
dayRE = "(\\d{1,2})(?:th)?";
yearRE = "(\\d{4})";

monthDayRE = monthRE + "\\s*" + dayRE;
dayMonthRE = dayRE + "\\s*" + monthRE;
wlMonthDayRE = wloRE + monthDayRE + wlcRE;
wlDayMonthRE = wloRE + dayMonthRE + wlcRE;
wlYearRE = wloRE + yearRE + wlcRE;

csRE1 = "\\s*(?:,\\s*|\\s+)";
csRE2 = "\\s*,?\\s*";

unitPRE = "([YZEPTGMkKhdcmµnpfazy]|da|)";
unitRE = "([gsmAKNJWCVΩFTHSM]|mol|cd|rad|sr|Hz|Pa|lm|lx|Wb|Bq|Gy|Sv|kat|°C)";

function fixCaps(a){  return a.slice(0,1) + a.slice(1).toLowerCase();  }

function monthDayYear(a, m, d, y){return "[[" + fixCaps(m) + " " + d + "]], [[" + y + "]]";}
function dayMonthYear(a, d, m, y){return "[[" + d + " " + fixCaps(m) + "]] [[" + y + "]]";}

function binPrefCap(p){  return p == "k" || p == "K" ? "k" : p.toUpperCase();  }

module("Gerbrant.edit.autoReplace.default", [

//Leave the following alone:
{re: "\\[\\[\\d{4}-\\d{2}-\\d{2}\\]\\]|\\|\\s*accessdate\\s*=\\s*\\d{4}-\\d{2}-\\d{2}|ISBN [-0-9]+|\\|\\s*url\\s*=.*(?:\\||\\}\\})|http://[-A-Za-z0-9._\\\\/~%+&#?!=@\\u0028\\u0029]+|\\s*\\n\\s*----|\\s*[\\n\\r]\\|-|\\|\\s*isbn\\s*=\\s*[-0-9]+|\\{\\{convert\\s*\\|.*?\\}\\}", hf: function(a){return a;}},

{re: "ISBN([-0-9]+)", hf: function(a, n){return "ISBN " + n;}},

{re: " --(?!>) ?| —(?! )", hf: function(){return " — ";}},
{re: "(?!<!)([^ ]{2})--(?!>) ?", hf: function(a,s){return s + " — ";}},
{re: "([^ ])— ?", hf: function(a,l){return l + " — ";}},
{re: " -(?!->) ?| –(?! )", hf: function(){return " – ";}},
{re: "(?!<!-)([^ ]{3})- ", hf: function(a,s){return s + " – ";}},
{re: "([^ ])– ", hf: function(a,l){return l + " – ";}},
{re: "(€[0-9,]+\\.)--?", hf: function(a,v){return v + "—";}},
{re: "(\\d)-(?=\\d)", hf: function(a,l){return l + "–";}},
{re: "\\.\\.\\.", hf: function(){return "…";}},
// Convert the word ohm(s) or the html entity into the actual Ω symbol (Omega, not the actual ohm symbol &#8486;) and make sure it's spaced
{re: "(\\d)\\s?" + unitPRE + "(?:\\s|-|)(?:&Omega;|ohm|Ohm)s?([\\s,.])", hf: function(a,d,p,t){return d + " " + p + "Ω" + t;}},
// Convert various micro symbols into the actual micro symbol, make sure it's spaced
{re: "(\\d)\\s?(?:&mu;|μ|&micro;)" + unitRE + "([\\s,.])", hf: function(a,d,u,t){return d + " µ" + u + t;}},
// Convert capital K to lowercase k in units
{re: "(\\d)\\s?K" + unitRE + "([\\s,.])", hf: function(a,d,u,t){return d + " k" + u + t;}},
// Capitalize units correctly (kHz is already covered)
{re: "(\\d)\\s?(?!MHz)(?:[Mm][Hh][Zz])([\\s,.])", hf: function(a,d,t){return d + " MHz" + t;}},
{re: "(\\d)\\s?(?!GHz)(?:[Gg][Hh][Zz])([\\s,.])", hf: function(a,d,t){return d + " GHz" + t;}},
{re: "(\\d)\\s?" + unitPRE + "(?:hz|HZ)([\\s,.])", hf: function(a,d,p,t){return d + " " + p + "Hz" + t;}},
{re: "(\\d)\\s?" + unitPRE + "(?:pa|PA)([\\s,.])", hf: function(a,d,p,t){return d + " " + p + "Pa" + t;}},
// Add a space before dB or B
{re: "(\\d)dB\\b", hf: function(a,d){return d + " dB";}},
{re: "(\\d)B\\b", hf: function(a,d){return d + " B";}},
// Add a space before any units that were missed before
// Separate one for seconds since they give a lot of false positives like "1970s ".  Only difference is mandatory prefix.
// (changed the way that works – now uses a negative lookahead)
{re: "((?:\\D|^)(?!\\d\\d(?:\\d\\d)?s)[0-9,.]*\\d)" + unitPRE + unitRE + "([\\s,.])", hf: function(a,d,p,u,t){return d + " " + p + u + t;}},
// bps or b/s or bits/s --> bit/s
{re: "([KkMmGgTtPpEeYyZz]|)(?:bps|bits\\/s|b\\/s)", hf: function(a,p){return p + "bit/s";}},
// Bps or byte/s or bytes/s --> B/s
{re: "([KkMmGgTtPpEeYyZz]|)(?:Bps|bytes?\\/s)", hf: function(a,p){return binPrefCap(p) + "B/s";}},
// (After that, – would req. multiple runs, so I integrated this in the above) make capitalization correct
{re: "([Kmgtpeyz])(bit|B)\\/s", hf: function(a,p,u){return binPrefCap(p) + u + "/s";}},
// Common error
{re: "\\bmibi(bit|byte)\\b", hf: function(a,u){return "mebi" + u;}},
// Convert hyphen next to a number into a minus sign character
{re: "([^a-zA-Z0-9\\,\\_\\{])-(\\d)", hf: function(a,l,r){return l + "−" + r;}},
// Changes 2x3 to 2×3
{re: "((?:\\D|^)(?!0x[0-9A-Fa-f])[0-9,.]*\\d\\s?)x(\\s?\\d)", hf: function(a,l,r){return l + "×" + r;}},
// Changes 10^3 to 10<sup>3</sup>
// Changes x^3 to x<sup>3</sup>
{re: "([0-9a-zA-Z])\\^(\\d+)(?!(?:(?!<math>).)*</math>)", hf: function(a,l,r){return l + "<sup>" + r + "</sup>";}},
// Changes <sup> tags inside <math> tags back into carets
// (don't know of a way to exclude them from the above statement)
//(but I do, see the RE above)
// Changes 2 +/- 3 to 2±3
{re: "(\\s|\\d)\\+\\/?(?:-|−|-)(\\s|\\d)", hf: function(a,l,r){return l + "±" + r;}},
// Remove all extra newlines?
{re: "(?:\\n\\s*){3,}", hf: function(){return "\n\n";}},

{re: "(?:\\n\\s*){2,}([*#;:])", hf: function(a,s){return "\n" + s;}},
{re: "\\[\\[(.*?)\\|\\1\\]\\]", hf: function(a,b){return "[[" + b + "]]";}},
{re: "\\{\\{[Cc]quote\\|((?:.|\\n)*?)\\}\\}", hf: function(a,b)
{  var r = /(\s*\n){2,}/g;
   if(r.test(b)) b = "<p>" + b.replace(r, "</p>\n<p>") + "</p>";
   return "<blockquote>" + b + "</blockquote>";  }},
{re: "\\{\\{(?:[Rr]quote\\|.*?|[Cc]quote2)\\|((?:.|\\n)*?)\\|((?:.|\\n)*?)\\}\\}", hf: function(a,b,c)
{  var r = /(\s*\n){2,}/g;
   if(r.test(b)) b = "<p>" + b.replace(r, "</p>\n<p>") + " – " + c + "</p>";
   else b += " – " + c;
   return "<blockquote>" + b + "</blockquote>";  }},

{re: monthDayRE + csRE1 + yearRE, hf: monthDayYear},
{re: wlMonthDayRE + csRE2 + yearRE, hf: monthDayYear},
{re: monthDayRE + csRE2 + wlYearRE, hf: monthDayYear},

{re: dayMonthRE + csRE2 + yearRE, hf: dayMonthYear},
{re: wlDayMonthRE + csRE2 + yearRE, hf: dayMonthYear},
{re: dayMonthRE + csRE2 + wlYearRE, hf: dayMonthYear},
{re: wloRE + monthRE + "\\s*(\\d{1,2})th" + wlcRE + csRE2 + wlYearRE, hf: monthDayYear},

//Leave all other links alone to prevent accidentally breaking them:
{re: "\\[\\[.*?(?:\\]\\]|\\|)", hf: function(a){return a;}},

{re: "\\d{4}-\\d{2}-\\d{2}(?!\\]\\])", hf: function(a){return "[[" + a + "]]";}},
{re: "(\\d{4})\\.(\\d{2})\\.(\\d{2})", hf: function(a, y, m, d){return "[[" + y + "-" + m + "-" + d + "]]";}},

{re: "\\s*(<ref(?:>|\\s+name)(?:(?!<ref(?:>|\\s+name)).)*(?:</ref>|/>))\\s*([.,:;!?])", hf: function(a,b,c){return c + b;}},
{re: "\\s*<ref(?=>|\\s)", hf: function(){return "<ref";}},

{re: "…\\.", hf: function(){return "…";}}

]);

}