Jump to content

Module:Infobox road/length: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 14: Line 14:


local first, unit, unit2, unit3
local first, unit, unit2, unit3
if mi ~= '0' and km == '' then
if nmi ~= '0' and mi == '' and km == '' then
first = mi
first = nmi
unit = "mi"
unit = "nmi"
unit2 = "km"
unit2 = "km mi"
unit3 = "m"
unit3 = "km mi"
elseif mi ~= '0' and km == '' then
first = mi
unit = "mi"
unit2 = "km"
unit3 = "m"
elseif km ~= '0' and mi == '' then
elseif km ~= '0' and mi == '' then
first = km
first = km
unit = "km"
unit = "km"
unit2 = "mi"
unit2 = "mi"
unit3 = "mi ft"
unit3 = "mi ft"
elseif nmi ~= '0' and mi == '' and km == '' then
first = nmi
unit = "nmi"
unit2 = "km mi"
unit3 = "km mi"
end
end



Revision as of 06:27, 14 December 2021

local p = {}

local concat = table.concat
local insert = table.insert
local format = mw.ustring.format
local frame = mw.getCurrentFrame()

function p._length(num, args)
	local km = args["length_km" .. num] or ''
    local mi = args["length_mi" .. num] or ''
    local nmi = args["length_nmi" .. num] or ''
    local ref = args["length_ref" .. num] or ''
    local notes = args["length_notes" .. num] or ''

    local first, unit, unit2, unit3
    if nmi ~= '0' and mi == '' and km == '' then
		first = nmi
		unit = "nmi"
		unit2 = "km mi"
		unit3 = "km mi"
    elseif mi ~= '0' and km == '' then
		first = mi
		unit = "mi"
		unit2 = "km"
		unit3 = "m"
    elseif km ~= '0' and mi == '' then
		first = km
		unit = "km"
		unit2 = "mi"
		unit3 = "mi ft"
    end

    if mi == '' and km == '' and nmi == '' then
        return nil
    elseif notes ~= '' and first < '1' then
    	return frame:expandTemplate{ title = 'cvt', args = { first, unit, unit3, disp = "x", ref .. "&nbsp;("}} .. ")<div>" .. notes .. "</div>"
    elseif notes == '' and first < '1' then
    	return frame:expandTemplate{ title = 'cvt', args = { first, unit, unit3, disp = "x", ref .. "&nbsp;("}} .. ")"
    elseif notes ~= '' then
		return frame:expandTemplate{ title = 'cvt', args = { first, unit, unit2, disp = "x", ref .. "&nbsp;("}} .. ")<div>" .. notes .. "</div>"
	else
    	return frame:expandTemplate{ title = 'cvt', args = { first, unit, unit2, disp = "x", ref .. "&nbsp;("}} .. ")"
    end
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