# MediaWiki:Gadget-fixRefs.js

From faraghaib
Jump to navigation Jump to search

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)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.

    //<pre><code>
    //
    // [[User:Gimmetrow/fixRefs.js]]

    function fixRefMarks() {
       var qt = String.fromCharCode(34)
       // var hb = String.fromCharCode(124)
       var txt = document.editform.wpTextbox1;

       // fact and other templates (geographic reference template, ref/note)
       var FactTag = "({{ *مدرک *}}|{{ *نیازمند +منبع *}}|{{[ ]*fact[ ]*}}|{{[ ]*fact[ ]*[\|][^}]*}}|{{[ ]*facts[ ]*}}|{{[ ]*citequote[ ]*}}|{{[ ]*citation needed[ ]*}}|{{[ ]*citation needed[ ]*[\|][^}]*}}|{{[ ]*cn[ ]*}}|{{[ ]*verification needed[ ]*}}|{{[ ]*verify source[ ]*}}|{{[ ]*verify credibility[ ]*}}|{{[ ]*who[ ]*}}|{{[ ]*failed verification[ ]*}}|{{[ ]*nonspecific[ ]*}}|{{[ ]*dubious[ ]*}}|{{[ ]*or[ ]*}}|{{[ ]*lopsided[ ]*}}|{{[ ]*GR[ ]*[\|][ ]*[^ ]+[ ]*}}|{{[ ]*[c]?r[e]?f[ ]*[\|][^}]*}}|{{[ ]*ref[ _]label[ ]*[\|][^}]*}}|{{[ ]*ref[ _]num[ ]*[\|][^}]*}})";

       txt.value = txt.value
       .replace(new RegExp("[\n\r\f\t ]+?"+FactTag, "gi"), "$1")
       // one+ space/linefeed, avoid matching references tag as a named ref
       .replace(new RegExp("[\n\r\f\t ]+?<ref([ >])", "gi"), "<ref$1")
       // remove trailing spaces from named refs
       .replace(new RegExp("<ref ([^>]*[^> ])[ ]*>", "gi"), "<ref $1>")
       // remove some superscripted punctuation between refs (is between necessary?)
       .replace(new RegExp("(</ref>|<ref[^>]*?/>)<sup>[ ]*[,;-]?[ ]*</sup><ref", "gi"), "$1<ref")
       .replace(new RegExp("(</ref>|<ref[^>]*?/>)[ ]*[,;-]?[ ]*<ref", "gi"), "$1<ref")

       .replace(new RegExp("(</ref>|<ref[^>]*?/>)[ \n\r\f\t]*([\.,])", "gi"), "$1$2");

       var LacksPunctuation = "([^\.،؛,;:!؟\?"+qt+"'’])";
       var QuestionOrExclam = "([!؟\?])";
       var MinorPunctuation = "([\.،؛,;:])";
       var Blockquote       = "(</blockquote>)";

       var Quote = "([" + qt + "'’]*)";
       //var Space = "[\n\r\f\t ]*"; // to handle  http://en.wikipedia.org/w/index.php?title=Cat&diff=99697351&oldid=99697026
       var Space = "[ ]*";
       var Space2 = "[\n\r\f\t ]*";

       var AnyPunctuation = "([\.،؛,;:!؟\?])";
       var MajorPunctuation = "([،؛,;:!؟\?])";
       var Period = "([\.])";

       // can this complicated close tag be simplified?
       //var closetag = "(([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?)</ref>";
       //var closetag = "((.*?))</ref>";
       var RefTag1 = "(<ref>([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?</ref>)";
       var RefTag2 = "(<ref[^>]*?[^/]>([^<]|<[^/]|</[^r]|</r[^e]|</re[^f]|</ref[^>])*?</ref>)";
       var RefTag3 = "(<ref[^>]*?/>)";
       //var Note = "("+RefTag1 + "|(" + RefTag2 + ")|(" + FactTag + "))";

       var match0a = LacksPunctuation+Quote+FactTag+Space+AnyPunctuation;
       var match0b = QuestionOrExclam+Quote+FactTag+Space+MajorPunctuation;
       var match0c = MinorPunctuation+Quote+FactTag+Space+AnyPunctuation;
       var match0d = QuestionOrExclam+Quote+FactTag+Space+Period;

       var match1a = LacksPunctuation+Quote+RefTag1+Space+AnyPunctuation;
       var match1b = QuestionOrExclam+Quote+RefTag1+Space+MajorPunctuation;
       var match1c = MinorPunctuation+Quote+RefTag1+Space+AnyPunctuation;
       var match1d = QuestionOrExclam+Quote+RefTag1+Space+Period;

       var match2a = LacksPunctuation+Quote+RefTag2+Space+AnyPunctuation;
       var match2b = QuestionOrExclam+Quote+RefTag2+Space+MajorPunctuation;
       var match2c = MinorPunctuation+Quote+RefTag2+Space+AnyPunctuation;
       var match2d = QuestionOrExclam+Quote+RefTag2+Space+Period;

       var match3a = LacksPunctuation+Quote+RefTag3+Space+AnyPunctuation;
       var match3b = QuestionOrExclam+Quote+RefTag3+Space+MajorPunctuation;
       var match3c = MinorPunctuation+Quote+RefTag3+Space+AnyPunctuation;
       var match3d = QuestionOrExclam+Quote+RefTag3+Space+Period;

       for (var j=0; j<5; j++)  // repeat for multiple refs together
       {
          txt.value = txt.value
          .replace(new RegExp(match0a, "gi"), "$1$2$4$3")
          .replace(new RegExp(match0b, "gi"), "$1$2$4$3")
          .replace(new RegExp(match0c, "gi"), "$2$4$3")
          .replace(new RegExp(match0d, "gi"), "$1$2$3")

          .replace(new RegExp(match1a, "gi"), "$1$2$5$3")
          .replace(new RegExp(match1b, "gi"), "$1$2$5$3")
          .replace(new RegExp(match1c, "gi"), "$2$5$3")
          .replace(new RegExp(match1d, "gi"), "$1$2$3")

          .replace(new RegExp(match2a, "gi"), "$1$2$5$3")
          .replace(new RegExp(match2b, "gi"), "$1$2$5$3")
          .replace(new RegExp(match2c, "gi"), "$2$5$3")
          .replace(new RegExp(match2d, "gi"), "$1$2$3")

          .replace(new RegExp(match3a, "gi"), "$1$2$4$3")
          .replace(new RegExp(match3b, "gi"), "$1$2$4$3")
          .replace(new RegExp(match3c, "gi"), "$2$4$3")
          .replace(new RegExp(match3d, "gi"), "$1$2$3");
       }
       // blockquotes (if trapped punctuation exists, remove it, and move blockquote past at least one ref)
       txt.value = txt.value
       .replace(new RegExp(Blockquote + AnyPunctuation + FactTag, "gi"), "$3$1")
       .replace(new RegExp(Blockquote + AnyPunctuation + RefTag1, "gi"), "$3$1")
       .replace(new RegExp(Blockquote + AnyPunctuation + RefTag2, "gi"), "$3$1")
       .replace(new RegExp(Blockquote + AnyPunctuation + RefTag3, "gi"), "$3$1")
       .replace(new RegExp(Blockquote + FactTag, "gi"), "$2$1")
       .replace(new RegExp(Blockquote + RefTag1, "gi"), "$2$1")
       .replace(new RegExp(Blockquote + RefTag2, "gi"), "$2$1")
       .replace(new RegExp(Blockquote + RefTag3, "gi"), "$2$1");

       var summary = document.editform.wpSummary;
       //summary.value = summary.value + " various minor fixes ";
    }

    $(document).ready(function () {
        if(document.forms.editform) {
            mw.util.addPortletLink('p-tb', 'javascript:fixRefMarks()', 'تصحیح نمادهای پانویس', 't-reffix', 'Ref fix', '', '');
        }
    });
    //</code></pre>

Retrieved from "https://en.faraghaib.ir/w/index.php?title=MediaWiki:Gadget-fixRefs.js&oldid=286"