Jump to content

User:Ohconfucius/script/tometricunits.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.
// <nowiki>

function Ohc_CONVERT_TOMETRICUNITS(){
//unlinking main units before adding template
	regex(/\[\[((?:acre|yard|f(ee|oo)t|inch|mile|pound|ounce)s?)\]\]/gi, '$1');
	regex(/\[\[pound \(mass\)\|((?:lb|pound)s?)\]\]/gi, '$1');
	regex(/\[\[foot \(unit\)\|(f[eo]{2}t)\]\]/gi, '$1');

//imperial conversion fixes
		////velocity
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(mph|miles per hour)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|mph|km\/h}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(mph|miles per hour)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|mph|km\/h}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(ft\/s|f[eo]{2}t per second)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|ft\/s|m\/s}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(ft\/s|f[eo]{2}t per second)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|ft\/s|m\/s}}');

////ranges
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:pounds|lbs?\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:pounds|lbs?)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|$3|$4$5|lb|kg}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:pounds|lbs?\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:pounds|lbs?)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|$3|$4$5|lb|kg}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:pounds|lbs?\.?)|)\s?([–—])\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:pounds|lbs?\.?)/g, '{{convert|$1$2|to|$4$5|lb|kg}}');

	regex(/(?<![,\d\(\[])(\d{1,2}(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:inch(?:es|)|in\.?)|)\s?(to|-)\s?(\d{1,2}(?:\.\d+|))(?:&nbsp;|\s|)(?:inch(?:es|)|in)\.(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1|$2|$3|in|cm}}');
	regex(/(?<![,\d\(\[])(\d{1,2}(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:inch(?:es|)|in\.?)|)\s?(to|-)\s?(\d{1,2}(?:\.\d+|))(?:&nbsp;|\s|)(?:inch(?:es|)|in)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1|$2|$3|in|cm}}');
	regex(/(?<![,\d\(\[])(\d{1,2}(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:inch(?:es|)|in\.?)|)\s?([–—])\s?(\d{1,2}(?:\.\d+|))(?:&nbsp;|\s|)(?:inch(?:es|)|in\.)/g, '{{convert|$1|$2|$3|in|cm}}');

	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:feet|ft\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:feet|ft)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|$3|$4$5|ft|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:feet|ft\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:feet|ft)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|$3|$4$5|ft|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:yards|yds?\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:yards|yds?)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|$3|$4$5|yd|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:yards|yds?\.?)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:yards|yds?)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|$3|$4$5|yd|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:miles)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)mi(?:les|)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|$3|$4$5|mi|km}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:(?:&nbsp;|\s|)(?:miles)|)\s?(to|-)\s?(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)miles(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|$3|$4$5|mi|km}}');

////multiple units
	regex(/(?<![,\d\(\[])(\d+)(?:&nbsp;|\s|)(feet|ft\.?),?(?:&nbsp;|\s|)([01]?\d)(?:&nbsp;|\s|)in(?:ches|ch|)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1|ft|$3|in}}');
	regex(/(?<![,\d\(\[])(\d+)(?:&nbsp;|\s|)(feet|ft\.?),?(?:&nbsp;|\s|)([01]?\d)(?:&nbsp;|\s|)in(?:ches|ch|)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1|ft|$3|in}}');
	regex(/(?<![,\d\(\[])(\d+)(?:&nbsp;|\s|)(pounds?|lbs?\.?),?(?:&nbsp;|\s|)([01]?\d)(?:&nbsp;|\s|)(?:oz|ounces?)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1|lb|$3|oz}}');
	regex(/(?<![,\d\(\[])(\d+)(?:&nbsp;|\s|)(pounds?|lbs?\.?),?(?:&nbsp;|\s|)([01]?\d)(?:&nbsp;|\s|)(?:oz|ounces?)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1|lb|$3|oz}}');


////simple
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:pounds|lbs?)(?=[\.,;]?(?:\s+[a-z\(]|<|\n))/g, '{{convert|$1$2|lb|kg}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:pounds|lbs?)(?=\.\s+[A-Z]|\s.\W|\n)/g, '{{convert|$1$2|lb|kg}}');

	regex(/(?<![,\d\(\[])(\d{1,2}(?:\.\d+|))(?:&nbsp;|\s|)in\.(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|in|cm}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)in(?:ches|ch)(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|in|cm}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:feet|ft)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|ft|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:yards?|yds?)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|yd|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(mi)(?:les|)\.?(?=[\.,;]?\s+[a-z\(])/g, '{{convert|$1$2|$3|km}}');

	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)inches/g, '{{convert|$1$2|in|cm}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)feet/g, '{{convert|$1$2|ft|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)yards/g, '{{convert|$1$2|yd|m}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)miles/g, '{{convert|$1$2|$3|km}}');

////adjectival
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(in)ch(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|in|cm|adj=on}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(foot|ft)(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|ft|m|adj=on}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(yard|yd)(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|yd|m|adj=on}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(mi)le(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|mi|km|adj=on}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(pound|lb)(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|lb|kg|adj=on}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))-(acre)(?:&nbsp;|\s)(?!\()/g, '{{convert|$1$2|acre|ha|adj=on}}');

////area (shorthand)
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:sq\. ft\.|sq ?ft)(&nbsp;|\s)(?!\()/g, '{{convert|$1$2|sqft|m2}}$3');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:sq\. yd\.|sq ?yd)(&nbsp;|\s)(?!\()/g, '{{convert|$1$2|sqyd|m2}}$3');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)(?:sq mi)(&nbsp;|\s)(?!\()/g, '{{convert|$1$2|sqmi|km2}}$3');
	
//area longhand
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)square feet/g, '{{convert|$1$2|sqft|m2}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)square yards?/g, '{{convert|$1$2|sqyd|m2}}');
	regex(/(?<![,\d\(\[])(\d*),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)square miles?/g, '{{convert|$1$2|sqmi|km2}}');
	regex(/(?<![,\d\(\[])(\d+),?(\d+(?:\.\d+|))(?:&nbsp;|\s|)acres?/g, '{{convert|$1$2|acre|ha}}');

	regex(/(?<![,\d\(\[])(\d+(?:\.\d+|))(?:&nbsp;|\s|)acres?/g, '{{convert|$1|acre|ha}}');



}
 function Ohc_CONVERT_TOMETRICUNITS_driver(){
	//Add a tag to the summary box
	Ohc_CONVERT_TOMETRICUNITS();
	setreason('Applying Convert template to non-metric units', 'appendonce');
}
 
$.when(mw.loader.using('mediawiki.util'), $.ready).done(function () {
 if(document.forms.editform) {
  mw.util.addPortletLink('p-tb', 'javascript:Ohc_CONVERT_TOMETRICUNITS_driver()', 'CONVERT to metric', 'convert', 'convert', '', '');

  }
});
// </nowiki>