Jump to content

Module:Jct/sandbox 2: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
fix
synch to main sandbox
Line 1: Line 1:
local p = {}
local p = {}


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


Line 11: Line 10:
local format = mw.ustring.format
local format = mw.ustring.format
local roadDataModule = require("Module:Road data" .. SUFFIX)
local roadDataModule = require("Module:Road data" .. SUFFIX)

local rdt


-- Links/abbreviations
-- Links/abbreviations
Line 44: Line 41:
local function extra(args)
local function extra(args)
local extraTypes = mw.loadData('Module:Road data/extra')
local extraTypes = mw.loadData('Module:Road data/extra')
local extraIcon = extraTypes[string.lower(args.extra or '')]
if not extraIcon then return '' end
local size = require("Module:Road data/size").size({style = 'jct'})
local countryIcon = extraIcon[args.country] or extraIcon.default
if type(countryIcon) == 'table' then
local localIcon = countryIcon[args.state] or countryIcon.default
return string.format("[[File:%s|%s|alt=|link=]]", localIcon, size)
else
return string.format("[[File:%s|%s|alt=|link=]]", countryIcon, size)
end
end

-- Shields
local function size(args)
local orientation = parser(args, 'orientation')
if rdt then
return mw.ustring.match(rdt, '^(%d+)$' .. "px") or 'x17px'
elseif orientation == "upright" then
return sizeModule
else return "x" .. sizeModule
end
end
local function shield(args, frame)
if args.noshield then return '' end
local firstSize, secondSize = size(args)
local shield, second = parser(args, 'shield')
if not shield or shield == '' then
return ''
elseif type(shield) == 'table' then
shield, second = shield[1], shield[2]
end
local function render(shield, size)
if frame:callParserFunction('#ifexist', 'Media:' .. shield, '1') ~= '' then
return string.format("[[File:%s|%s|link=|alt=]]", shield, firstSize)
else
args.shielderr = true
local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
return mw.ustring.format("[[Category:Jct template errors|$ %s]]", page)
end
end
local rendered = render(shield, firstSize)
if second and type(second) == 'string' then
local size = secondSize or firstSize
rendered = rendered .. render(second, size)
end
return rendered
end

local function banners(routes)
local format = string.format
local firstRun = {}
local hasBanner = false
for k,v in ipairs(routes) do
local banner
if v.shield == '' or v.shielderr then
banner = false
else
banner = parser(v, 'banner') or ''
if banner and banner ~= '' then
hasBanner = true
end
end
insert(firstRun, banner)
end
if not hasBanner then return '' end
local secondRun = {}
for k,v in ipairs(routes) do
local bannerFile = firstRun[k]
if not bannerFile then
elseif bannerFile == '' then
local widthCode = parser(v, 'width') or 'square'
if type(widthCode) == 'number' then
insert(secondRun, "[[File:No image wide.svg|" .. tostring(widthCode) .. "px|bottom|link=|alt=]]")
elseif widthCode == 'square' then
insert(secondRun, "[[File:No image wide.svg|24px|bottom|link=|alt=]]")
elseif widthCode == 'expand' then
local route = v.route
local width = (#route >= 3) and '30' or '24'
insert(secondRun, format("[[File:No image wide.svg|%spx|bottom|link=|alt=]]", width))
elseif widthCode == 'US1926' then
insert(secondRun, "[[File:No image wide.svg|24px|bottom|link=|alt=]]")
elseif widthCode == 'wide' then
insert(secondRun, "[[File:No image wide.svg|30px|bottom|link=|alt=]]")
end
else
local widthCode = parser(v, 'width') or 'square'
if widthCode == 'square' then
insert(secondRun, format("[[File:%s|24px|bottom|link=|alt=]]", bannerFile))
elseif widthCode == 'expand' then
if (v.state == 'CA') then
local route = v.route
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|2px|bottom|link=|alt=]][[File:%s|24px|bottom|link=|alt=]][[File:No image.svg|2px|bottom|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|24px|bottom|link=|alt=]]", bannerFile))
end
else local route = v.route
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|3px|bottom|link=|alt=]][[File:%s|24px|bottom|link=|alt=]][[File:No image.svg|3px|bottom|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|24px|bottom|link=|alt=]]", bannerFile))
end
end
elseif widthCode == 'wide' then
insert(secondRun, format("[[File:No image.svg|3px|bottom|link=|alt=]][[File:%s|24px|bottom|link=|alt=]][[File:No image.svg|3px|bottom|link=|alt=]]", bannerFile))
elseif widthCode == 'MOSupp' then
local route = v.route
if #route >= 2 then
insert(secondRun, format("[[File:No image.svg|3px|bottom|link=|alt=]][[File:%s|24px|bottom|link=|alt=]][[File:No image.svg|3px|bottom|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|24px|bottom|link=|alt=]]", bannerFile))
end
elseif widthCode == 'US1926' then
insert(secondRun, format("[[File:%s|24px|bottom|link=|alt=]]", bannerFile))
end
end
end
return concat(secondRun) .. '<br>'
end

local function extra(args)
local extraTypes = mw.loadData('Module:Road data/extra/sandbox')
local extraIcon = extraTypes[string.lower(args.extra or '')]
local extraIcon = extraTypes[string.lower(args.extra or '')]
if not extraIcon then return '' end
if not extraIcon then return '' end
Line 182: Line 54:


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

local params = {'denom', 'county', 'township', 'dab', 'nolink', 'noshield', 'to', 'dir', 'name'}
local params = {'denom', 'county', 'township', 'dab', 'nolink', 'noshield', 'to', 'dir', 'name'}
local routeArgs = {}
local routes = {}
local routeCount = 1
local routeCount = 1
local seenTo = false
while true do
while true do
local routeType = args[routeCount * 2 - 1]
local routeType = args[routeCount * 2 - 1]
Line 201: Line 75:
for _,v in pairs(params) do
for _,v in pairs(params) do
route[v] = args[v .. routeCount]
route[v] = args[v .. routeCount]
end
if args.nolink then
route.nolink = args.nolink
end
end
route.country = country
route.country = country
route.state = state
route.state = state

insert(routeArgs, route)
-- Set the first .to to true.
-- Set all following .to to ''.
if seenTo then
if route.to then
-- Report duplicate to flag.
route.toerror = true
end
route.to = ''
elseif route.to then
route.to = true
seenTo = true
end

route.rdt = args.rdt
insert(routes, route)
routeCount = routeCount + 1
routeCount = routeCount + 1
end
end
return routeArgs
return routes
end

local function prefix(to, num)
if to and to ~= '' then
return num == 1 and 'To ' or ' to '
end
return num == 1 and '' or '&nbsp;/ '
end

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


function p._jct(args, frame)
function p._jct(args, frame)
rdt = args.rdt
local routes = parseArgs(args)
local routes = parseArgs(args)
local extra = extra(args)
local shields = {}
local shields = {}
local links = {}
local links = {}
local allMissingShields = {}
local typeErr = false
local toErr = false
frame = frame or mw.getCurrentFrame()
frame = frame or mw.getCurrentFrame()
for num,route in ipairs(routes) do
for num,route in ipairs(routes) do
if not (args.noshield or route.noshield) then
local routeShield = shield(route, frame)
local shield, missingShields = roadDataModule.shield(route)
insert(shields, routeShield)
route.shield = routeShield
insert(shields, shield)
if args.jctname then
if missingShields[1] then
insert(links, namedLink(route, num))
insert(allMissingShields, concat(missingShields, ' / '))
else
end
insert(links, completeLink(route, num))
end
end
local prefix = prefix(route.to, num)
if prefix ~= '' then insert(links, prefix) end
insert(links, routeText(route, args.jctname, frame))
typeErr = typeErr or route.typeerror or false
toErr = toErr or route.toerror or false
end
end
local bannerText = banners(routes)
local graphics = concat(shields) .. extra(args) .. ' '
local shieldText = concat(shields)
local linkText = concat(links)
local linkText = concat(links)
local graphics = (not(args.noshield) and (bannerText .. shieldText) or '') .. extra .. ' '
local roadStr = args.road and (args.toroad and ' to ' or '&nbsp;/ ') .. args.road or ''
local cities = ''
local cities = ''
if args.city1 or args.location1 then
if args.city1 or args.location1 then
local citiesPrefix
local cityModule = require("Module:Jct/city" .. SANDBOX_SUFFIX)
if args.citiesprefix then
cities = cityModule.city(args)
citiesPrefix = args.citiesprefix ~= '' and format(" %s ", args.citiesprefix) or ''
end
local roadStr = ''
local road = args.road
if road then
if args.toroad then
roadStr = ' to ' .. road
else
else
roadStr = ' / ' .. road
citiesPrefix = '&nbsp;'
end
end
local cityModule = require(cityModuleName)
cities = citiesPrefix .. cityModule.city(args)
end
end

local errorMsg = {}
local output = graphics .. linkText .. roadStr .. cities
-- Errors must be reported by the level of severity, most severe first.
return mw.text.trim(output)
if typeErr then
-- Report invalid type errors.
addErrorMsg("2", 'error: Invalid route type', errorMsg)
end
if #allMissingShields > 0 then
-- Report missing shield error.
-- shieldExists() would have populated missingShields if shields are missing.
addErrorMsg("1", 'error: Missing route marker graphics: ' .. concat(allMissingShields, ' / '), errorMsg)
end
if toErr then
-- Report invalid to errors.
addErrorMsg("3", 'error: Invalid "to" argument', errorMsg)
end
if args.road then
-- Report deprecated "road" warning.
addErrorMsg("W", 'warning: "road" parameter is deprecated', 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 .. roadStr .. cities
end
end



Revision as of 05:53, 21 July 2021

local p = {}

local SUFFIX = ""
-- Change to main module upon deployment
local parserModuleName = "Module:Road data/parser" .. SUFFIX
local cityModuleName = "Module:Jct/city" .. 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

		if route.rdt then
			return '\n' .. frame:expandTemplate{ title = 'BSsplit', args = {mainText, parenText, align = 'left', valign = 'middle' } }
		else
			return format('%s (%s)', mainText, parenText)
		end
	else
		return routeText
	end
end

local function extra(args)
	local extraTypes = mw.loadData('Module:Road data/extra')
	local extraIcon = extraTypes[string.lower(args.extra or '')]
	if not extraIcon then return '' end
	local size = require("Module:Road data/size").size({style = 'jct'})
	local countryIcon = extraIcon[args.country] or extraIcon.default
	if type(countryIcon) == 'table' then
		local localIcon = countryIcon[args.state] or countryIcon.default
		return string.format("[[File:%s|%s|alt=|link=]]", localIcon, size)
	else
		return string.format("[[File:%s|%s|alt=|link=]]", countryIcon, size)
	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
	local seenTo = false
	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

		-- Set the first .to to true.
		-- Set all following .to to ''.
		if seenTo then
			if route.to then
				-- Report duplicate to flag.
				route.toerror = true
			end
			route.to = ''
		elseif route.to then
			route.to = true
			seenTo = true
		end

		route.rdt = args.rdt
		insert(routes, route)
		routeCount = routeCount + 1
	end
	return routes
end

local function prefix(to, num)
	if to and to ~= '' then
		return num == 1 and 'To ' or ' to '
	end
	return num == 1 and '' or '&nbsp;/ '
end

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

function p._jct(args, frame)
	local routes = parseArgs(args)
	local shields = {}
	local links = {}
	local allMissingShields = {}
	local typeErr = false
	local toErr = 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
		local prefix = prefix(route.to, num)
		if prefix ~= '' then insert(links, prefix) end
		insert(links, routeText(route, args.jctname, frame))
		typeErr = typeErr or route.typeerror or false
		toErr = toErr or route.toerror or false
	end
	local graphics = concat(shields) .. extra(args) .. ' '
	local linkText = concat(links)
	local roadStr = args.road and (args.toroad and ' to ' or '&nbsp;/ ') .. args.road or ''
	local cities = ''
	if args.city1 or args.location1 then
		local citiesPrefix
		if args.citiesprefix then
			citiesPrefix = args.citiesprefix ~= '' and format(" %s ", args.citiesprefix) or ''
		else
			citiesPrefix = '&nbsp;'
		end
		local cityModule = require(cityModuleName)
		cities = citiesPrefix .. cityModule.city(args)
	end

	local errorMsg = {}
	-- Errors must be reported by the level of severity, most severe first.
	if typeErr then
		-- Report invalid type errors.
		addErrorMsg("2", 'error: Invalid route type', errorMsg)
	end
	if #allMissingShields > 0 then
		-- Report missing shield error.
		-- shieldExists() would have populated missingShields if shields are missing.
		addErrorMsg("1", 'error: Missing route marker graphics: ' .. concat(allMissingShields, ' / '), errorMsg)
	end
	if toErr then
		-- Report invalid to errors.
		addErrorMsg("3", 'error: Invalid "to" argument', errorMsg)
	end
	if args.road then
		-- Report deprecated "road" warning.
		addErrorMsg("W", 'warning: "road" parameter is deprecated', 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 .. roadStr .. cities
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