Module:ConvertNumeric: Difference between revisions
Richardpruen (talk | contribs) m 1 revision imported |
imported>Johnuniq please don't use tricky syntax: stick to boring stuff that works |
||
Line 3: | Line 3: | ||
-- When editing, preview with: [[Module_talk:ConvertNumeric/testcases]] | -- When editing, preview with: [[Module_talk:ConvertNumeric/testcases]] | ||
-- First, edit [[Module:ConvertNumeric/sandbox]], then preview with [[Module_talk:ConvertNumeric/sandbox/testcases]] | -- First, edit [[Module:ConvertNumeric/sandbox]], then preview with [[Module_talk:ConvertNumeric/sandbox/testcases]] | ||
require('strict') | |||
local ones_position = { | local ones_position = { | ||
Line 626: | Line 627: | ||
local function _numeral_to_english2(args) | local function _numeral_to_english2(args) | ||
local num = args.num | local num = tostring(args.num) | ||
num = num:gsub("^%s*(.-)%s*$", "%1") -- Trim whitespace | |||
num = num:gsub(",", "") -- Remove commas | |||
num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age | |||
if num ~= '' then -- a fraction may have an empty whole number | |||
if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then | |||
-- Input not in a valid format, try to eval it as an expr to see | |||
-- if that produces a number (e.g. "3 + 5" will become "8"). | |||
local noerr, result = pcall(mw.ext.ParserFunctions.expr, num) | |||
if noerr then | |||
num = result | |||
end | end | ||
end | end | ||
end | end | ||
-- | -- Call helper function passing args | ||
return _numeral_to_english( | return _numeral_to_english( | ||
num, | num, | ||
Line 681: | Line 683: | ||
function p.numeral_to_english(frame) | function p.numeral_to_english(frame) | ||
local args = frame.args | local args = frame.args | ||
-- Tail call to helper function passing args from frame | |||
return _numeral_to_english2{ | |||
['num'] = args[1], | |||
['numerator'] = args['numerator'], | |||
['denominator'] = args['denominator'], | |||
['capitalize'] = args['case'] == 'U' or args['case'] == 'u', | |||
['use_and'] = args['sp'] ~= 'us', | |||
['hyphenate'] = args['adj'] == 'on', | |||
['ordinal'] = args['ord'] == 'on', | |||
['plural'] = args['pl'] == 'on', | |||
['links'] = args['lk'], | |||
['negative_word'] = args['negative'], | |||
['round'] = args['round'], | |||
['zero'] = args['zero'], | |||
['use_one'] = args['one'] == 'one' -- experiment: using '|one=one' makes fraction 2+1/2 give "two and one-half" instead of "two and a half" | |||
} | |||
args['case'] == 'U' or args['case'] == 'u', | |||
args['sp'] ~= 'us', | |||
args['adj'] == 'on', | |||
args['ord'] == 'on', | |||
args['pl'] == 'on', | |||
args['lk'], | |||
args['negative'], | |||
args['round'], | |||
args['zero'], | |||
args['one'] == 'one' -- experiment: using '|one=one' makes fraction 2+1/2 give "two and one-half" instead of "two and a half" | |||
end | end | ||