Jump to content

Module:Jct/sandbox 2

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fredddie (talk | contribs) at 05:51, 21 July 2021 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

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

local rdt

-- 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

-- 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 '')]
	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
	local country
	if args.country then
		country = string.upper(args.country)
		args.country = country
	else
		local countryModule = mw.loadData("Module:Road data/countrymask")
		country = countryModule[state] or 'UNK'
		args.country = country
	end
	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
		if args.nolink then
			route.nolink = args.nolink
		end
		route.country = country
		route.state = state
		insert(routeArgs, route)
		routeCount = routeCount + 1
	end
	return routeArgs
end

function p._jct(args, frame)
	rdt = args.rdt
	local routes = parseArgs(args)
	local extra = extra(args)
	local shields = {}
	local links = {}
	frame = frame or mw.getCurrentFrame()
	for num,route in ipairs(routes) do
		local routeShield = shield(route, frame)
		insert(shields, routeShield)
		route.shield = routeShield
		if args.jctname then
			insert(links, namedLink(route, num))
		else
			insert(links, completeLink(route, num))
		end
	end
	local bannerText = banners(routes)
	local shieldText = concat(shields)
	local linkText = concat(links)
	local graphics = (not(args.noshield) and (bannerText .. shieldText) or '') .. extra .. ' '
	
	local cities = ''
	if args.city1 or args.location1 then
		local cityModule = require("Module:Jct/city" .. SANDBOX_SUFFIX)
		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
	
	local output = graphics .. linkText .. roadStr .. cities
	return mw.text.trim(output)
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