Jump to content

Module:Epi

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Frietjes (talk | contribs) at 16:10, 15 October 2018 (start of a lua version of Template:Epi, has bugs and doesn't have formatnum yet). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {}

local function ntsh(n, notnum, scale)
	if tonumber(n) == nil then
		return frame:expandTemplate{ title = 'ntsh', args = {notnum}}
	else
		return frame:expandTemplate{ title = 'ntsh', args = {tonumber(n)*scale}}
	end
end

local function cvt(n1, u1, e, u2, d, fac, l, flip)

	local rnd = require('Module:Math')._round
	local so, sc = '', ''
	if n1 < 0 then
		so, sc = '<span style="color:red">', '</span>'
	end
	local n2 = rnd(n1/fac,d) .. '&nbsp;' .. u2
	n1 = rnd(n1,e) .. '&nbsp;' .. u1
	if flip then
		if l == '1' then
			return so .. n2 .. ' (' .. n1 .. ')'
		else
			return so .. n2 .. '<br/>' .. n1
		end
	else
		if l == '1' then
			return so .. n1 .. ' (' .. n2 .. ')'
		else
			return so .. n1 .. '<br/>' .. n2
		end
	end
	
	return n1
end

local function moft(n, e, d, l, s, p, flip)
	if tonumber(n) then
		n = tonumber(n)
		return cvt(n, 'm', e, 'ft', d, 0.3048, l, flip)
	end
	return ntsh(n, '0', 1) .. s .. p .. n .. s
end

local function kmomi(n, l, s, p, flip)
	if tonumber(n) then
		n = tonumber(n)
		local e, d = 0, 0
		if n < 19.995 then
			e = 2
		elseif n < 199.95 then
			e = 1
		end
		if n < 32.179 then
			d = 2
		elseif n < 321.789 then
			d = 1
		end
		return cvt(n, 'km', e, 'mi', d, 1.609344, l, flip)
	end
	return ntsh(n, '-1e10', 1000) .. s .. p .. n .. s
end

function p.main(frame)
	local args = frame.getParent().args
	local elev_m = (args[1] or '')
	local prom_m = (args[2] or '')
	local iso_km = (args[3] or '')
	local a = 'align=' .. (args['a'] or 'center')
	local r = (args['r'] or '1') ~= '1' and (' rowspan=' .. args['r']) or ''
	local d = args['d'] or '0' -- input precision
	local e = args['e'] or d   -- output precision
	local l = args['l'] or '2' -- ?
	local p = args['p'] or ''  -- prefix
	local s = args['s'] or ''  -- italics and/or bold formatting
	
	if prom_m == '>500' then
		prom_m = '500'
		p = p .. '>'
	end
	
	local elev, prom, iso = '', '', ''
	local flip = (args['m'] or '1') == '2'
	elev = '|' .. a .. r .. '|' .. moft(elev_m, e, d, l, s, p, flip) .. '\n'
	prom = '|' .. a .. r .. '|' .. moft(prom_m, d, d, l, s, p, flip) .. '\n'
	iso  = '|' .. a .. r .. '|' .. kmomi(iso_km, l, s, p, flip) .. '\n'
	return elev .. prom .. iso
end

return p