Jump to content

Module:Infobox road/route/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fredddie (talk | contribs) at 01:52, 2 October 2021 (starting over from Jct's module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local SUFFIX = ""
-- Change to main module upon deployment
local parserModuleName = "Module:Road data/parser" .. SUFFIX

local concat = table.concat
local insert = table.insert
local format = mw.ustring.format
local roadDataModule = require("Module:Road data" .. SUFFIX)

-- Links/abbreviations
local function routeText(route, jctname, frame)
	local link
	local type = route.type
	if not type or type == '' then
		link = route.route
	else
		link = roadDataModule.link(route)
	end

	local dir = route.dir and ' ' .. string.lower(route.dir) or ''
	local routeText = link .. dir

	local name = route.name
	if name and name ~= '' then
		local mainText = jctname and name or routeText
		local parenText = jctname and routeText or name
		
		return format('%s (%s)', mainText, parenText)
	else
		return routeText
	end
end

local function parseArgs(args)
	local state = args.state or args.province or ''
	args.state = state
	local country
	if args.country and args.country ~= '' then
		country = string.upper(args.country)
	else
		local countryModule = mw.loadData("Module:Road data/countrymask")
		country = countryModule[state] or 'UNK'
	end
	args.country = country

	local params = {'denom', 'county', 'township', 'dab', 'nolink', 'noshield', 'to', 'dir', 'name'}
	local routes = {}
	local routeCount = 1

	return routes
end

local function addErrorMsg(catCode, msg, errorMsg)
	errorMsg.code = errorMsg.code or catCode
	insert(errorMsg, format('<span style="display: none;">Module:Infobox road/route/sandbox %s</span>', msg))
end

function p._jct(args, frame)
	local routes = parseArgs(args)
	local shields = {}
	local links = {}
	local allMissingShields = {}
	local typeErr = false
	frame = frame or mw.getCurrentFrame()
	for num,route in ipairs(routes) do
		if not (args.noshield or route.noshield) then
			local shield, missingShields = roadDataModule.shield(route)
			insert(shields, shield)
			if missingShields[1] then
				insert(allMissingShields, concat(missingShields, ' / '))
			end
		end
		insert(links, routeText(route, args.jctname, frame))
		typeErr = typeErr or route.typeerror or false
	end
	local graphics = concat(shields) .. extra(args) .. ' '
	local linkText = concat(links)

	local errorMsg = {}
	-- Errors must be reported by the level of severity, most severe first.
	if typeErr then
		-- Report invalid type errors.
		addErrorMsg("§", 'error: Invalid route type', errorMsg)
	end
	if #allMissingShields > 0 then
		-- Report missing shield error.
		-- shieldExists() would have populated missingShields if shields are missing.
		addErrorMsg("¶", 'error: Missing route marker graphics: ' .. concat(allMissingShields, ' / '), errorMsg)
	end
	if #errorMsg > 0 then
		local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
		-- Add a category for the first, most severe error.
		insert(errorMsg, format('[[Category:Jct template errors|%s %s]]', errorMsg.code, page))
		errorMsg = concat(errorMsg)
	else
		errorMsg = ''
	end

	return graphics .. linkText .. errorMsg
end

function p.jct(frame)
	-- Import module function to work with passed arguments
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame, {removeBlanks = false})
	return p._jct(args, frame)
end

return p