Jump to content

Module:Infobox road/length

From Simple English Wikipedia, the free encyclopedia
Revision as of 09:10, 14 July 2013 by Happy5214 (talk | changes) (This won't work, but it's better than deleting it. Portions from en:Module:Routelist row.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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]
    local mi = args["length_mi" .. num]
    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 = 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 = mi
        lengths.orig = "mi"
        lengths.comp = "km"
    end
    return lengths
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 ''
    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
    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

return p