Difference between revisions of "Module:Infobox road/length"
Jump to navigation
Jump to search
test>Happy5214 (Disproportionate fix for the relatively minor problem of trailing zeros) |
test>Rschen7754 (emergency revert) |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
| − | |||
| − | |||
local function getLengths(args, num) | local function getLengths(args, num) | ||
| + | local math = require "Module:Math" | ||
local precision = math._precision | local precision = math._precision | ||
local round = math._round | local round = math._round | ||
| − | |||
local lengths = {} | local lengths = {} | ||
local km = args["length_km" .. num] or '' | local km = args["length_km" .. num] or '' | ||
local mi = args["length_mi" .. num] or '' | local mi = args["length_mi" .. num] or '' | ||
| − | |||
if '' == km then | if '' == km then | ||
local n = tonumber(mi) | local n = tonumber(mi) | ||
| − | prec = | + | local prec = tonumber(args["length_round" .. num]) or precision(mi) |
if n then | if n then | ||
| − | + | lengths.km = round(n * 1.609344, prec) | |
| − | |||
else | else | ||
| − | lengths.km = | + | lengths.km = 0 |
end | end | ||
else | else | ||
| − | + | lengths.km = tonumber(km) | |
| − | |||
| − | lengths.km = | ||
lengths.orig = "km" | lengths.orig = "km" | ||
lengths.comp = "mi" | lengths.comp = "mi" | ||
| Line 29: | Line 23: | ||
if '' == mi then | if '' == mi then | ||
local n = tonumber(km) | local n = tonumber(km) | ||
| − | prec = | + | local prec = tonumber(args["length_round" .. num]) or precision(km) |
if n then | if n then | ||
| − | + | lengths.mi = round(n / 1.609344, prec) | |
| − | |||
else | else | ||
| − | lengths.mi = | + | lengths.mi = 0 |
end | end | ||
else | else | ||
| − | + | lengths.mi = tonumber(mi) | |
| − | |||
| − | lengths.mi = | ||
lengths.orig = "mi" | lengths.orig = "mi" | ||
lengths.comp = "km" | lengths.comp = "km" | ||
| Line 50: | Line 41: | ||
local notes = args["length_notes" .. num] or '' | local notes = args["length_notes" .. num] or '' | ||
local lengths = getLengths(args, num) | local lengths = getLengths(args, num) | ||
| − | + | local lang = mw.getContentLanguage() | |
local first, second | local first, second | ||
if lengths.orig == "mi" then | if lengths.orig == "mi" then | ||
| − | first = lengths.mi | + | first = lang:formatNum(lengths.mi) |
| − | second = lengths.km | + | second = lang:formatNum(lengths.km) |
else | else | ||
| − | first = lengths.km | + | first = lang:formatNum(lengths.km) |
| − | second = lengths.mi | + | second = lang:formatNum(lengths.mi) |
end | end | ||
if first == '0' and second == '0' then | if first == '0' and second == '0' then | ||
Revision as of 18:37, 24 August 2013
Documentation for this module may be created at Module:Infobox road/length/doc
local p = {}
local function getLengths(args, num)
local math = require "Module:Math"
local precision = math._precision
local round = math._round
local lengths = {}
local km = args["length_km" .. num] or ''
local mi = args["length_mi" .. num] or ''
if '' == km then
local n = tonumber(mi)
local prec = tonumber(args["length_round" .. num]) or precision(mi)
if n then
lengths.km = round(n * 1.609344, prec)
else
lengths.km = 0
end
else
lengths.km = tonumber(km)
lengths.orig = "km"
lengths.comp = "mi"
end
if '' == mi then
local n = tonumber(km)
local prec = tonumber(args["length_round" .. num]) or precision(km)
if n then
lengths.mi = round(n / 1.609344, prec)
else
lengths.mi = 0
end
else
lengths.mi = tonumber(mi)
lengths.orig = "mi"
lengths.comp = "km"
end
return lengths
end
function p._length(num, args)
local ref = args["length_ref" .. num] or ''
local notes = args["length_notes" .. num] or ''
local lengths = getLengths(args, num)
local lang = mw.getContentLanguage()
local first, second
if lengths.orig == "mi" then
first = lang:formatNum(lengths.mi)
second = lang:formatNum(lengths.km)
else
first = lang:formatNum(lengths.km)
second = lang:formatNum(lengths.mi)
end
if first == '0' and second == '0' then
return
end
local text = {first, " ", lengths.orig, ref, " (", second, " ", lengths.comp, ")", }
if notes ~= '' then
table.insert(text, "<br><small>" .. notes .. "</small>")
end
return table.concat(text)
end
function p.length(frame)
local pframe = frame:getParent()
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
local num = config.num or ''
return p._length(num, args)
end
return p