Jump to content

Module:Jct

From Simple English Wikipedia, the free encyclopedia
Revision as of 16:43, 7 February 2014 by Happy5214 (talk | changes) (Default extra to empty string)

This module is used to implement Template:Jct.

Usage

{{#invoke:Jct|jct}}

Tracking/maintenance category


local p = {}

local concat = table.concat
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
 
local 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
local 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

local function completeLink(args, num)
	local actualLink = link(args)
	local isTo = args.to
	local prefix
	if num == 1 then
		if isTo then
			prefix = "To "
		else
			prefix = ''
		end
	else
		if isTo then
			prefix = " to "
		else
			prefix = " / "
		end
	end
	local suffix = {}
	local dir = args.dir
	if dir then
		insert(suffix, ' ' .. dir)
	end
	local name = args.name
	if name then
		insert(suffix, mw.ustring.format(' (%s)', name))
	end
	return prefix .. actualLink .. concat(suffix)
end

local function extra(args)
	local extraTypes = {rail = "[[File:Rail Sign.svg|20px|alt=|link=]]",
	                    ["light-rail"] = "[[File:Light Rail Sign.svg|20px|alt=|link=]]",
	                    bus = "[[File:Bus Sign.svg|20px|alt=|link=]]",
	                    ferry = "[[File:Ferry Sign.svg|20px|alt=|link=]]",
	                    hospital = "[[File:Hospital sign.svg|20px|alt=|link=]]",
	                    airport = "[[File:Airport Sign.svg|20px|alt=|link=]]"}
	return extraTypes[string.lower(args.extra or '')] or ''
end

local 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 extra = extra(args)
	local shields = {}
	local links = {}
	for num,route in ipairs(routes) do
		insert(shields, shield(route))
		insert(links, completeLink(route, num))
	end
	local shieldText = concat(shields)
	local linkText = concat(links)
	
	local cities = ''
	if args.city1 or args.location1 then
		local cityModule = require "Module:Jct/city/sandbox"
		cities = cityModule.city(args)
	end
	
	local roadStr = ''
	local road = args.road
	if road then
		if args.toroad then
			roadStr = ' to ' .. road
		else
			roadStr = ' / ' .. road
		end
	end
	
	return shieldText .. extra .. ' ' .. linkText .. roadStr .. cities
end

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

return p