Jump to content

User:Gimmetrow/fixRefs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gimmetrow (talk | contribs) at 00:12, 22 November 2007 (tst). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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 formatFix() {
   // from [[User:AndyZ/peerreviewer.js]]
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value               //whitespace
    .replace(/\t/g, " ")
    .replace(/^ ? ? \n/gm, "\n")
    .replace(/(\n\n)\n+/g, "$1")
    .replace(/== ? ?\n\n==/g, "==\n==")
    .replace(/\n\n(\* ?\[?http)/g, "\n$1")
    .replace(/^ ? ? \n/gm, "\n")
    .replace(/\n\n\*/g, "\n*")
    .replace(/[ \t][ \t]+/g, " ")
    .replace(/([=\n]\n)\n+/g, "$1")
    .replace(/ \n/g, "\n")              //ending sections 
    .replace(/(== ?)(external links:?|outside links|web ?links:?|exterior links:?)( ?==)/gi, "$1External links$3")
    .replace(/(== ?)(external link:?|web ?link:?|exterior link:?)( ?==)/gi, "$1External link$3")
    .replace(/(== ?)(reference:?)(s? ?==)/gi, "$1Reference$3")
    .replace(/(== ?)(source:?)(s? ?==)/gi, "$1Source$3")
    .replace(/(== ?)(further readings?:?)( ?==)/gi, "$1Further reading$3")
    .replace(/\[\[ ?[Cc]ategory ?: ?/g, "[[Category:")  //categories 
   //repair bad internal links
    .replace(/([^\[]|^)\[?\[([^\]]*?)\]\]?([^\]]|$)/gm, "$1[[$2]]$3")
    .replace(/\[\[ ?([^\]]*?) ?\]\]/g, "[[$1]]")
    .replace(/\[\[([^\]]*?)( |_)#([^\]]*?)\]\]/g, "[[$1#$3]]")
   //repair bad external links
    .replace(/\[?\[http:\/\/([^\]]*?)\]\]?/gi, "[http://$1]")
    .replace(/\[http:\/\/([^\]]*?)\|([^\]]*?)\]/gi, "[http://$1 $2]")
    .replace(/\[\[([^\]\|]+)\|([^\]\|]+)\]\]([A-Za-z\'][A-Za-z]*)([\.\,\;\:\"\!\?\s\n])/g
}

function delinkJustYears() {
   var txt = document.editform.wpTextbox1;
   txt.value = txt.value.replace(/([^,\]] |[^,\] ])\[\[([0-9][0-9][0-9][0-9])\]\]/g, "$1$2$3");

   var summary = document.editform.wpSummary;
   summary.value = summary.value + " delinking [[MOS:DATE|years]] ";
}

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[ ]*}}|{{[ ]*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
   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 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 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");
      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");
   }
   // 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");
   txt.value = txt.value.replace(new RegExp(Blockquote + AnyPunctuation + RefTag1, "gi"), "$3$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + AnyPunctuation + RefTag2, "gi"), "$3$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + AnyPunctuation + RefTag3, "gi"), "$3$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + FactTag, "gi"), "$2$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + RefTag1, "gi"), "$2$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + RefTag2, "gi"), "$2$1");
   txt.value = txt.value.replace(new RegExp(Blockquote + RefTag3, "gi"), "$2$1");

   var summary = document.editform.wpSummary;
   //summary.value = summary.value + " [[WP:FN]] [[User:Gimmetrow/fixRefs.js|fixes]] ";
   summary.value = summary.value + " [[WP:FN]] fixes ";
}

addOnloadHook(function () {
    if(document.forms.editform) {
        // addLink('p-cactions', 'javascript:fixRefMarks()', 'fix refs', 'ca-reffix', 'Ref fix for the edit window', '', 'ca-history');
        addPortletLink('p-tb', 'javascript:delinkJustYears()', 'Delink years only', 't-delyrs', 'Delink years', '', '');
        addPortletLink('p-tb', 'javascript:formatFix()', 'Fix format', 't-forfix', 'Format fix', '', '');
        addPortletLink('p-tb', 'javascript:fixRefMarks()', 'Fix reference marks', 't-reffix', 'Ref fix', '', '');
    }
});
//</code></pre>