مدیاویکی:Gadget-Extra-Editbuttons-autoed.js

از ویکی‌گفتاورد

نکته: پس از انتشار ممکن است برای دیدن تغییرات نیاز باشد که حافظهٔ نهانی مرورگر خود را پاک کنید.

  • فایرفاکس / سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-F5 یا Ctrl-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-R)
  • گوگل کروم: کلیدهای Ctrl+Shift+R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-Shift-R)
  • اینترنت اکسپلورر/ Edge: کلید Ctrl را نگه‌دارید و روی دکمهٔ Refresh کلیک کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید
  • اپرا: Ctrl-F5 را بفشارید.
// <nowiki> // DO NOT REMOVE THIS LINE EVER
//Selected from [[:en:WP:AutoEd]] scripts
window.autoEd = {
	//---------------------isbn.js--------------------------------------
	autoEdISBN: function(str) { //MAIN FUNCTION describes list of fixes
	
	        //Allows WikiMagic to work with ISBNs
	        str = str.replace(/ISBN-10:|ISBN-13:|ISBN-10|ISBN-13|ISBN:/gi, 'ISBN');
	
	        return str;
	    },
	
	//---------------------whitespace.js--------------------------------
	autoEdWhitespace: function(str) { //MAIN FUNCTION describes list of fixes
	
	        str = str.replace(/\t/g, " ");
	
	        str = str.replace(/^ ? ? \n/gm, "\n");
	        str = str.replace(/(\n\n)\n+/g, "$1");
	        str = str.replace(/== ? ?\n\n==/g, "==\n==");
	        str = str.replace(/\n\n(\* ?\[?http)/g, "\n$1");
	
	        str = str.replace(/^ ? ? \n/gm, "\n");
	        str = str.replace(/\n\n\*/g, "\n*");
	        //  str = str.replace(/[ \t][ \t]+/g, " ");
	        str = str.replace(/([=\n]\n)\n+/g, "$1");
	        str = str.replace(/ \n/g, "\n");
	
	        //* bullet points
	        str = str.replace(/^([\*#]+:*) /gm, "$1");
	        str = str.replace(/^([\*#]+:*)/gm, "$1 ");
	
	        //==Headings==
	        str = str.replace(/^(={1,4} )[ ]*([^= ][^=]*[^= ])[ ]*( ={1,4})$/gm, "$1$2$3");
	        str = str.replace(/^(={1,4})([^= ][^=]*[^= ])[ ]+(={1,4})$/gm, "$1$2$3");
	        str = str.replace(/^(={1,4})[ ]+([^= ][^=]*[^= ])(={1,4})$/gm, "$1$2$3");
	
	        return str;
	    },
	
	//---------------------wikilinks.js-------------------------------
	// Credits: A modification of [[Wikipedia:WikiProject User scripts/Scripts/Formatter]]
	autoEdWikilinks: function(str) { //MAIN FUNCTION describes list of fixes
	
	    //Get the list of all wikilinks with underscores
	    var m = str.match(/\[\[[^\[\]]*_[^\[\]]*\]\]/g);
	    if (m) {
	        //For each wikilink in the list
	        for (var i = 0; i < m.length; i++) {
	            var x = m[i].toString() // Contains the entire wikilink
	                // Exclude URLs and leading underscores
	            if (!x.match(/^\[\[[\t ]*(?:http|ftp|https):/i) && !x.match(/^\[\[_[^\[\]]*\]\]/)) {
	                var x_arr = x.match(/^(\[\[[^\[\]\|]*)(\|?[^\[\]]*?\]\])$/);
	                var a = x_arr[1]; // Everything before the pipe (or everything minus ]])
	                var b = x_arr[2]; // Everything after the pipe (or ]])
	
	                // Replace underscores with spaces
	                a = a.replace(/_/g, ' ');
	                // Do the replacement
	                str = str.replace(x, a + b);
	            }
	        }
	    }
	
	    //Fix links which have no target
	    str = str.replace(/\[\[[\t ]*\|/gi, '[[');
	
	    //Leading and trailing space before the pipe inside wikilinks
	    str = str.replace(/(\[\[)[\t ]+([^\[\]\|]*?)(\|)/g, '$1$2$3');
	    str = str.replace(/(\[\[)([^\[\]\|]*?)[\t ]+(\|)/g, '$1$2$3');
	    //Leading space after the pipe (or in an unpiped) wikilink
	    str = str.replace(/^(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, '$1$2$3');
	    str = str.replace(/(\[\[[^\[\]\|]*?\||\[\[)[\t ]+([^\[\]\|\t ][^\[\]\|]*?)(\]\])/g, ' $1$2$3');
	    //Trailing space after the pipe (or in an unpiped) wikilink
	    str = str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|\t ][^\[\]\|]*?)[\t ]+(\]\])([^A-Za-z])/gi, '$1$2$3 $4');
	    str = str.replace(/(\[\[[^\[\]\|]*?\||\[\[)([^\[\]\|]*?)[\t ]+(\]\])$/gi, '$1$2$3');
	
	    //Get the list of all piped wikilinks
	    var m = str.match(/\[\[[^\[]*?\|[^\[]*?\]\]/g);
	    if (m) {
	        //For each piped wikilink in the list
	        for (var i = 0; i < m.length; i++) {
	            var n_arr = m[i].toString().match(/\[\[[ ]*([^\[]*?)\|[ ]*([^\[]*?)\]\]/);
	            var n = n_arr[0]; // Contains the entire piped link
	            var a = n_arr[1]; // Contains everything before pipe
	            var b = n_arr[2]; // Contains everything after pipe
	            var c = b.replace(/[\.,:; ]*$/); // Same as b, but without trailing punctuation
	            //Is the display name a leading substring of the wikilink?
	            if (b.indexOf(a) == 0 || b.indexOf(autoEd_first2lower(a)) == 0) {
	                //Create a simplified replacement string
	                var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w]*?)\]\]/i, "[[$2]]$3");
	                //And do the replacement
	                str = str.replace(n, k);
	            }
	            if (c.indexOf(a) == 0 || c.indexOf(autoEd_first2lower(a)) == 0) {
	                // Create a simplified replacement string
	                var k = n.replace(/\[\[([^\]\|]*?)\|(\1)([\w\.,:;]*?)\]\]/i, "[[$2]]$3");
	                // And do the replacement
	                str = str.replace(n, k);
	            }
	        }
	    }
	
	    //Push trailing characters into display string of piped wikilinks
	    str = str.replace(/\[\[([^\[\]\|]+)\|([^\[\]\|]+)\]\]([a-z]+)/g, "[[$1|$2$3]]");
	
	    //Removes links to current article
	    var p1 = wgPageName; // PAGENAME including underscores
	    var p2 = wgPageName.replace('_', ' '); // PAGENAME without underscores
	    var p3 = autoEd_first2lower(p1); // First character lowercase PAGENAME including underscores
	    var p4 = autoEd_first2lower(p2); // First character lowercase PAGENAME without underscores
	    // Standard wikilinks
	    str = str.replace(new RegExp('\\[\\[(' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\]\\]', 'g'), '$1');
	    // Piped wikilinks
	    str = str.replace(new RegExp('\\[\\[(?:' + p1 + '|' + p2 + '|' + p3 + '|' + p4 + ')\\|([^\\]\\|]*)\\]\\]', 'g'), '$1');
	
	    //Shorten interwiki links
	    str = str.replace(/\[\[WIKTIONARY:/gi, '[[wikt:');
	    str = str.replace(/\[\[WIKINEWS:/gi, '[[n:');
	    str = str.replace(/\[\[WIKIBOOKS:/gi, '[[b:');
	    str = str.replace(/\[\[WIKIQUOTE:/gi, '[[q:');
	    str = str.replace(/\[\[WIKISOURCE:/gi, '[[s:');
	    str = str.replace(/\[\[WIKISPECIES:/gi, '[[species:');
	    str = str.replace(/\[\[WIKIVERSITY:/gi, '[[v:');
	    str = str.replace(/\[\[(?:WIKIMEDIA|FOUNDATION):/gi, '[[wmf:');
	    str = str.replace(/\[\[METAWIKIPEDIA:/gi, '[[m:');
	
	    //Replace [[Foo #bar]] -> [[Foo#bar]]
	    str = str.replace(/\[\[([^\]]*?)( |_)+#([^\]]*?)\]\]/g, '[[$1#$3]]');
	
	    //Replace [[Foo|Foo]] -> [[Foo| ]]
	    str = str.replace(/\|\]\]/g, '| ]]');
	
	    return str;
	},
	
	// Converts the first character in a string to lower case
	// Notes: Used by autoEdWikilinks
	autoEd_first2lower: function(str) {
	    if (str != "") {
	        var letter = str.substr(0, 1);
	        return letter.toLowerCase() + str.substr(1, str.length);
	    } else {
	        return "";
	    }
	},
	
	//---------------------htmltowikitext.js--------------------------------
	//Convert HTML to wikitext
	autoEdHTMLtoWikitext: function(str) {
	        // <b>, <strong>, <i>, and <em> tags
	        str = str.replace(/<(B|STRONG)[ ]*>((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\3>)*?)<\/\1[ ]*>/gi, "'''$2'''");
	        str = str.replace(/<(I|EM)[ ]*>((?:[^<>]|<[a-z][^<>]*\/>|<([a-z]+)(?:| [^<>]*)>[^<>]*<\/\3>)*?)<\/\1[ ]*>/gi, "''$2''");
	        // </br>, <\br>, <br\>, <BR />, ...
	        str = str.replace(/<[\\\/]+BR[\\\/\s]*>/gim, '<br />');
	        str = str.replace(/<[\\\/\s]*BR[\s]*[\\\/]+[\s]*>/gim, '<br />');
	        // <.br>, <br.>, <Br>, ...
	        str = str.replace(/<[\s\.]*BR[\s\.]*>/gim, '<br>');
	        // <br>>, <<br />, <<br >> ...
	        str = str.replace(/<[\s]*(<br[\s\/]*>)/gim, '$1');
	        str = str.replace(/(<br[\s\/]*>)[\s]*>/gim, '$1');
	        // <hr> مشکل دارد
	        // str = str.replace(/([\r\n])[\t ]*<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1----');
	        // str = str.replace(/(.)<[\\\/\. ]*HR[\\\/\. ]*>/gi, '$1\n----');
	        // Not really an HTML-to-wikitext fix, but close enough
	        str = str.replace(/<[\\\/\s]*REFERENCES[\\\/\s]*>/gim, '<references />');
	        // Repeated references tag
	        str = str.replace(/(<references \/>)[\s]*\1/gim, '$1');
	        // Make sure <H1>, ..., <H6> is after a newline
	        str = str.replace(/([^\r\n ])[\t ]*(<H[1-6][^<>]*>)/gim, '$1\n$2');
	        // Make sure </H1>, ..., </H6> is before a newline
	        str = str.replace(/(<\/H[1-6][^<>]*>)[\t ]*([^\r\n ])/gim, '$1\n$2');
	        // Remove newlines from inside <H1>, ..., <H6>
	        var loopcount = 0;
	        while (str.search(/<H([1-6])[^<>]*>(?:[^<>]|<\/?[^\/h\r\n][^<>]*>)*?<\/H\1[^<>]*>/gim) >= 0 && loopcount <= 10) {
	            str = str.replace(/(<H)([1-6])([^<>]*>(?:[^<>]|<\/?[^\/h\r\n][^<>]*>)*?)[\r\n]((?:[^<>]|<\/?[^\/h\r\n][^<>]*>)*?<\/H)\2([^<>]*>)/gim, '$1$2$3 $4$2$5');
	            loopcount++;
	        }
	        // Replace <H1>, ..., <H6> with wikified section headings داخل تگ‌هایی مثل سورس را نباید تغییر دهد
	        /*str = str.replace(/(^|[\r\n])[\t ]*<H1[^<>]*>([^\r\n]*?)<\/H1[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1=$2=$3');
	        str = str.replace(/(^|[\r\n])[\t ]*<H2[^<>]*>([^\r\n]*?)<\/H2[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1==$2==$3');
	        str = str.replace(/(^|[\r\n])[\t ]*<H3[^<>]*>([^\r\n]*?)<\/H3[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1===$2===$3');
	        str = str.replace(/(^|[\r\n])[\t ]*<H4[^<>]*>([^\r\n]*?)<\/H4[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1====$2====$3');
	        str = str.replace(/(^|[\r\n])[\t ]*<H5[^<>]*>([^\r\n]*?)<\/H5[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1=====$2=====$3');
	        str = str.replace(/(^|[\r\n])[\t ]*<H6[^<>]*>([^\r\n]*?)<\/H6[\r\n\t ]*>[\t ]*([\r\n]|$)/gim, '$1======$2======$3');*/
	
	        return str;
	    },
	
	//---------------------headlines.js--------------------------------
	autoEdHeadlines: function(str) { //MAIN FUNCTION describes list of fixes
	
	    // Remove bold from section headings
	    var loopcount = 0;
	    while (str.search(/^[=]{1,5}[^=\r\n]*'''[^=\r\n]*[=]{1,5}/gim) >= 0 && loopcount <= 10) { //'
	        str = str.replace(/(^[=]{1,5}[^=\r\n]*)'''([^=\r\n]*[=]{1,5})[\t ]*/gim, '$1$2'); //'
	        loopcount++;
	    }
	
	    // Remove trailing colon from section headings
	    str = str.replace(/(^[=]{1,5}[^=\r\n]*)[:]([\t ]*[=]{1,5})[\t ]*/gim, '$1$2');
	
	    // Correct caps in "See also" section
	    str = str.replace(/(==[\t ]*)see also([\t ]*==)/gi, "$1See also$2");
	
	    // Change common synonyms for "See also" to "See also", but only if "See also" doesn't exist
	    if (!str.match(/=[\t ]*See also[\t ]*=/gi)) {
	        str = str.replace(/(==[\t ]*)(?:related topics|related articles|internal links|also see)([\t ]*==)/gi, "$1See also$2");
	    }
	    // Common synonyms for "External links"
	    str = str.replace(/(==[\t ]*)(?:external links?|outside links?|web ?links?|exterior links?)([\t ]*==)/gi, "$1External links$2");
	
	    // Capitalization and/or plural of "References", "Sources", "Further reading"
	    str = str.replace(/(==[\t ]*)references([\t ]*==)/gi, "$1References$2");
	    str = str.replace(/(==[\t ]*)sources([\t ]*==)/gi, "$1Sources$2");
	    str = str.replace(/(==[\t ]*)further readings?([\t ]*==)/gi, "$1Further reading$2");
	
	    return str;
	},
	
	
	//---------------------tablestowikitext.js--------------------------------
	autoEdTablestoWikitext: function(str) { //MAIN FUNCTION describes list of fixes
	
	        // Remove newlines from inside table specific tags
	        var loopcount = 0;
	        while (str.search(/(?:<\/?table|<\/?tr|<\/?td|<\/?th)[^<>]*[\r\n]/gi) >= 0 && loopcount <= 10) {
	            str.replace(/((?:<\/?table|<\/?tr|<\/?td|<\/?th)[^<>]*)[\r\n]/gi, '$1 ')
	            loopcount++;
	        }
	        // Remove extra whitespace from inside table specific tags
	        str = str.replace(/(<table|<tr|<td|<th)([^<>]*?)[\s]+(>)/gim, '$1$2$3');
	        str = str.replace(/(<table|<tr|<td|<th)([^<>]*?)[\s][\s]+/gim, '$1$2 ');
	        // Remove any extra junk </tr>, </td>, </th>, </table>
	        str = str.replace(/(<\/table|<\/tr|<\/td|<\/th)[^<>]+(>)/gim, '$1$2');
	        // Remove space whitespace after </tr>, </td>, </th>, <table>
	        str = str.replace(/(<\/tr>|<\/td>|<\/th>|<table[^<>]*>)[\s]+/gim, '$1');
	        // Remove space before <tr>, <td>, <th>, </table>
	        str = str.replace(/[\s]+(<\/table>|<tr[^<>]*>|<td[^<>]*>|<th[^<>]*>)/gim, '$1');
	        // Replace '<table>' with '{|'
	        str = str.replace(/<table( [^<>]*|)>[\s]*/gim, '{|$1\n');
	        // Replace '</table>' with '|}'
	        str = str.replace(/[\s]*<\/table>/gi, '\n|}');
	        // Replace '</td><td>' with '||'
	        str = str.replace(/<\/td[\s]*>[\s]*<td[\s]*>/gim, '||');
	        str = str.replace(/<\/td[\s]*>[\s]*<td ([^<>]+)>/gim, '|| $1 |');
	        // Replace '</th><th>' with '!!'
	        str = str.replace(/<\/th[\s]*>[\s]*<th[\s]*>/gim, '!!');
	        str = str.replace(/<\/th[\s]*>[\s]*<th ([^<>]+)>/gim, '!! $1 |');
	        // Replace '</td></tr>' and '</th></tr>' with EOL
	        str = str.replace(/<\/(?:td|th)>[\s]*<\/tr>[\s]/gim, '\n');
	        // Replace '</td>', '</th>', '</tr>' with EOL
	        str = str.replace(/<\/(?:td|th|tr)>[\s]*/gim, '\n');
	        // Replace '<tr>' with '|-'
	        str = str.replace(/[\s]*<tr>[\s]*/gim, '\n|-\n');
	        str = str.replace(/[\s]*<tr ([^<>]*)>[\s]*/gim, '\n|- $1\n');
	        // Replace '<td>' with '|'
	        str = str.replace(/[\s]*<td>([^\s])/gim, '\n| $1');
	        str = str.replace(/[\s]*<td>([\s])/gim, '\n|$1');
	        str = str.replace(/[\s]*<td[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n| $1 | $2');
	        str = str.replace(/[\s]*<td[\s]*([^<>]*?)[\s]*>([\s])/gim, '\n| $1 |$2');
	        // Replace '<th>' with '!'
	        str = str.replace(/[\s]*<th>([^\s])/gim, '\n! $1');
	        str = str.replace(/[\s]*<th>([\s])/gim, '\n!$1');
	        str = str.replace(/[\s]*<th[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n! $1 | $2');
	        str = str.replace(/[\s]*<th[\s]*([^<>]*?)[\s]*>([^\s])/gim, '\n! $1 |$2');
	
	        return str;
	    },
	
	//---------------------extrabreaks.js--------------------------------
	autoEdExtraBreaks: function(str) { //MAIN FUNCTION describes list of fixes
	
	        //Usually unneeded BR tags from ends of image descriptions and wikilinks (]]), templates (}}), template parameters (|)
	        str = str.replace(/[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\t\n ]*?)(\]\]|}}|\|)/gim, '$1$2');
	        //BR tag before a list item
	        str = str.replace(/[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\s]*?[\n]\*)/gim, '$1');
	        //BR tag followed by at least two newlines
	        str = str.replace(/[\t ]*<[\s\/\.]*br[\s\/\.]*>[\t ]*([\n])[\t ]*([\n])/gim, '$1$2');
	
	        return str;
	    },
	
	//---------------------links.js--------------------------------
	autoEdLinks: function(str) { //MAIN FUNCTION describes list of fixes
	
	    str = str.replace(/\]\[/g, "] [");
	
	    //repair bad external links
	    str = str.replace(/\[?\[http:\/\/([^\]\n]*?)\]\]?/gi, "[http://$1]");
	    //str = str.replace(/\[http:\/\/([^\]]*?)\|([^\]]*?)\]/gi, "[http://$1 $2]");
	
	    return str;
	},
};