Jump to content

Module:Interprovincial highway

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 18:12, 14 August 2024 (Still show label for termini). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('strict')

local p = {}
local parserModule = require('Module:Road data/parser')
local parser = parserModule.parser

local function makeRoute(route, routeType, routeLoc, currProvince)
	local out = ''
	
	local parserArgs = {
		route = route,
		type = routeType,
		province = routeLoc
	}
	
	local shield = parser(parserArgs, 'shieldmain') or parser(parserArgs, 'shield') or ''
	local label = (routeLoc ~= currProvince and (routeLoc .. ' ') or '')
		.. (parser(parserArgs, 'name') or parser(parserArgs, 'abbr') or '')
	local link = parser(parserArgs, 'link')
	local alt = label .. ' marker'
	
	if type(shield) == 'table' then
		shield = shield[1]
	end
	
	if shield and shield ~= '' then
		out = out ..  string.format('[[File:%s|15px|alt=%s]]', shield, alt) .. ' '
	end
	
	if not link or link == '' then
		out = out  .. label
	else
		out = out  .. string.format('[[%s|%s]]', link, label)
	end
	
	if out ~= '' then
		out = "'''" .. out .. "'''"
	end
	
	return out
end

-- Generate a single route link.
-- 
-- Current frame arguments:
--   1 - The route number.
--   type - The route type.
--   province - The route province.
--   curr_province - The current province. Defaults to <province>.
-- Parent frame arguments: None
function p.route(frame)
	return makeRoute(
		frame.args[1],
		frame.args.type,
		frame.args.province,
		frame.args.curr_province or frame.args.province
	)
end

-- Generate prev/next navigation links.
-- 
-- Current frame arguments:
--   1 - The prefix of the parent frame parameters to process.
--   label - The label for the output. Shown as "<label> route(s)".
--   curr_province - The current province.
-- Parent frame arguments:
--    <prefix><number?> - The route number.
--    <prefix><number?>_type - The route type.
--    <prefix><number?>_type - The route province.
function p.nav(frame)
	local prefix = frame.args[1]
	local label = frame.args.label
	local currProvince = frame.args.curr_province
	local templateArgs = frame:getParent().args

	local index = 1
	local out = ''
	
	while templateArgs[prefix .. index] or (index == 1 and templateArgs[prefix ]) do
		local route = templateArgs[prefix .. index]
			or (index == 1 and templateArgs[prefix] or '')

		local routeType = templateArgs[prefix .. index .. '_type']
			or (index == 1 and templateArgs[prefix .. '_type'] or '')

		local routeLoc = templateArgs[prefix .. index .. '_province']
			or (index == 1 and templateArgs[prefix .. '_province'] or '')
		
		if index ~= 1 then
			out = out .. '<hr>'
		end
		
		out = out .. makeRoute(route, routeType, routeLoc, currProvince)
		
		index = index + 1
	end
	
	if out == '' then
		out = "'''Terminus'''"
	end
	
	return label .. ' ' .. (index - 1 > 1 and 'routes' or 'route') .. '<br>' .. out
end

-- Display title for the given type
--
-- Current frame arguments:
--    1 - The type to return the title for
-- Parent frame arguments: None
function p.type(frame)
	local data = mw.loadData('Module:Sandbox/BrandonXLF/Module:Interprovincial highway/data')
	return data.types[frame.args[1]] or require('Module:Error').error{'Unknown |type='}
end

-- Show the supported types in a table.
--
-- Current frame arguments: None
-- Parent frame arguments: None
function p.supported(frame)
	local data = mw.loadData('Module:Sandbox/BrandonXLF/Module:Interprovincial highway/data')
	local post = 'Types retrieved from [[Module:Interprovincial highway/data]] ('
		.. frame:expandTemplate{ title = 'edit', args = { 'Module:Interprovincial highway/data' } } .. ').'

	local tableEl = mw.html.create('table'):addClass('wikitable')
	
	local headerRow = tableEl:tag('tr')
	headerRow:tag('th'):wikitext('Value')
	headerRow:tag('th'):wikitext('Title')

	for name, title in pairs(data.types) do
		local row = tableEl:tag('tr')
		row:tag('td'):tag('code'):wikitext(name)
		row:tag('td'):wikitext("'''" .. title .. "'''")
	end

	return tostring(tableEl) .. post
end

return p