-- This module implements
local p =
local function splitsuffix(s) local suffix = if mw.ustring.match(s,'^(.-)(
local function clean(s) s = mw.ustring.gsub(s, '[Mm]etre', 'm') s = mw.ustring.gsub(s, '([^a])meter', '%1m') -- prevents "parameter" from being changed to "param" s = mw.ustring.gsub(s, '[Cc]enti', 'c') -- changes "centim" to "cm" s = mw.ustring.gsub(s, 'ms', 'm') s = mw.ustring.gsub(s, 'm[%.,]', 'm')
s = mw.ustring.gsub(s, '[Ff]eet', 'ft') s = mw.ustring.gsub(s, 'foot', 'ft') s = mw.ustring.gsub(s, 'ft[%.,]', 'ft')
s = mw.ustring.gsub(s, '[Ii]nches', 'in') s = mw.ustring.gsub(s, '[Ii]nch', 'in') s = mw.ustring.gsub(s, 'ins', 'in') s = mw.ustring.gsub(s, 'in[%.,]', 'in')
s = mw.ustring.gsub(s, '%[%[[Mm]%]%]s', 'm') s = mw.ustring.gsub(s, '%[%[[Mm]|[Mm]%]%]', 'm') s = mw.ustring.gsub(s, '%[%[[Cc]m%]%]s', 'cm') s = mw.ustring.gsub(s, '%[%[[Cc]m|[Cc]m%]%]', 'cm') s = mw.ustring.gsub(s, '%[%[[Cc]entim|cm%]%]', 'cm') s = mw.ustring.gsub(s, '%[%[[Ii]n|in%]%]', 'in') s = mw.ustring.gsub(s, '(%d)%s+[Cc][Mm]', '%1 cm') s = mw.ustring.gsub(s, '(%d)%s+[Mm]', '%1 m') s = mw.ustring.gsub(s, '(%d)%s+[Ff][Tt]', '%1 ft') s = mw.ustring.gsub(s, '(%d)%s+[Ii][Nn]', '%1 in') s = mw.ustring.gsub(s, '(%d&[Nn][Bb][Ss][Pp];)[Cc][Mm]', '%1cm') s = mw.ustring.gsub(s, '(%d&[Nn][Bb][Ss][Pp];)[Mm]', '%1m') s = mw.ustring.gsub(s, '(%d&[Nn][Bb][Ss][Pp];)[Ff][Tt]', '%1ft') s = mw.ustring.gsub(s, '(%d&[Nn][Bb][Ss][Pp];)[Ii][Nn]', '%1in') s = mw.ustring.gsub(s, '(%d)%s*[′\']%s*(%d+)%s*[\"″]', '%1 ft %2 in')
return send
local function isnumber(s) if s then s = mw.ustring.gsub(s, '%+%s*%d+%s*/%s*%d+%s*$', ) s = mw.ustring.gsub(s, '%s*1%s*/%s*2%s*$', '0.5') return tonumber(s) end return nilend
local function trimspace(s) if s then s = mw.ustring.gsub(s, '^%s*(.-)%s*$', '%1') end return send
local function force_unit(s, t) s = mw.ustring.gsub(s, '&[Nn][Bb][Ss][Pp];', '★') if (t
local function get_convert_args(s, prefer, enforce, ri) local prefer_m = (prefer or )
'm' local prefer_cm = (prefer or )
'cm' ri = (ri or )
s = mw.ustring.gsub(unconverted, '&[Nn][Bb][Ss][Pp];', ' ') local m = mw.ustring.find(s, 'm') local c = mw.ustring.find(s, 'cm') local f = mw.ustring.find(s, 'ft') local i = mw.ustring.find(s, 'in') if m
nil and i
nil and i
1) and 'ri3' or 'ri2' end if ri ~= then adj = force_m and 'ri2' or 'ri0' end return force_m and or, mw.ustring.sub(s, c+2) end return , unconverted end if m ~= nil and c
nil and i
nil then local n1 = mw.ustring.sub(s, 1, f - 1) local n2 = mw.ustring.sub(s, f+2, i - 1) if isnumber(n1) and isnumber(n2) then if ri ~= then adj = 'ri0' end return (force_m or prefer_m) and or, mw.ustring.sub(s, i+2) end return , unconverted end if f ~= nil and i
nil then local n = mw.ustring.sub(s, 1, f - 1) if isnumber(n) then if ri ~= then local n2 = 12*(n - math.floor(n)) adj = 'ri0' return (force_m or prefer_m) and or, mw.ustring.sub(s, f+2) end return (force_m or prefer_m) and or, mw.ustring.sub(s, f+2) end return , unconverted end if i ~= nil and f
nil then local n = mw.ustring.sub(s, 1, i - 1) if isnumber(n) then if ri ~= then adj = 'ri0' end return (force_m or prefer_m) and or, mw.ustring.sub(s, i+2) end return , unconverted end if force_cm and c
function convert(frame, args) local str, suffix = splitsuffix(args[1] or ) local targs, str = get_convert_args(str, args['prefer'] or , args['enforce'] or , args['ri'] or ) str = str .. suffix
if type(targs)
function p.main(frame) return convert(frame, frame.args[1] and frame.args or frame:getParent.args)end
return p