Jump to content

Module:Jct

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Happy5214 (talk | contribs) at 10:37, 7 February 2014 (Partially functional initial version). 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 insert = table.insert
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments
local parserModule = require "Module:Road data/parser"
local parser = parserModule.parser

-- Shields
local function size(args)
	local state = args.state or args.province or ''
	local type = args.type
	if state == 'FL' then
		if type == 'Toll' or type == 'FLTP' or type == 'HEFT' then
			return '20'
		end
	elseif state == 'NY' then
		if type == 'NY 1927' or type == 'NY 1948' or (type == 'Parkway' and args.route == "Robert Moses") or (type == 'CR' and args.county == 'Erie') then
			return '20'
		end
	elseif state == 'AB' then
		if type == 'AB' or type == 'Hwy' or type == '2ndHwy' then
			return '18'
		end
	elseif state == 'NS' and type == 'Hwy' then
		return '18'
	elseif state == 'ON' then
		if type == 'ON' or type == 'Hwy' or type == 'Highway' then
			local route = args.route
			if route == '58A' or tonumber(route) < 500 then
				return 'x22'
			else
				return 'x21'
			end
		elseif type == 'QEW' then
			return 'x22'
		else
			local countyTypes = {CH = true, CR = true, DR = true, KLR = true, RH = true, RR = true}
			if countyTypes[type] then
				return 'x21'
			end
		end
	elseif state == 'QC' then
		if type == 'QC' or type == 'Route' or type == 'A' or type == 'Autoroute' then
			return '18'
		end
	elseif state == 'SK' then
		if type == 'Hwy' or type == 'SK' then
			return 'x25'
		end
	end
 
	return 'x20'
end
 
function shield(args)
	if args.noshield then return '' end
	local size = size(args)
	local shield = parser(args, 'shield')
	if not shield or shield == '' then return '' end
	return string.format("[[File:%s|%spx|link=|alt=]]", shield, size)
end

-- Links/abbreviations
function link(args)
	local nolink = args.nolink
	local abbr = parser(args, 'abbr')
	if nolink then
		return abbr
	else
		local link = parser(args, 'link')
		if not link or link == '' then
			return abbr
		else
			return mw.ustring.format("[[%s|%s]]", link, abbr)
		end
	end
end


function parseArgs(args)
	local country = args.country
	local state = args.state or args.province
	local params = {'denom', 'county', 'township', 'dab', 'nolink', 'noshield', 'to', 'dir', 'name'}
	local routeArgs = {}
	local routeCount = 1
	while true do
		local routeType = args[routeCount * 2 - 1]
		if not routeType then break end
		local route = {type = routeType, route = args[routeCount * 2]}
		for _,v in pairs(params) do
			route[v] = args[v .. routeCount]
		end
		route.country = country
		route.state = state
		insert(routeArgs, route)
		routeCount = routeCount + 1
	end
	return routeArgs
end

function p._jct(args)
	local routes = parseArgs(args)
	local shields = {}
	local links = {}
	for _,route in ipairs(routes) do
		insert(shields, shield(route))
		insert(links, link(route))
	end
	local shieldText = table.concat(shields)
	local linkText = table.concat(links, " / ")
	return shieldText .. ' ' .. linkText
end

function p.jct(frame)
	local args = getArgs(frame)
	return p._jct(args)
end

return p