Jump to content

User:ಮಲ್ನಾಡಾಚ್ ಕೊಂಕ್ಣೊ/center.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.
// <nowiki>
function center_queryString(p) {
    var re = RegExp('[&?]' + p + '=([^&]*)');
    var matches = re.exec(document.location);
    if (matches) {
        try { 
            return decodeURI(matches[1]);
        } catch (e) { }
    }
    return null;
}

//Add a 'center' tab
if(mw.config.get('wgArticleId') != 0 ) { 
    $( function centerEditButton() {
        mw.util.addPortletLink('p-cactions', 
                       mw.util.getUrl(null,{action:'edit',centeredit:true}),
                       'center',
                       'p-center',
                       'center');
    }
)}

if(mw.config.get('wgAction') == 'edit' && center_queryString('centeredit') == 'true') {
    $(function lint() {
        var myContent = document.getElementById('wpTextbox1').value;
		/*
   		//center caption in {{Wide image}}
		myContent = myContent.replace(/(\{\{\s*wide image\s*\|\s*[^}]*)<center>([^<]+)<\/center\>(\s*\}\})/gi,'$1align-cap=center|$2$3');
		
		//Table cell having an image
        myContent = myContent.replace(/(\|\s*)<center>(\s*\[\[(file|image):[^\|]+?\|)([^\]]+?\]\]\s*)<\/center>/gi,'$1$2center|$4');
        
        //Center tag wrapping table
		myContent = myContent.replace(/<center>\n*(\{\| *)\n/gi,'$1style="margin:1em auto;"\n');
		myContent = myContent.replace(/(\|\}) *\n*<\/center>/gi,'$1');

		//Table header with scope row or col having piped Wikilink
		myContent = myContent.replace(/(\! *scope=\"*[rc]o[wl]\"*) *\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'$1 style="text-align:center;" |$2$3');
		
		//Table header with scope row or col having unpiped Wikilink
		myContent = myContent.replace(/(\! *scope=\"*[rc]o[wl]\"*) *\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'$1 style="text-align:center;" |$2$3');
		
		//Table header with scope row or col having anything without | before closing center tag
		myContent = myContent.replace(/(\! *scope=\"*[rc]o[wl]\"*) *\|( *)<center>([^|]*)<\/center>/gi,'$1 style="text-align:center;" |$2$3');
		
		//Table cell with rowspan and colspan having piped Wikilink
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"*( *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'$1"$2"$3"$4" style="text-align:center;" |$5$6');
		
		//Table cell with rowspan and colspan having unpiped Wikilink
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"*( *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'$1"$2"$3"$4" style="text-align:center;" |$5$6');
		
		//Table cell with rowspan and colspan having anything without | before closing center tag
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"*( *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>([^|]*)<\/center>/gi,'$1"$2"$3"$4" style="text-align:center;" |$5$6');
		
		//Table cell with rowspan or colspan having piped Wikilink
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'$1"$2" style="text-align:center;" |$3$4');

		//Table cell with rowspan or colspan having unpiped Wikilink
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'$1"$2" style="text-align:center;" |$3$4');

		//Table cell with rowspan or colspan having anything without | before closing center tag
		myContent = myContent.replace(/([\!\|] *[rc]o[wl]span=)\"*(\d+)\"* *\|( *)<center>([^|]*)<\/center>/gi,'$1"$2" style="text-align:center;" |$3$4');
		
		//Table cell with bgcolor attribute hex without # having piped Wikilink
        myContent = myContent.replace(/bgcolor=\"* *([a-f\d][a-f\d][a-f\d]|[a-f\d][a-f\d][a-f\d][a-f\d][a-f\d][a-f\d]) *\"* *\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'style="background:#$1; text-align:center;" |$2$3');
		
        //Table cell with bgcolor attribute having piped Wikilink
        myContent = myContent.replace(/bgcolor=\"*(\#*[\d\w]+)\"* *\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'style="background:$1; text-align:center;" |$2$3');
        
        //Table cell with bgcolor attribute hex without # having unpiped Wikilink
        myContent = myContent.replace(/bgcolor=\"* *([a-f\d][a-f\d][a-f\d]|[a-f\d][a-f\d][a-f\d][a-f\d][a-f\d][a-f\d]) *\"* *\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'style="background:#$1; text-align:center;" |$2$3');
        
        //Table cell with bgcolor attribute having unpiped Wikilink
        myContent = myContent.replace(/bgcolor=\"*(\#*[\d\w]+)\"* *\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'style="background:$1; text-align:center;" |$2$3');
        
        //Table cell with bgcolor attribute hex without # having anything without | before closing center tag
        myContent = myContent.replace(/bgcolor=\"* *([a-f\d][a-f\d][a-f\d]|[a-f\d][a-f\d][a-f\d][a-f\d][a-f\d][a-f\d]) *\"* *\|( *)<center>([^|]*)<\/center>/gi,'style="background:#$1; text-align:center;" |$2$3');
        
        //Table cell with bgcolor attribute having anything without | before closing center tag
        myContent = myContent.replace(/bgcolor=\"*(\#*[\d\w]+)\"* *\|( *)<center>([^|]*)<\/center>/gi,'style="background:$1; text-align:center;" |$2$3');
        
        //Table cell with style having piped Wikilink
        myContent = myContent.replace(/(style=\")([^\"]+\" *\| *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'$1text-align:center;$2$3');

        //Table cell with style having unpiped Wikilink
        myContent = myContent.replace(/(style=\")([^\"]+\" *\| *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'$1text-align:center;$2$3');
        
        //Table cell with style having anything without | before closing center tag
        myContent = myContent.replace(/(style=\")([^\"]+\" *\| *)<center>([^|]*)<\/center>/gi,'$1text-align:center;$2$3');		

        //Table cell having single self-closed ref tag
        myContent = myContent.replace(/\|( *)<center>( *<ref name\=\"*[^/]+\"* *\/> *)<\/center>/gi, '|style="text-align:center;"|$1$2');
        
        //Table cell having ref tag
        myContent = myContent.replace(/\|( *)<center> *(<ref)/gi,'|style="text-align:center;"|$1$2');
        myContent = myContent.replace(/(<\/ref>) *<\/center>/gi,'$1');
        
        //Table cell having specific templates with parameters
        myContent = myContent.replace(/\|( *)<center>( *\{\{(flagicon|convert|leagueicon|fb|sort|nowrap|nbay)\|[^}]+\}\} *)<\/center>/gi,'|style="text-align:center;"|$1$2');

        //Table cell having piped wikilink
        myContent = myContent.replace(/\|( *)<center>( *\'*\[\[[^|]+\|[^\]]+\]\]\'* *)<\/center>/gi,'|style="text-align:center;"|$1$2');
        
        //Table cell having unpiped wikilink
        myContent = myContent.replace(/\|( *)<center>( *\'*\[\[[^\]]+\]\]\'* *)<\/center>/gi,'|style="text-align:center;"|$1$2');

        //Table cell having anything without | before closing center tag
        myContent = myContent.replace(/\|( *)<center>([^|]*)<\/center>/gi,'|style="text-align:center;"|$1$2');
        */
        
        //center table using margin 1em auto. Be careful about one center tag used to center two or more tables
        myContent = myContent.replace(/< *center *> *\n*(\{\| *)\n/gi, '$1style="margin:1em auto;"\n');
    	myContent = myContent.replace(/< *center *> *\n*(\{\|.*style\=\")/gi, '$1margin:1em auto; ');
    	myContent = myContent.replace(/< *center *> *\n*(\{\| *class\=\"[^"]+?\" *)\n*/gi, '$1 style="margin:1em auto;"\n');
    	myContent = myContent.replace(/< *center *> *\n*(\{\| *class\=) *(wikitable) *\n*/gi, '$1"$2" style="margin:1em auto;"\n');
    	myContent = myContent.replace(/< *center *> *\n*(\{\|[^\n]*)\n/gi, '$1 style="margin:1em auto;"\n');
    	myContent = myContent.replace(/(\|\}) *\n*<\/ *center *>/gi, '$1');
    	
        //replace center tags with {{center}}
		myContent = myContent.replace(/<center>/gi,'{{center|');
		myContent = myContent.replace(/<\/center>/gi,'}}');        
		
		/*
		//replace center tags with <div class="center">
		myContent = myContent.replace(/<center>/gi,'<div class="center">');
		myContent = myContent.replace(/<\/center>/gi,'<\/div>');
		*/

if(document.getElementById('wpTextbox1').value != myContent) {
                  document.getElementById('wpTextbox1').value=myContent;
                  document.getElementById('wpSummary').value='Replaced [[WP:HTML5|obsolete center tags]] and reduced [[WP:LINT|Lint errors]]';
                  document.getElementById('wpMinoredit').checked = true;
                  document.forms.editform.wpDiff.click();
              }
    }
)}
// </nowiki>