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

از پژوهشکده امر به معروف
نسخهٔ تاریخ ‏۱۱ اسفند ۱۳۹۴، ساعت ۱۰:۱۶ توسط Admin (نظرات | مشارکت‌ها) (صفحه‌ای تازه حاوی «global mw: //Selected from en:WP:AutoEd scripts var autoEd = (function () { "use strict"; //---------------------isbn.js---...» ایجاد کرد)

(تفاوت) → نسخهٔ قدیمی‌تر | نمایش نسخهٔ فعلی (تفاوت) | نسخهٔ جدیدتر ← (تفاوت)
پرش به: ناوبری، جستجو

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

  • فایرفاکس / سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-F5 یا Ctrl-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-R)
  • گوگل کروم: کلیدهای Ctrl+Shift+R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-Shift-R)
  • اینترنت اکسپلورر: کلید Ctrl را نگه‌دارید و روی دکمهٔ Refresh کلیک کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید
  • اپرا: حافظهٔ نهانی مرورگر را از طریق منوی Tools → Preferences پاک کنید
/*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");

        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");

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

    //---------------------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");
        // <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 داخل تگ\u200cهایی مثل سورس را نباید تغییر دهد
        /*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--------------------------------
    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;
}