User:Est. 2021/Script/AutoCleanup/Templates.js
Appearance
< User:Est. 2021 | Script
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Est. 2021/Script/AutoCleanup/Templates.
/*
Subpage of [[User:Est. 2021/Script/AutoCleanup.js]]
*/
// <syntaxhighlight lang="js">
$(function() {
function clean_wikitext(text) {
const replacements = [
// Tags #OWN
[/(\n)*<(small)>([^\2\/]*)<\/?\2>/gis, '$1{{$2|$3}}'], // [[Template:Small]]
[/\[\[\:([\w]{2,3})\:([^\|\]]+)\|\2\]\]/g, '{{ill|$2|$1}}'], // [[Template:Ill]]
[/\[\[\:([\w]{2,3})\:(([^\|\]]+) \([^\|\]]+\))\|\3\]\]/g, '{{ill|$2|$1|lt=$3}}'], // [[Template:Ill]] (piped)
[/(\w*)\{\{anchor\|\1\}\}/gi, '{{va|$1}}'], // [[Template:Va]] // Shorten anchors
// Tags (spacing) #OWN
[/(\{\{usd)(\}\}) *([\d\.\,]+)/gi, '$1|$3$2'], // [[Template:USD]]
// Taken from [[User:Sam Sailor/Scripts/autoFormatterSettings.js]]
// Multi-templates
[/(\{\{Cite)(book|news|web)( |\||\}|\n)/ig, '$1 $2$3'], // #CUSTOM
// Sports Multi-templates
[/\n(\{\{)(Baseball|Basketball|Football) *(stats)/ig, '\n* $1$2 $3'], // start line with asterisk #CUSTOM
[/(\{\{)(Baseball|Basketball|Football) *(stats)( |\||\}|\n)/ig, '$1$2 $3$4'], // #CUSTOM
[/\{\{Infobox (baseball|basketball|CFL|NFL|volleyball) player( |\||\}|\n)/ig, '{{Infobox $1 biography$2'], // #CUSTOM
[/\{\{Infobox gridiron football person( |\||\}|\n)/ig, '{{Infobox CFL biography$1'],
[/\{\{Infobox MLB player( |\||\}|\n)/ig, '{{Infobox baseball biography$1'],
[/\{\{Infobox NBA biography( |\||\}|\n)/ig, '{{Infobox basketball biography$1'],
// templates renamed
[/\{\{Infobox Airliner accident( |\||\}|\n)/ig, '{{Infobox aircraft occurrence$1'],
[/\{\{airport-dest-list( |\||\}|\n)/ig, '{{airport destination list$1'],
[/\{\{b-da( |\||\}|\n)/ig, '{{birth-date and age$1'],
[/\{\{bda( |\||\}|\n)/ig, '{{birth date and age$1'],
[/\{\{Current Iowa Representatives( |\||\}|\n)/ig, '{{Iowa House of Representatives$1'],
[/\{\{d-da( |\||\}|\n)/ig, '{{death-date and age$1'],
[/\{\{dda( |\||\}|\n)/ig, '{{death date and age$1'],
[/\{\{eliteprospects( |\||\}|\n)/ig, '{{EliteProspects$1'],
[/\{\{empty category( |\||\}|\n)/ig, '{{Possibly empty category$1'],
[/\{\{FIBT profile( |\||\}|\n)/ig, '{{IBSF$1'],
[/\{\{fig( |\||\}|\n)/ig, '{{FIG$1'],
[/\{\{(?:Find a grave|Findagrave|FAG|Fag|Fg)( |\||\}|\n)/ig, '{{Find a Grave$1'], // #CUSTOM
[/\{\{footballbox collapsible( |\||\}|\n)/ig, '{{football box collapsible$1'],
[/\{\{hockeydb( |\||\}|\n)/ig, '{{HockeyDB$1'],
[/\{\{Ice *Hockey *Stats( |\||\}|\n)/ig, '{{Ice hockey stats$1'], // #CUSTOM
[/\{\{India Districts( |\||\}|\n)/ig, '{{Infobox India district$1'],
[/\{\{Infobox art( |\||\}|\n)/ig, '{{Infobox artwork$1'],
[/\{\{Infobox athlete( |\||\}|\n)/ig, '{{Infobox sportsperson$1'],
[/\{\{Infobox Given Name(?: *2| *Revised)?( |\||\}|\n)/ig, '{{Infobox given name$1'], // #CUSTOM
[/\{\{Infobox Mandir( |\||\}|\n)/ig, '{{Infobox Hindu temple$1'],
[/\{\{Infobox Military (?:Cemetery|Memorial)( |\||\}|\n)/ig, '{{Infobox military memorial$1'], // #CUSTOM
[/\{\{Infobox Military Structure( |\||\}|\n)/ig, '{{Infobox military installation$1'],
[/\{\{Infobox mountain range( |\||\}|\n)/ig, '{{Infobox mountain $1'],
[/\{\{Infobox painting( |\||\}|\n)/ig, '{{Infobox artwork$1'],
[/\{\{Infobox surname( |\||\}|\n)/ig, '{{Infobox name$1'],
[/\{\{ISAF sailor( |\||\}|\n)/ig, '{{World Sailing profile$1'],
[/\{\{IBSF profile( |\||\}|\n)/ig, '{{IBSF$1'],
[/\{\{ISSF name( |\||\}|\n)/ig, '{{ISSF$1'],
[/\{\{ISU name( |\||\}|\n)/ig, '{{ISU figure skater$1'],
[/\{\{ITF (male|female) profile( |\||\}|\n)/ig, '{{ITF profile$2'],
[/\{\{(L)ang(?:uage)?[ \-]?icon( |\||\}|\n)/ig, '{{$1ink language$2'],
// [/\{\{(P)ortalbar( |\||\}|\n)/ig, '{{Portal bar$1'], // #OFF
[/\{\{Robert Awards chron( |\||\}|\n)/ig, '{{Robert Awards$1'],
[/\{\{spanish name( 2)?( |\||\}|\n)/ig, '{{Spanish name$2'],
[/\{\{url( |\||\}|\n)/ig, '{{URL$1'],
[/\{\{USA *Gymnastics *Name( |\||\}|\n)/ig, '{{USA Gymnastics$1'], // #CUSTOM
[/\{\{wta( |\||\}|\n)/ig, '{{WTA$1'],
[/\{\{worldcurling( |\||\}|\n)/ig, '{{World Curling Federation$1'],
[/\{\{worldcurl(?:ingtour)?( |\||\}|\n)/ig, '{{World Curling Tour$1'], // #CUSTOM
// convert {{USCSub}} with only two parameters to {{USC}}
[/\|etseq\}\}/g, '}} [[et seq.]]'], // fix invalid subsection link
[/\{\{USCSub\|([0-9]{1,2})\|([0-9a-z]{1,7})\}\}/ig, '{{USC|$1|$2}}'],
// renamed URLs
[ 'books.google.co.uk', 'books.google.com'],
[ 'books.google.dk', 'books.google.com'],
[ 'http://www.collegefootball.org/famersearch.php?id=', 'http://www.footballfoundation.org/Programs/CollegeFootballHallofFame/SearchDetail.aspx?id='],
[ 'http://www.collegefootball.org/famer_selected.php?id=', 'http://www.footballfoundation.org/Programs/CollegeFootballHallofFame/SearchDetail.aspx?id='],
[ 'http://www.profootballhof.com/hof/member.jsp?PLAYER_ID=', 'http://www.profootballhof.com/hof/member.aspx?PlayerId='],
[ 'http://www.profootballhof.com/hof/member.jsp?player_id=', 'http://www.profootballhof.com/hof/member.aspx?PlayerId='],
[ 'http://www.profootballhof.com/hof/member.aspx?PLAYER_ID=', 'http://www.profootballhof.com/hof/member.aspx?PlayerId='],
[ 'http://www.profootballhof.com/hof/member.aspx?player_id=', 'http://www.profootballhof.com/hof/member.aspx?PlayerId='],
// convert bare ISSF URL to ext link
[
/http:\/\/www\.issf-sports\.org\/athletes\/athlete\.ashx\?personissfid=([A-Z]{6}[0-9]{10})/g,
'\n==External links==\n* {{ISSF name | $1 }}'
],
// athletics related
[/\{\{iaaf name/ig, '{{IAAF'],
[/\| *?letter *?= *?[A-Za-z] *?([\|\}])/g, '$1'], // remove deprecated letter parameter and value from Template:IAAF
[ '{{AchievementTable', '{{AchievementTable|width=auto'],
[ '{{AchievementTable|width=auto|width=auto', '{{AchievementTable|width=auto'],
[
/\{\{MedalSport ?\| ?(Men's|Women's) \[\[Athletics \(sport\)\|Athletics\]\] ?\}\}/ig,
'{{MedalSport | $1 [[athletics (sport)|athletics]]}}'
],
[
/\{\{MedalSport ?\| ?(Men's|Women's) Athletics ?\}\}/ig,
'{{MedalSport | $1 [[athletics (sport)|athletics]]}}'
],
// rowing related
[
/\{\{MedalCountry ?\| ?(\{\{[A-Z]{3}\}\}) ?\}\}\n\{\{MedalSport ?\| ?\[\[Rowing at the Summer Olympics\|(Men's|Women's) Rowing\]\] ?\}\}\n\{\{MedalCompetition ?\| ?\[\[Olympic Games\]\] ?\}\}/ig,
'{{MedalSport | $2 [[rowing (sport)|rowing]] }}\n{{MedalCountry | $1 }}\n{{MedalCompetition | [[Rowing at the Summer Olympics|Olympic Games]]}}'
],
[
/\{\{MedalTop\}\}\n\{\{MedalSport ?\| ?(Men's|Women's) \[\[Rowing at the Summer Olympics\|Rowing\]\] ?\}\}/ig,
'{{MedalTableTop}}\n{{MedalSport | $1 [[rowing (sport)|rowing]] }}\n{{MedalCountry | {{unk}} }}\n{{MedalCompetition | [[Rowing at the Summer Olympics|Olympic Games]]}}'
],
[
/\{\{MedalSport ?\| ?(Men's|Women's) \[\[Rowing \(sport\)\|Rowing\]\] ?\}\}/ig,
'{{MedalSport | $1 [[rowing (sport)|rowing]]}}'
],
[
/\{\{(MedalSport|MedalCompetition|Medal\|Comp) ?\| ?\[\[Rowing World Championships/g,
'{{MedalCompetition | [[World Rowing Championships'
],
[
/\{\{(MedalSport|MedalCompetition|Medal\|Comp) ?\| ?\[\[(Olympic Games|World Rowing Championships|World Rowing Championships\|World Championships|European Rowing Championships|European Rowing Championships\|European Championships)\]\] ?\}\}/g,
'{{MedalCompetition | [[$2]]}}'
],
[/\[\[Rowing \(sport\)\|row/ig, '[[rowing (sport)|row'],
[/\[\[Sport rowing\|/ig, '[[rowing (sport)|'],
[/\[\[Coxswain\|cox/ig, '[[coxswain (rowing)|cox'],
// general medal template formatting
[/\{\{Medal\|?Olympics?\}\}/ig, '{{MedalCompetition | [[Olympic Games]] }}'],
[/\{\{Medal\|?WorldChampionships\}\}/ig, '{{MedalCompetition | World Championships }}'],
[/\{\{Medal\|?EuropeanChampionships\}\}/ig, '{{MedalCompetition | European Championships }}'],
[/\{\{(Medal\|?(?:Sport|Country|Competition|Comp|Gold|Silver|Bronze|Disqualified)) *\| *(.*?) ?\}\}\n/g, '{{$1 | $2 }}\n'], // spacing for readability when editing
// [/\]\] *\| *(?![\|])/g, ']] | '], // spacing before 2nd parameter - replace "]]|" with "]] | " unless followed by another | #OFF
[
/\n(\}\}|\{\{MedalBottom\}\})\n?'''/g,
"\n$1\n\n'''"
], // blank line between end of infobox or medal templates and first sentence
[
/\[\[([0-9]{4}) (Summer Olympics|Winter Olympics|World Rowing Championships)\|(.+?(?= [0-9])) ([0-9]{4})\]\]/g,
'[[$1 $2|$4 $3]]'
], // from [[1984 Summer Olympics|Los Angeles 1984]] to [[1984 Summer Olympics|1984 Los Angeles]]
[/\{\{MedalTop\|?\}\}/ig, '{{MedalTableTop | name = no | medals =\n{{MedalCompetition | [[Olympic Games]] }}'],
[/\{\{MedalTableTop\|?\}\}/ig, '{{MedalTableTop | medals ='],
[/\{\{MedalBottom\}\}/ig, '}}'],
[
/(\{\{Medal\|?Country[^\n]*\n)(\{\{Medal\|?Sport[^\n]*\n)/i,
'$2$1'
], // place MedalCountry after MedalSport
// sports-ref citations
[
/(>|\* *)\[?(http:\/\/www\.sports\-reference\.com\/olympics\/athletes\/[a-z]{2}\/[a-z1-9\-]{3,30}\.html) *[A-Za-z \]\-\.]*(\n|<\/ref>)/ig,
'$1{{cite Sports-Reference |url=$2}}$3'
],
[
/\{\{cite web *\| *title *= *Olympics *\| *url *= *(http:\/\/www\.sports\-reference\.com\/olympics\/athletes\/[a-z]{2}\/[a-z1-9\-]{3,30}\.html) *\| *publisher *= *sports\-reference *\| *accessdate *= */ig,
'{{cite Sports-Reference |url=$1 |accessdate='
],
[
/\{\{cite web *\| *url *= *(http:\/\/www\.sports\-reference\.com\/olympics\/athletes\/[a-z]{2}\/[a-z1-9\-]{3,30}\.html) *\| *title *= *([^\|]*)\| *publisher *= *Sports\-reference\.com *\| *accessdate *= */ig,
'{{cite Sports-Reference |url=$1 |title=$2|accessdate='
],
[
/\{\{Sports-reference *\| *id *= */ig,
'{{cite Sports-Reference |url=http://www.sports-reference.com/olympics/athletes/'
],
[
/\{\{Sports-reference *\|/ig,
'{{cite Sports-Reference |url=http://www.sports-reference.com/olympics/athletes/'
],
[
/\{\{Sports-reference\}\}/ig,
'{{cite Sports-Reference}}'
],
[
/\{\{cite Sports\-Reference \|url=([^0-9]*[0-9])([^\.])/ig,
'{{cite Sports-Reference |url=$1.html$2'
], // add .html if missing
[
/\{\{ *cite *sports\-reference *\| *1 *= *([^\|]*)\| *2 *= *([^\|]*)\| *3 *= *([^\}]*)}}/ig,
'{{cite Sports-Reference|title=$1|url=$2|accessdate=$3}}'
],
// replace link with template
[
/\[http:\/\/www\.databaseolympics\.com\/players\/playerpage\.htm\?ilkid=([A-Z0-9%]{5,30}) (Olympics profile|profile|databaseOlympics)\]/ig,
'{{databaseOlympics |id=$1}}'
],
// boxing related
[
/(==Professional boxing record==[\n\r]*?)\{\{[Ss]-start}}([\n\r]*?\|)/g,
'$1{| class="wikitable" style="text-align:center;"$2'
],
[
/\[(http:\/\/boxrec\.com\/list_bouts\.php\?human_id=[0-9]{1,6}&cat=boxer)]/g,
"<ref>[$1 Boxing record for {{PAGENAMEBASE}}]. ''[[BoxRec.com]]''.</ref>"
],
[ '{{boxrec|id=', '{{BoxRec|'],
[ '|align="center" colspan=8', '| colspan="8"'],
[ '| align="center" style="border-style:', '| style="border-style:'],
// cricket external links
[
/\{\{ *Cricinfo/ig,
'{{ESPNcricinfo'
],
[
/\{\{ *Cricketarchive/ig,
'{{CricketArchive'
],
[
/\{\{ *Yahoo!_Cricket/ig,
'{{Yahoo! Cricket'
],
[
/\{\{ESPNcricinfo *\| *ref *= *[a-z]*\/content\/player\/([0-9]*)\.html/ig,
'{{ESPNcricinfo |id= $1'
],
[
/\{\{CricketArchive *\| *ref *= *Archive\/Players\/[0-9]*\/[0-9]*\/([0-9]*)\.html/ig,
'{{CricketArchive |id= $1'
],
[
/\{\{Yahoo! Cricket *\| *ref *= *player\-profile\/([a-z0-9_\-]*)/ig,
'{{Yahoo! Cricket |id= $1'
],
// End cleaning
[/([^\s\}]) (\})/g, '$1$2'], // Remove ending space from templates #OWN
];
for (const replacement of replacements) {
text = text.replace(...replacement);
}
return text;
}
function process_summary(summary) {
const ac_summary = 'formatted templates';
if (summary) {
if (/^\/\*.*?\*\/ $/.test(summary)) { // auto summary
return summary + ac_summary;
} else {
return summary + '; ' + ac_summary;
}
} else {
return ac_summary;
}
}
function main() {
let text_box = $('#wpTextbox1');
let old_text = text_box.textSelection('getContents');
let new_text = clean_wikitext(old_text);
if (old_text != new_text) {
text_box.textSelection('setContents', new_text);
if (mw.util.getParamValue('section') != 'new') { // not section title instead of summary
let summary_box = $('#wpSummary');
summary_box.val(
process_summary(summary_box.val())
)}
mw.notify(
'Done',
{
tag: 'tol-autocleanup-tl',
title: 'AC Templates',
type: 'success'
}
);
}
}
function add_button() {
try {
if ($('editform').length) {
mw.loader.using(['oojs-ui', 'oojs-ui-widgets'], function() {
let button = new OO.ui.ButtonWidget({label: 'AC Templates'});
button.on('click', main);
$('.editButtons').append(button.$element);
});
}
} catch(error) {
alert(error);
}
}
//$(add_button);
if (mw.config.get('wgPageContentModel') === 'wikitext' && mw.config.get('wgAction') === 'edit' && mw.util.getParamValue( "AC/TLdisable" ) != "1" ) {
mw.util.addPortletLink( "p-cactions", window.location.href + '&AC/TLdisable=1', "Disable AC Templates", "pt-disableAC/TL" );
$(main);
}
});
// </syntaxhighlight>