MediaWiki:Gadget-Extra-Editbuttons-autoed.js

From Commons Wiki Poetry
Revision as of 18:05, 31 May 2023 by منتذا>Admin (صفحه‌ای تازه حاوی «→‎global mw: //Selected from en:WP:AutoEd scripts var autoEd = (function () { "use strict"; //---------------------isbn.js-------------------------------------- function autoEdISBN(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"); //ISSN regexs from [[:en:Wikipedia:AutoWikiBro...» ایجاد کرد)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*global mw*/
//Selected from [[:en:WP:AutoEd]] scripts
var autoEd = (function () {
    "use strict";
    //---------------------isbn.js--------------------------------------
    function autoEdISBN(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");
        //ISSN regexs from [[:en:Wikipedia:AutoWikiBrowser/Settings/ISSN]]
        str = str.replace(/ISSN\s*(\d)/gi,"ISSN $1");
        str = str.replace(/ISSN (\d)(\d)(\d)(\d)[\.\: ~\=]*(\d)(\d)(\d)([\dx])/gi,"ISSN $1$2$3$4-$5$6$7$8 ");
        str = str.replace(/ISSN (\d)(\d)(\d)(\d)\-(\d)(\d)(\d)x/gi,"ISSN $1$2$3$4-$5$6$7X");
        str = str.replace(/ISSN (\d)(\d)(\d)(\d)\-(\d)(\d)(\d)x/gi,"ISSN $1$2$3$4-$5$6$7X");

        //ISBN regexs from [[:Wikipedia:AutoWikiBrowser/Settings/ISBN-hyph]]
        str = str.replace(/ISBN(\d)/gi,"ISBN $1");
        str = str.replace(/\[\[ *(ISBN [\d\-x]{10,13}) *\]\]/gi,"$1");
        str = str.replace(/\[\[ISBN\|(ISBN\s*[^\]]*)\]\]/gi,"$1");
        str = str.replace(/\[*ISBN\]*\:*[ \t]+([0-9X\-]+)/gi,"ISBN $1");
        str = str.replace(/ISBN +([\d-]{1,9}) (\d+|X\W)/gi,"ISBN $1$2");
        str = str.replace(/\[*ISBN\]*\:* *\[\[Special\:Booksources\/\d*\|([\dxX\- ]+)\]\]/gi,"ISBN $1");
        str = str.replace(/\[isbn\]\:* *(\d)/gi,"ISBN $1");
        str = str.replace(/ISBN (\d{10,10}) - *(\d)/gi,"ISBN $1 ,$2");
        var loopcount = 0;
        while (loopcount<10) { //'
            str = str.replace(/ISBN (\d{1,9}) (\d|x)/gi,"ISBN $1$2");
            loopcount++;
        }
        str = str.replace(/ISBN (\d{1,9})(x)/gi,"ISBN $1X");
        str = str.replace(/ISBN (\d\d\d\d\d\d\d\d\d(\d|x)) +(\d)/gi,"ISBN $1, $3");
        str = str.replace(/ISBN ([\d-]{12,12}) (\d|x)/gi,"ISBN $1-$2");
        /* broken ISBNs with hyphens */
        // autoFormatter.js > cleanISBNs 
        str = str.replace(
            /(^|[\s#'(*>|])(?:(ISBN\d?\s*=\s*)|ISBN(?:-?1[03]\b| *1[03]:)?:?\s*)(9-?7-?[89]-?)?([013][\d\u2010-\u2012\u2212-]{8,}[\dX]\b)/gim,
            function( $0, $1, $2, $3, $4 ) {
                return $1 + ( $2 || 'ISBN ' ) + ( $3 || '' ).replace( /^9\D*7\D*(\d)\D*/, '97$1-' ) + $4
                    /* Remove all dashes */
                    .replace( /[^\dX]+/gi, '' )
                    /* Group 0 for English books */
                    .replace( /^0([01]\d)(\d{6})\B/, '0$1-$2-' )
                    .replace( /^0([2-6]\d\d)(\d{5})\B/, '0$1-$2-' )
                    .replace( /^0(7\d{3}|8[0-4]\d\d)(\d{4})\B/, '0$1-$2-' )
                    .replace( /^0(8[5-9]\d{3})(\d{3})\B/, '0$1-$2-' )
                    .replace( /^0(9[0-4]\d{4})(\d\d)\B/, '0$1-$2-' )
                    .replace( /^0(9[5-9]\d{5})(\d)\B/, '0$1-$2-' )
                    /* Group 1 for English books */
                    .replace( /^1(0\d)(\d{6})\B/, '1$1-$2-' )
                    .replace( /^1([1-3]\d\d)(\d{5})\B/, '1$1-$2-' )
                    .replace( /^1(4\d{3}|5[0-4]\d\d)(\d{4})\B/, '1$1-$2-' )
                    .replace( /^1(5[5-9]\d{3}|[67]\d{4}|8[0-5]\d{3}|86[0-8]\d\d|869[0-7]\d)(\d{3})\B/, '1$1-$2-' )
                    .replace( /^1(869[89]\d\d|8[7-9]\d{4}|9[0-8]\d{4}|99[0-8]\d{3})(\d\d)\B/, '1$1-$2-' )
                    .replace( /^1(999\d{4})(\d)\B/, '1$1-$2-' )
                    /* Group 3 for German books */
                    .replace( /^3(0[0-24-9]|1\d)(\d{6})\B/, '3$1-$2-' )
                    .replace( /^3(03[0-3]|[2-6]\d\d)(\d{5})\B/, '3$1-$2-' )
                    .replace( /^3(03[4-6]\d|7\d{3}|8[0-4]\d\d)(\d{4})\B/, '3$1-$2-' )
                    .replace( /^3(03[7-9]\d\d|8[5-9]\d{3}|95[4-9]\d\d|9[69]\d{3})(\d{3})\B/, '3$1-$2-' )
                    .replace( /^3(9[0-4]\d{4})(\d\d)\B/, '3$1-$2-' )
                    .replace( /^3(95[0-3]\d{4}|9[78]\d{5})(\d)\B/, '3$1-$2-' )
                    /* Add missing dash after group */
                    .replace( /^([0-57]|6\d\d|8\d|9[0-4]|9[5-8]\d|99[0-8]\d|999\d\d)\B/, '$1-' );
            }
        );
        return str
    }

    //---------------------whitespace.js--------------------------------
    function autoEdWhitespace(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");

        //==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;
    }

    //---------------------htmltowikitext.js--------------------------------
    //Convert HTML to wikitext
    function autoEdHTMLtoWikitext(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");

        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');
        //Replace <ol><li> with #
        str = str.replace(
                    /(\<ol\>[\s\S]+\<\/ol\>)/g,
                    function($1) {
                        return $1.replace(/[\r\n] *\<li\>/g,'\n# ').replace(/\<\/li\>/g,'');
                    }
                ).replace(/\n\<\/?ol\>/g,'')
        //Replace <ul><li> with *
        str = str.replace(
                    /(\<ul\>[\s\S]+\<\/ul\>)/g,
                    function($1) {
                        return $1.replace(/[\r\n] *\<li\>/g,'\n* ').replace(/\<\/li\>/g,'');
                    }
                ).replace(/\n\<\/?ul\>/g,'')

        return str;
    }

    //---------------------headlines.js--------------------------------
    function autoEdHeadlines(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--------------------------------
    function autoEdTablestoWikitext(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 = 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--------------------------------
    function autoEdExtraBreaks(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;
    }
    
    return {
        autoEdISBN: autoEdISBN,
        autoEdWhitespace: autoEdWhitespace,
        autoEdHTMLtoWikitext: autoEdHTMLtoWikitext,
        autoEdHeadlines: autoEdHeadlines,
        autoEdTablestoWikitext: autoEdTablestoWikitext,
        autoEdExtraBreaks: autoEdExtraBreaks
    };
}());
if (typeof window !== "undefined") {
    window.autoEd = autoEd;
}