Jump to content

User:Qwerfjkl/scripts/autoCleanup.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.
/*
Fork of [[User:Tol/AutoCleanup.js]]

Licensing:
* Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0)
*: https://creativecommons.org/licenses/by-sa/3.0/
* Creative Commons Attribution-ShareAlike 4.0 (CC BY-SA 4.0)
*: https://creativecommons.org/licenses/by-sa/4.0/
* GNU Lesser General Public License (GNU LGPL)
*: https://www.gnu.org/copyleft/lgpl.html
* GNU Free Documentation License (GNU FDL)
*: http://www.gnu.org/licenses/fdl-1.3.html

Attribution:
* [[User:Tol]] [https://en.wikipedia.org/wiki/User:Tol]

*/

// <syntaxhighlight lang="js">

$(function() {
	
	function clean_wikitext(text) {
		const replacements = [
			[/'|'/g, '\''], // Replace curly single quotes with straight ones
			[/"|"/g, '"'], // Replace curly double quotes with straight ones
			[/(\n[^\|\}].+) +\n/g, '$1\n'], // Remove lines' trailing spaces (that don't start with "|" or "}"
			[/^(=+) *(.*?) *\1$/gm, '$1 $2 $1'], // Standardise spacing inside headers
			[/\n+^((=+) .*? \2)$/gm, '\n\n$1'], // Standardise spacing before headers
			[/^((=+) .*? \2)$\n+/gm, '$1\n\n'], // Standardise spacing after headers
			[/\n{3,}/g, '\n\n'], // Replace three or more newlines with two newlines
            [/((?:<ref[^>]*?>[^>]*?<\/ref>){1,})\. /gm, '.$1 '], // Taken from [[User:Novem Linguae/DraftCleaner.js]]
		    [/((?:<ref[^>]*?>[^>]*?<\/ref>){1,})\.\n/gm, ".$1\n"], // Fix ref punctuation
            [/(?<!\[)\[https?:\/\/en\.wikipedia\.org\/wiki\/([^ \]]*)(?: ([^\]]*))?\]/gs, function(match, p1, p2) {
			p1 = decodeURIComponent(p1);
			p1 = p1.replace(/_/g, ' ');
            if (p2 == undefined) {
			return `[[${p1}]]`;
            } else {
            return `[[${p1}|${p2}]]`;
            } }],
            // Taken from [[User:Ingenuity/cleaner.js]]; clean up references
		    [/ ?(<ref(?: name ?= ?"[^<>]{0,30}")?>(?:(?!<\/ref>).)+?<\/ref>)/gs, "$1"],
		    [/ ?(<ref name ?= ?"[^<>]{0,30}" ?\/>)/gs, "$1"],
		    [/(<ref(?: name ?= ?"[^<>]{0,30}")?>(?:(?!<\/ref>).)+?<\/ref>)[\n ]?([,\.\?\!\;])/gs, "$2$1"],
		    [/(<ref name ?= ?"[^<>]{0,30}" ?\/>)[\n ]?([,\.\?\!\;])/g, "$2$1"], 
            // Taken from pywikibot safe-syntax replaces, from the fixes.py script
            [/\[\[(?<url>https?:\/\/[^\]]+?)\]\]/g, '[$1]'], // external link in double brackets
            [/\[\[(?<url>https?:\/\/.+?)\]/g, '[$1]'], // external link starting with double bracket
            [/\[(?<url>https?:\/\/[^\|\] \r\n]+?) +\| *(?<label>[^\|\]]+?)\]/, '[$1 $2]'], // external link and description separated by a dash, with whitespace in front of the dash, so that it is clear that the dash is not a legitimate part of the URL.
            [/\[(?<url>https?:\/\/[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?<label>[^\|\]]+?)\]/, '[$1 $3]'], // dash in external link, where the correct end of the URL can be detected from the file extension. It is very unlikely that this will cause mistakes.
            [/\[\[([^\|\]]+)\|\1\]\]/, '[[$1]]'], // link equal to linktext
  // Taken from [[User:Sam Sailor/Scripts/autoFormatterSettings.js]]
  // [ 'use <ref>', 'use ref' ],
  [ / =\n/g, ' = \n' ],   // add trailing space after = for empty infobox parameters
  [ /\{\{ /g, '{{' ],   // remove space after {{
  [ /\|tau\]\]/g, '|τ]]' ],   // fix unwanted change in category sorting
  [ /b\. *(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/g, 'born $1' ],   // replace "b. Jan" with "born Jan"
  [ /b\. *([0-9]{1,2}) *(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/g, 'born $1 $2' ],   // replace "b. 31 Jan" with "born 31 Jan"
  [ /(born|b\.) \{\{ *birth ?date *\|/ig, 'born {{SUBST:User:Zyxw/bd|' ],   // fix duplicated birth date templates
  [ /(\| *height *= *\{\{convert\| *[1-2]),([0-9]{1,2} *\| *m)/ig, '$1.$2' ],   // fix height with comma in {{convert}}
  [ /\n\{\{Baseballstats/ig,   '\n* {{Baseball stats' ],   // start line with asterisk
  [ /\n\{\{Basketballstats/ig, '\n* {{Basketball stats' ], // start line with asterisk
  [ /\n\{\{Footballstats/ig,   '\n* {{Football stats' ],   // start line with asterisk
  [ /\n(== *)External Links( *==)/, '\n$1External links$2' ],
  [ /\n(== *)External Link( *==)/, '\n$1External links$2' ],
  [ /\n(== *)External links( *==)/, '\n$1External links$2' ],
  [ /\n(== *)Further Reading( *==)/, '\n$1Further reading$2' ],
  [ /\n(== *)Published Papers( *==)/, '\n$1Published papers$2' ],
  [ /\n(== *)See Also( *==)/, '\n$1See also$2' ],
  [ /\n(== *)Selected Works( *==)/, '\n$1Selected works$2' ],
  [ /([0-9]{4})-present/ig, '$1–present' ],

// templates renamed
  [ /\{\{Infobox Airliner accident( |\||\}|\n)/ig, '{{Infobox aircraft occurrence$1' ],
  [ /\{\{airport-dest-list( |\||\}|\n)/ig, '{{airport destination list$1' ],
  [ /\{\{Baseballstats( |\||\}|\n)/ig, '{{Baseball stats$1' ],
  [ /\{\{Basketballstats( |\||\}|\n)/ig, '{{Basketball stats$1' ],
  [ /\{\{b-da( |\||\}|\n)/ig, '{{birth-date and age$1' ],
  [ /\{\{bda( |\||\}|\n)/ig, '{{birth date and age$1' ],
  [ /\{\{citebook( |\||\}|\n)/ig, '{{cite book$1' ],
  [ /\{\{citenews( |\||\}|\n)/ig, '{{cite news$1' ],
  [ /\{\{citeweb( |\||\}|\n)/ig, '{{cite web$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' ],
  [ /\{\{Find a grave( |\||\}|\n)/ig, '{{Find a Grave$1' ],
  [ /\{\{Findagrave( |\||\}|\n)/ig, '{{Find a Grave$1' ],
  [ /\{\{FAG( |\||\}|\n)/ig, '{{Find a Grave$1' ],
  [ /\{\{Fag( |\||\}|\n)/ig, '{{Find a Grave$1' ],
  [ /\{\{Fg( |\||\}|\n)/ig, '{{Find a Grave$1' ],
  [ /\{\{FIBT profile( |\||\}|\n)/ig, '{{IBSF$1' ],
  [ /\{\{fig( |\||\}|\n)/ig, '{{FIG$1' ],
  [ /\{\{footballbox collapsible( |\||\}|\n)/ig, '{{football box collapsible$1' ],
  [ /\{\{Footballstats( |\||\}|\n)/ig, '{{Football stats$1' ],
  [ /\{\{hockeydb( |\||\}|\n)/ig, '{{HockeyDB$1' ],
  [ /\{\{Icehockeystats( |\||\}|\n)/ig, '{{Ice hockey stats$1' ],
  [ /\{\{Ice Hockey Stats( |\||\}|\n)/ig, '{{Ice hockey stats$1' ],
  [ /\{\{Ice Hockey Stats( |\||\}|\n)/ig, '{{Ice hockey stats$1' ],
  [ /\{\{India Districts( |\||\}|\n)/ig, '{{Infobox India district$1' ],
  [ /\{\{Infobox art( |\||\}|\n)/ig, '{{Infobox artwork$1' ],
  [ /\{\{Infobox athlete( |\||\}|\n)/ig, '{{Infobox sportsperson$1' ],
  [ /\{\{Infobox baseball player( |\||\}|\n)/ig, '{{Infobox baseball biography$1' ],
  [ /\{\{Infobox basketball player( |\||\}|\n)/ig, '{{Infobox basketball biography$1' ],
  [ /\{\{Infobox CFL player( |\||\}|\n)/ig, '{{Infobox CFL biography$1' ],
  [ /\{\{Infobox Given Name( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox Given name( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox Given Name 2( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox Given name 2( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox Given Name Revised( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox given name2( |\||\}|\n)/ig, '{{Infobox given name$1' ],
  [ /\{\{Infobox given name( |\||\}|\n)/ig, '{{Infobox name$1' ],
  [ /\{\{Infobox gridiron football person( |\||\}|\n)/ig, '{{Infobox CFL biography$1' ],
  [ /\{\{Infobox Mandir( |\||\}|\n)/ig, '{{Infobox Hindu temple$1' ],
  [ /\{\{Infobox Military Cemetery( |\||\}|\n)/ig, '{{Infobox military memorial$1' ],
  [ /\{\{Infobox Military Memorial( |\||\}|\n)/ig, '{{Infobox military memorial$1' ],
  [ /\{\{Infobox Military Structure( |\||\}|\n)/ig, '{{Infobox military installation$1' ],
  [ /\{\{Infobox MLB player( |\||\}|\n)/ig, '{{Infobox baseball biography$1' ],
  [ /\{\{Infobox mountain range( |\||\}|\n)/ig, '{{Infobox mountain $1' ],
  [ /\{\{Infobox NBA biography( |\||\}|\n)/ig, '{{Infobox basketball biography$1' ],
  [ /\{\{Infobox NFL player( |\||\}|\n)/ig, '{{Infobox NFL biography$1' ],
  [ /\{\{Infobox painting( |\||\}|\n)/ig, '{{Infobox artwork$1' ],
  [ /\{\{Infobox surname( |\||\}|\n)/ig, '{{Infobox name$1' ],
  [ /\{\{Infobox volleyball player( |\||\}|\n)/ig, '{{Infobox volleyball biography$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' ],
  [ /\{\{Robert Awards chron( |\||\}|\n)/ig, '{{Robert Awards$1' ],
  [ /\{\{spanish name( 2)?( |\||\}|\n)/ig, '{{Spanish name$2' ],
  [ /\{\{url( |\||\}|\n)/ig, '{{URL$1' ],
  [ /\{\{USAGymnastics name( |\||\}|\n)/ig, '{{USA Gymnastics$1' ],
  [ /\{\{USAGymnasticsName( |\||\}|\n)/ig, '{{USA Gymnastics$1' ],
  [ /\{\{wta( |\||\}|\n)/ig, '{{WTA$1' ],
  [ /\{\{worldcurling( |\||\}|\n)/ig, '{{World Curling Federation$1' ],
  [ /\{\{worldcurlingtour( |\||\}|\n)/ig, '{{World Curling Tour$1' ],
  [ /\{\{worldcurl( |\||\}|\n)/ig, '{{World Curling Tour$1' ],

// 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 |
  [
    /\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

// if no ref section
//  [ /\.\n\n== ?External links ?==/g, '.\n\n== References ==' ],

// 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:' ],
  [
    /(\| *)([0-9]{2})\/([0-9]{2})\/([0-9]{4})/g,
    '$1$4-$3-$2'
  ], // replace "| 31/12/2015" with "| 2015-12-31"
  [
    /(\| *)([0-9]{4})[–—−\/]([0-9]{2})[–—−\/]([0-9]{2})/g,
    '$1$2-$3-$4'
  ], // replace "| 2015–12–31" with "| 2015-12-31" (endash/emdash/minus/slash to hyphen)

// 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'
  ],

// infobox image cleanup
  [
    /(\| *image *= *)(Image:|File:)/g,
    '$1'
  ], // if "| image = File:name" is used, remove unneeded "File:"

// spaces after bullets
  [
    /(\n[*#]+) *([^\s*#:;R])/g,
    '$1 $2'
  ], // space after one or more * or # at start of line

// table spacing for readability when editing
  [
    /\n\|(?![\s\n\-\+\}\|])/g,
    "\n| "
  ], // space after | at start of line, if not followed by space newline - + } |
  [
    /\n(\!|\{\||\|\+|\|\-)(?![\s\n])/g,
    "\n$1 "
  ], // space after ! {| |+ |- at start of line, if not followed by space or newline
//  [
//    /(\|\||\!\!)(?![\s\n])/g,
//    "$1 "
//  ], // space after || or !! if not followed by space or newline
//  [
//    /([^\s\n])(\|\||\!\!)/g,
//    "$1 $2"
//  ], // space before || or !! if not preceded by space or newline
  [
    /(\{\{[a-z]*) \|\| /ig,
    "$1||"
  ], // fix templates changed from "{{abc||" to "{{abc || "
  [
    /align=("?)(left|right|center)("?) *\| */g,
    'align=$1$2$3 | '
  ], // spacing around | in table rows with align="right", etc.
  [
    /(background: *|bgcolor=)("?)(#?\w{1,6}|[a-z]{3,20})("?) *\| */g,
    '$1$2$3$4 | '
  ], // spacing around | in table rows with bgcolor=#F9E8C7, bgcolor="red", background: #F9E8C7, etc.
  [
    /scope=("?)(col|row)("?) *\| */g,
    'scope=$1$2$3 | '
  ], // spacing around | in table rows with scope="col", etc.
  [
    /span=("?)([1-9]{1,3})("?) *\| */g,
    'span=$1$2$3 | '
  ], // spacing around | in table rows with colspan="13", rowspan="2", etc.
  [
    /(width="?[0-9]{1,3}%?"?)\|/g,
    '$1 | '
  ], // spacing around | in table rows with width="100", width="5%", etc.
  [
    /; *"\| */g,
    ';" | '
  ], // spacing around | in table rows with ;"
  [
    /\|\-\n\|\}/g,
    '|}'
  ], // remove unneeded |- (<tr>) at end of table
  [
    /(\|\-\n)\n/g,
    '$1'
  ], // remove extra blank line after |- in table
  [
    /\n(\n\|\})/g,
    '$1'
  ], // remove extra blank line before |} in table
  [
    /(\n\! )\|/g,
    '$1'
  ], // remove unneeded | after !
  [
    /\n\|\-(\n\|\-)/g,
    '$1'
  ], // remove duplicate |-
		];
		for (const replacement of replacements) {
			text = text.replace(...replacement);
		}
		return text;
	}
	
	function process_summary(summary) {
		const ac_summary = 'cleanup (via [[User:Qwerfjkl/scripts/autoCleanup|script]])';
		if (summary) {
			if (/^\/\*.*?\*\/ $/.test(summary)) { // auto summary
				return summary + ac_summary;
			} else {
				return summary + '; ' + ac_summary;
			}
		} else {
			return ac_summary;
		}
	}
	
	function main() {
		mw.notify(
			'Processing...',
			{
				tag: 'tol-autocleanup',
				title: 'AutoCleanup',
				type: 'info',
				autoHide: true,
				autoHideSeconds: 2
			}
		);
		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',
					title: 'AutoCleanup',
					type: 'success'
				}
			);
		} else {
			mw.notify(
				'No changes made',
				{
					tag: 'tol-autocleanup',
					title: 'AutoCleanup',
					type: 'info'
				}
			);
		}
	}
	
	function add_button() {
		try {
			if ($('editform').length) {
				mw.loader.using(['oojs-ui', 'oojs-ui-widgets'], function() {
					let button = new OO.ui.ButtonWidget({label: 'AutoCleanup'});
					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( "ACdisable" ) != "1" ) {
	    mw.util.addPortletLink( "p-cactions", window.location.href + '&ACdisable=1', "Disable AutoCleanup", "pt-disableAC" );
		$(main);
	}
});

// </syntaxhighlight>