Jump to content

Module:Convert and Module:Convert/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
Content deleted Content added
 
handle uncertainty notation
 
Line 1: Line 1:
-- Convert a value from one unit of measurement to another.
-- Convert a value from one unit of measurement to another.
-- Example: {{convert|123|lb|kg}} --> 123 pounds (56 kg)
-- Example: {{convert|123|lb|kg}} → 123 pounds (56 kg)
-- See [[:en:Template:Convert/Transwiki guide]] if copying to another wiki.
-- See [[:en:Template:Convert/Transwiki guide]] if copying to another wiki.


Line 278: Line 278:
end
end
return cat or ''
return cat or ''
⚫
end

⚫
local function subparm(text, ...)
-- Return text after substituting any '$n' in text (n = 1, 2, 3, ...)
-- with the corresponding parameter from the optional arguments.
⚫
local rep = {}
⚫
for i, v in ipairs({...}) do
⚫
rep['$' .. i] = v
⚫
end
⚫
return (text:gsub('$%d+', rep))
end
end


Line 298: Line 308:
end
end
parms.have_problem = true
parms.have_problem = true
⚫
local function subparm(fmt, ...)
⚫
local rep = {}
⚫
for i, v in ipairs({...}) do
⚫
rep['$' .. i] = v
⚫
end
⚫
return (fmt:gsub('$%d+', rep))
⚫
end
if msg then
if msg then
local parts = {}
local parts = {}
Line 2,001: Line 2,004:
end
end
return true
return true
end

local function text_uncertainty(parms, valinfo)
-- Return wikitext to display an uncertainty value.
local fmt
if parms.test == 'clean' then
fmt = {
-- Can use |test=clean for simpler (clean) output when testing.
'<~$1><±$2>',
'<~$1><+$2><-$3>',
}
else
fmt = {
-- Following is the wikitext produced by Template:Val except that val
-- would insert "&nbsp;UNIT" before the final "</span>".
-- That is too hard to do here.
'<span class="nowrap">$1<span style="margin-left:0.225em;margin-right:0.225em;">±</span>$2</span>',
'<span class="nowrap">$1<span style="margin-left:0.3em;"><span style="display:inline-block;margin-bottom:-0.3em;vertical-align:-0.4em;line-height:1.2em;font-size:85%;text-align:right;">+$2<br />−$3</span></span></span>',
}
end
return subparm(fmt[parms.uncertainty], valinfo[1].show, valinfo[2].show, (valinfo[3] or {}).show)
end
end


Line 2,067: Line 2,091:
end
end
i = i + 1
i = i + 1
range:add(range_item)
if range_item == 'uncertainty' then
parms.uncertainty = (parms.uncertainty or 0) + 1
else
range:add(range_item)
end
if type(range_item) == 'table' then
if type(range_item) == 'table' then
-- For range "x", if append unit to some values, append it to all.
-- For range "x", if append unit to some values, append it to all.
Line 2,079: Line 2,107:
if range.n > 30 then -- limit abuse, although 4 is a more likely upper limit
if range.n > 30 then -- limit abuse, although 4 is a more likely upper limit
return false, { 'cvt_invalid_num' } -- misleading message but it will do
return false, { 'cvt_invalid_num' } -- misleading message but it will do
end
if parms.uncertainty then
return false, { 'cvt_bad_range' }
end
end
parms.range = range
parms.range = range
elseif had_nocomma then
elseif had_nocomma then
return false, { 'cvt_unknown', parm2 }
return false, { 'cvt_unknown', parm2 }
end
if parms.uncertainty then
if parms.uncertainty <= 2 then -- input like 999~111 or 999~111~222
valinfo[1].show = text_uncertainty(parms, valinfo)
else
return false, { 'cvt_bad_range' }
end
end
end
return true, valinfo, i
return true, valinfo, i
Line 2,909: Line 2,947:
local function make_link(link, id, unit_table)
local function make_link(link, id, unit_table)
-- Return wikilink "[[link|id]]", possibly abbreviated as in examples:
-- Return wikilink "[[link|id]]", possibly abbreviated as in examples:
-- [[Mile|mile]] --> [[mile]]
-- [[Mile|mile]] → [[mile]]
-- [[Mile|miles]] --> [[mile]]s
-- [[Mile|miles]] → [[mile]]s
-- However, just id is returned if:
-- However, just id is returned if:
-- * no link given (so caller does not need to check if a link was defined); or
-- * no link given (so caller does not need to check if a link was defined); or
Line 3,421: Line 3,459:
else
else
decorate_value(parms, out_current, 1, true)
decorate_value(parms, out_current, 1, true)
if parms.uncertainty then
⚫
result = valinfo[1].show
result = text_uncertainty(parms, valinfo)
else
⚫
result = valinfo[1].show
end
end
end
if parms.opt_output_number_only then
if parms.opt_output_number_only then