User:Gerbrant/edit/autoReplace/default.js

From Wikipedia, the free encyclopedia

< User:Gerbrant | edit | autoReplace

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

new function()
{

wloRE = "\\[\\[";
wlcRE = "\\]\\]";
monthRE = "([J|j]anuary|[|F|f]ebruary|[|M|m]arch|[|A|a]pril|[|M|m]ay|[|J|j]une|[|J|j]uly|[|A|a]ugust|[|S|s]eptember|[|O|o]ctober|[|N|n]ovember|[|D|d]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", [

{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: "\\{\\{cquote\\|((?:.|\\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: 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}

]);

}