-- This module implements
local p =
local function clean_length(s) s = mw.ustring.gsub(s, 'centimetre', 'cm') s = mw.ustring.gsub(s, 'centimeter', 'cm') s = mw.ustring.gsub(s, 'cms', 'cm') s = mw.ustring.gsub(s, 'cm%.', 'cm') s = mw.ustring.gsub(s, 'metre', 'm') s = mw.ustring.gsub(s, 'meter', 'm') s = mw.ustring.gsub(s, 'ms', 'm') s = mw.ustring.gsub(s, 'm%.', 'm') s = mw.ustring.gsub(s, 'inches', 'in') s = mw.ustring.gsub(s, 'inch', 'in') s = mw.ustring.gsub(s, 'ins', 'in') s = mw.ustring.gsub(s, 'in%.', 'in') s = mw.ustring.gsub(s, '%[%[[Cc]entim|cm%]%]', 'cm') s = mw.ustring.gsub(s, '%[%[cm%]%]s', 'cm') s = mw.ustring.gsub(s, '%[%[m%]%]s', 'm') s = mw.ustring.gsub(s, '%[%[in|in%]%]', 'in') return send
local function isnumber(s) if s then s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', ) return tonumber(s) end return nilend
local function get_convert_length_args(s, prefer, enforce) local prefer_m = (prefer or )
'm' local prefer_cm = (prefer or )
'cm' unconverted = clean_length(s or ) -- basic unit cleaning s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ') local m = mw.ustring.find(s, 'm') local c = mw.ustring.find(s, 'cm') local i = mw.ustring.find(s, 'in') if m
nil then return , unconverted end if c ~= nil and i
nil and i
nil then local n = mw.ustring.sub(s, 1, i - 1) if isnumber(n) then return (force_m or prefer_m) and or, mw.ustring.sub(s, i+2) end return , unconverted end return , unconvertedend
function convert_length(frame, args) local targs, str = get_convert_length_args(args[1], args['prefer'] or , args['enforce'] or )
if type(targs)
function p.length(frame) return convert_length(frame, frame.args[1] and frame.args or frame:getParent.args)end
return p