Jump to content

User:Netmonger/fixRefs.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
//<pre><code>
// This code requires addLink from: [[User:Omegatron/monobook.js/addlink.js]]
//
// [[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[ ]*}}|{{[ ]*citequote[ ]*}}|{{[ ]*citation needed[ ]*}}|{{[ ]*cn[ ]*}}|{{[ ]*verification needed[ ]*}}|{{[ ]*verify source[ ]*}}|{{[ ]*verify credibility[ ]*}}|{{[ ]*who[ ]*}}|{{[ ]*failed verification[ ]*}}|{{[ ]*nonspecific[ ]*}}|{{[ ]*dubious[ ]*}}|{{[ ]*or[ ]*}}|{{[ ]*lopsided[ ]*}}|{{[ ]*GR[ ]*[\|][ ]*[^ ]+[ ]*}}|{{[ ]*[c]?r[e]?f[ ]*[\|][^}]*}}|{{[ ]*ref[ _]label[ ]*[\|][^}]*}})";
   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
   txt.value = txt.value.replace(new RegExp("[\n\r\f\t ]+?<ref([ >])", "gi"), "<ref$1");

   // remove trailing spaces from named refs
   txt.value = txt.value.replace(new RegExp("<ref ([^>]*[^> ])[ ]*>", "gi"), "<ref $1>");

   // remove some superscripted punctuation between refs (is between necessary?)
   txt.value = txt.value.replace(new RegExp("(</ref>|<ref[^>]*?/>)<sup>[ ]*[,;-]?[ ]*</sup><ref", "gi"), "$1<ref");
   txt.value = txt.value.replace(new RegExp("(</ref>|<ref[^>]*?/>)[ ]*[,;-]?[ ]*<ref", "gi"), "$1<ref");

   var LacksPunctuation = "([^\.,;:!\?"+qt+"'’])";
   var QuestionOrExclam = "([!\?])";
   var MinorPunctuation = "([\.,;:])";

   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 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<10; j++)  // repeat for multiple refs together
   {
      txt.value = txt.value.replace(new RegExp(match0a, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0b, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0c, "gi"), "$2$4$3");
      txt.value = txt.value.replace(new RegExp(match0d, "gi"), "$1$2$3");

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

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

      txt.value = txt.value.replace(new RegExp(match3a, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3b, "gi"), "$1$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3c, "gi"), "$2$4$3");
      txt.value = txt.value.replace(new RegExp(match3d, "gi"), "$1$2$3");
   }
   var summary = document.editform.wpSummary;
   //summary.value = summary.value + "+[[WP:FN]] [[User:Gimmetrow/fixRefs.js|fixes]] ";
   summary.value = summary.value + "+[[WP:FN]] fixes ";
}

$(function () {
    if(document.forms.editform) {
        // addLink('p-cactions', 'javascript:fixRefMarks()', 'fix refs', 'ca-reffix', 'Ref fix for the edit window', '', 'ca-history');
        addLink('p-tb', 'javascript:fixRefMarks()', 'Fix reference marks', 't-reffix', 'Ref fix for the edit window', '', '');
    }
});
//</code></pre>