Jump to content

User:Gimmetrow/MOS.js

From Wikipedia, the free encyclopedia
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>
//
function formatFix() {
   // selections from [[User:AndyZ/peerreviewer.js]]
   var txt = document.editform.wpTextbox1;

   txt.value = txt.value
   //MOSNUM remove s
    .replace(/(\d)\s?(|kilo|hecto|deca|deci|centi|milli|micro|nano)(meter|metre|liter|litre|gram|byte)(s?\W])/gi, '$1 $2$3$4')
    .replace(/(\d)\s?(k|d|c|m|µ|µ|µ)(g|l|m)s?(\W)/gi, '$1 $2$3$4')
    .replace(/(\d)\s?(inche?|foot|feet|yard|mile|acre|pound|ounce|ton|gallon|quart|pint|cup|angstrom)(s?\W)/gi, '$1 $2$3')
    .replace(/(\d)\s?(lb|ft|yd|mi|oz)s?(\W)/gi, '$1 $2$3')
 
   //MOSNUM, first ones are currency fixes
    .replace(/\$US(\d)/gi, '[[United States dollar|US$]]$1')
    .replace(/(\W)EU€/gi, '$1€')
   //add leading zero to decimals - problem with sports articles
    //.replace(/\s(\-?)\.(\d+)\s/g, ' $10.$2 ') 
   //times fixing
    .replace(/\s(\d\d?)\s?(a|p)m\s/g, ' $1 $2.m. ')
    .replace(/\s(\d\d?)\s?AM\s/g, ' $1 a.m. ')
    .replace(/\s(\d\d?)\s?PM\s/g, ' $1 p.m. ')
    .replace(/\s(\d\d?)[\.:](\d\d)\s?(a|p)m\s/g, ' $1:$2 $3.m. ')
    .replace(/\s(\d\d?)[\.:](\d\d)\s?AM\s/g, ' $1:$2 a.m. ')
    .replace(/\s(\d\d?)[\.:](\d\d)\s?PM\s/g, ' $1:$2 p.m. ')
    .replace(/\s12\snoon(\W)/g, ' noon$1')
 
   //MSH to fix captalization issues in headings
    .replace(/\n(={2,3})(\s?)See\sAlso\s?={2,3}/g, '\n$1$2See also$2$1')
   	.replace(/\n(={2,3})(\s?)Further\sReading\s?={2,3}/g, '\n$1$2Further reading$2$1') 
   	.replace(/\n(={2,3})(\s?)Works\sCited\s?={2,3}/g, '\n$1$2Works cited$2$1')
    .replace(/\n(={2,3})(\s?)External\slink(s?)\s?={2,3}/g, '\n$1$2External link$3$2$1')
    .replace(/\n(={2,3})(\s?)Foot\s[Nn]otes\s?={2,3}/g, '\n$1$2Footnotes$2$1')
   //miscellaneous in headings (remove special characters)
    // sometimes adds extra =
    //.replace(/\n(={2,4})(.*)\s&\s(.*)={2,4}/g , '\n$1$2 and $3$1')


    .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 
}

$(function () {
    if(document.forms.editform) {
        mw.util.addPortletLink('p-tb', 'javascript:formatFix()', 'Fix format', 't-forfix', 'Format fix', '', '');
    }
});
//</code></pre>