User:LOL/mmastylefix.js
Appearance
< User:LOL
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:LOL/mmastylefix. |
var mma = mma || {};
mma.tb;
mma.exceptions = [];
mma.exceptions.push({regex: /\bd'arce\b/gi, replacement: 'D\'Arce'},
{regex: /\bperuvian\b/gi, replacement: 'Peruvian'},
{regex: /\bbrabo\b/gi, replacement: 'Brabo'},
{regex: /\bezekiel\b/gi, replacement: 'Ezekiel'});
mma.fixAll = function() {
mma.fixCaps(mma.tb.value);
mma.fixEvents(mma.tb.value);
mma.fixLocations(mma.tb.value);
mma.removeFlags(mma.tb.value);
mma.useDashes(mma.tb.value);
mma.useDts(mma.tb.value);
mma.misc(mma.tb.value);
}
mma.toLowerCase = function(s) {
s = s.toLowerCase();
s = s.replace(/\u2019/g, '\''); // Replace curly apostrophe
for (var i in mma.exceptions) {
var me = mma.exceptions[i];
s = s.replace(me.regex, me.replacement);
}
return s;
}
mma.fixCaps = function(s) {
// Heading ending in "Record"
s = s.replace(/([A-Za-z]) +Record( *=+ *)$/gm, '$1 record$2');
// Heading ending in "Career"
s = s.replace(/([A-Za-z]) +Career( *=+ *)$/gm, '$1 career$2');
// "Mixed Martial Art" in headings
s = s.replace(/^(=+ *)mixed +martial +art/gm, '$1Mixed martial art');
// <ul> entries containing "Bout"
s = s.replace(/^(\* *[A-Za-z' ]+?) +Bout *: *(.)/gm, '$1 bout: $2');
// Heading containing "Card"
s = s.replace(/^(={2,}.+?)Card\b/gm, '$1card');
// Heading containing "Match"
s = s.replace(/^(={2,}.+?)Match\b/gm, '$1match');
// Heading containing "Official Fight"
s = s.replace(/^(={2,}.+?)Official +Fight\b/gm, '$1Official fight');
// Technical/Verbal Submission
s = s.replace(/(technical|verbal) *submission *\(/gi, '$1 submission (');
// Technical Decision/Draw
s = s.replace(/(technical) *d(ecision|raw)/gi, '$1 d$2');
// No contest
s = s.replace(/(no) +contest/gi, '$1 contest');
// PRIDE
s = s.replace(/\bPRIDE\b/g, 'Pride');
// DREAM
s = s.replace(/\bDREAM\b/g, 'Dream');
// DEEP
s = s.replace(/\bDEEP\b/g, 'Deep');
// RINGS
s = s.replace(/\bRINGS\b/g, 'Rings');
// ICON
s = s.replace(/\bICON\b/g, 'Icon');
// HERO'S
s = s.replace(/\bHERO'[Ss]\b/g, 'Hero\'s');
var re, m;
// Capitalization of meridian per MOS:TIME
s = s.replace(/([^.:a-zA-Z0-9]\d{1,2})(:\d{2})?( *| )?a(\.?)m(\W)/gi, '$1$2 a$4m$5');
s = s.replace(/([^.:a-zA-Z0-9]\d{1,2})(:\d{2})?( *| )?p(\.?)m(\W)/gi, '$1$2 p$4m$5');
// Method capitalization
re = /\b(decision|submission|draw|no contest|NC|DQ|knockout|KO|TKO) *\( *([^\)\n]+?) *\)/gi;
while ((m = re.exec(s)) != null) {
s = s.substring(0, m.index + m[1].length) + ' (' + mma.toLowerCase(m[2])
+ ')' + s.substring(m.index + m[0].length);
}
mma.tb.value = s;
}
mma.fixEvents = function(s) {
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?.+?)(?: +- *| *- +)/gm,
'$1: ');
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?\[\[) *([^\|]+?) *\| *\2(?: *: *| +- *| *- +)[^\]]+\]\]/gm,
'$1$2]]');
mma.tb.value = s;
}
mma.fixLocations = function(s) {
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)\[\[ *([A-Za-z ]+?) *(?: *,|\]\] *, *\[\[) *(Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming)(?: *\|\1(?: *\]\] *, *\[\[ *\2 *)?)? *\]\](?: *, *(?:\[\[)? *(?:United States|U\.?S\.?A?\.?) *(?:\]\])?)? *$/gm, '$1[[$2, $3|$2]], [[$3]], [[United States]]');
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)\[\[ *([A-Za-z ]+?) *(?: *,|\]\] *, *\[\[) *(British Columbia|Alberta|Saskatchewan|Manitoba|Ontario|Quebec|Nova Scotia|New Brunswick|Prince Edward Island|Newfoundland|Yukon|Northwest Territories|Nunavut)(?: *\|\1(?: *\]\] *, *\[\[ *\2 *)?)? *\]\](?: *, *(?:\[\[)? *Canada *(?:\]\])?)? *$/gm, '$1[[$2, $3|$2]], [[$3]], [[Canada]]');
mma.tb.value = s;
}
mma.removeFlags = function(s) {
s = s.replace(/^(\| *(?:align *= *['"]?(?:left|center)['"]? *\| *)?)(\{\{[Ff]lag ?icon[^\}]+\}\} *)/gm, '$1');
s = s.replace(/^(\|.+?) *\{\{[Ff]lag ?icon *\|[^\}]+\}\} *$/gm, '$1');
mma.tb.value = s;
}
mma.toggleDates = function(s) {
// [dm]/[dm]/yy dates
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)(\d{1,2})\/(\d{1,2})\/(\d{4}|\d{2})\b/gm,
'$1$3/$2/$4');
// {{dts|yyyy-dd-mm}}
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?\{\{dts\|\d{4}-)(\d{1,2})-(\d{1,2})/gm,
'$1$3-$2');
mma.tb.value = s;
}
mma.useDashes = function(s) {
// En dashes
s = s.replace(/([^\/\w-]\d{1,2})-(\d{1,2}[^\/\w-/])/g, '$1–$2');
s = s.replace(/([^\/\w-]\d{1,2})[-–](\d{1,2})[-–](\d{1,2}[^\/\w-])/g,
'$1–$2–$3');
mma.tb.value = s;
}
mma.useDts = function(s) {
var re, m;
// ISO 8601-like dates
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)(\d{4}-\d{1,2}-\d{1,2})\b/gm,
'$1{{dts|$2}}');
// m/d/yy dates
re = /^(\| *(?:align *= *['"]?left['"]? *\| *)?)(\d{1,2})\/(\d{1,2})\/(\d{4}|\d{2})\b/gm;
while ((m = re.exec(s)) != null) {
if (m[4].length == 2) {
// Handle YY with an arbitrary cutoff to determine century
if (parseInt(m[4], 10) < 50) {
m[4] = '20' + m[4];
} else {
m[4] = '19' + m[4];
}
}
// Leading zeroes
if (m[2].length == 1) {
m[2] = '0' + m[2];
}
if (m[3].length == 1) {
m[3] = '0' + m[3];
}
s = s.substring(0, m.index + m[1].length) + '{{dts|' + m[4] + '-' + m[2]
+ '-' + m[3] + '}}' + s.substring(m.index + m[0].length);
}
// mdy dates without {{dts}}
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)(January|February|March|April|May|June|July|August|September|October|November|December) *(\d{1,2}) *,? *(\d{4})\b/gm,
'$1{{dts|$4|$2|$3}}');
// dmy dates without {{dts}}
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)(\d{1,2}) *,? *(January|February|March|April|May|June|July|August|September|October|November|December) *,? *(\d{4})\b/gm,
'$1{{dts|format=dmy|$4|$3|$2}}');
mma.tb.value = s;
}
mma.misc = function(s) {
// Exactly one space after {{flagicon}}
s = s.replace(/\{\{ *([Ff]lagicon) *\| *(\w+) *\}\} *([\w\[])/g,
'{\u007b$1|$2}} $3');
// Replace "lbs" per MOS:NUM
s = s.replace(/(\d+) *lbs\.?/gi, '$1 lb');
// Format draws and NCs in record tables
s = s.replace(/^(\|-[^\n]*\n)\| *(?:(?:align|style) *=[^\|]+\|)? *(?:< *span +.+?<\/span> *)?Draw *$/gm,
'$1| {{draw}}Draw');
s = s.replace(/^(\|-[^\n]*\n)\| *(?:(?:align|style) *=[^\|]+\|)? *(?:< *span +.+?<\/span> *)?(?:NC|No +[Cc]ontest) *$/gm,
'$1| {{nocontest}}NC');
// Use {{small}} instead of unclosed <small>
s = s.replace(/^(\| *(?:align *= *['"]?left['"]? *\| *)?)<small> *([^<\n]+?) *$/gm,
'$1{{small|$2}}');
var re, m;
// Remove extraneous table row syntax
re = /(\n\|-[^\n]*){2,}/g;
while ((m = re.exec(s)) != null) {
s = s.substring(0, m.index)
+ m[0].substring(m[0].lastIndexOf('\n'))
+ s.substring(m.index + m[0].length);
}
mma.tb.value = s;
}
$(function() {
if (document.forms.editform) {
mma.tb = document.getElementById('wpTextbox1');
mw.util.addPortletLink('p-tb', 'javascript:mma.fixAll()', 'MMA style cleanup', 't-mmastylefix', 'Fix various style and formatting issues that are common in MMA articles', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.fixCaps(mma.tb.value)', '\u00a0 \u00a0 - capitalization', 't-mmacaps', 'Fix capitalization', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.useDashes(mma.tb.value)', '\u00a0 \u00a0 - dashes', 't-mmadashes', 'Use dashes', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.fixEvents(mma.tb.value)', '\u00a0 \u00a0 - events', 't-mmaevents', 'Fix event format', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.fixLocations(mma.tb.value)', '\u00a0 \u00a0 - locations', 't-mmalocs', 'Fix format of some locations', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.removeFlags(mma.tb.value)', '\u00a0 \u00a0 - flags', 't-mmaflags', 'Remove unnecessary flags from MMA record tables', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.toggleDates(mma.tb.value)', '\u00a0 \u00a0 - toggle table date fmt', 't-mmadmy', 'Swap the first two numbers in d/m/y or m/d/y dates in record tables', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.useDts(mma.tb.value)', '\u00a0 \u00a0 - {{dts}}', 't-mmadts', 'Use {{dts}} in MMA record tables', '', '');
mw.util.addPortletLink('p-tb', 'javascript:mma.misc(mma.tb.value)', '\u00a0 \u00a0 - misc', 't-mmamisc', 'Misc cleanup', '', '');
}
});