Module:ConvertNumeric: Difference between revisions

please don't use tricky syntax: stick to boring stuff that works
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)


if (not tonumber(num)) then
num = num:gsub("^%s*(.-)%s*$", "%1")  -- Trim whitespace
num = num:gsub("^%s*(.-)%s*$", "%1")  -- Trim whitespace
num = num:gsub(",", "")  -- Remove commas
num = num:gsub(",", "")  -- Remove commas
num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age
num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age
if num ~= '' then  -- a fraction may have an empty whole number
if num ~= '' then  -- a fraction may have an empty whole number
if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
-- Input not in a valid format, try to eval it as an expr to see
-- Input not in a valid format, try to pass it through #expr to see
-- if that produces a number (e.g. "3 + 5" will become "8").
-- if that produces a number (e.g. "3 + 5" will become "8").
local noerr, result = pcall(mw.ext.ParserFunctions.expr, num)
num = mw.getCurrentFrame():preprocess('{{#expr: ' .. num .. '}}')
if noerr then
num = result
end
end
end
end
end
end


-- Pass args from frame to helper function
-- 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
local num = args[1]
-- Tail call to helper function passing args from frame
num = num:gsub("^%s*(.-)%s*$", "%1")  -- Trim whitespace
return _numeral_to_english2{
num = num:gsub(",", "")  -- Remove commas
['num'] = args[1],
num = num:gsub("^<span[^<>]*></span>", "") -- Generated by Template:age
['numerator'] = args['numerator'],
if num ~= '' then  -- a fraction may have an empty whole number
['denominator'] = args['denominator'],
if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
['capitalize'] = args['case'] == 'U' or args['case'] == 'u',
-- Input not in a valid format, try to pass it through #expr to see
['use_and'] = args['sp'] ~= 'us',
-- if that produces a number (e.g. "3 + 5" will become "8").
['hyphenate'] = args['adj'] == 'on',
num = frame:preprocess('{{#expr: ' .. num .. '}}')
['ordinal'] = args['ord'] == 'on',
end
['plural'] = args['pl'] == 'on',
end
['links'] = args['lk'],
 
['negative_word'] = args['negative'],
-- Pass args from frame to helper function
['round'] = args['round'],
return _numeral_to_english(
['zero'] = args['zero'],
num,
['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['numerator'],
}
args['denominator'],
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"
) or ''
end
end


Anonymous user