local p =
-- Change to "" upon deployment.local moduleSuffix = ""
local parserModuleName = "Module:Road data/parser" .. moduleSuffixlocal statenameModuleName = "Module:Jct/statename" .. moduleSuffix -- TODO transitionlocal cityModuleName = "Module:Jct/city" .. moduleSuffix
local concat = table.concatlocal insert = table.insertlocal format = mw.ustring.formatlocal trim = mw.text.trim
local parserModule = require(parserModuleName)local parser = parserModule.parserlocal util = require("Module:Road data/util")
-- Shieldslocal defaultShieldSize = 17
local function addContextBanner(route, name, suffix, bannerSpec) local bannerModule = 'Module:Road data/banners/' .. string.upper(route.country) local shieldfield = name .. 'shield' local shield = parser(route, shieldfield) if shield
nil then suffix = parser(route, 'shield', 'suffix', bannerModule) end if suffix and suffix ~= then shield = shield .. " " .. suffix end shield = shield .. ".svg" end end if shield and shield ~= then local shieldSize = defaultShieldSize -- Add banner plate. insert(bannerSpec,) endend
local function bannerSpec(banner, bannerSize, bannerSuffix, route) local banners = if type(banner)
if route.dir then addContextBanner(route, 'dir', bannerSuffix, banners) end if route.to then addContextBanner(route, 'to', bannerSuffix, banners) end
return bannersend
local function shieldSpec(route, mainShield) local shieldSpec =
local shield if mainShield then shield = parser(route, "shieldmain") end if not shield then shield = parser(route, 'shield') or end if shield
local shieldsize = defaultShieldSize local banner = parser(route, 'banner') or local bannersize = defaultShieldSize local bannersuffix = parser(route, 'bannersuffix')
local bannerIsNotTable = type(banner) ~= "table" local bannersizeIsNotTable = type(bannersize) ~= "table" local bannersuffixIsNotTable = type(bannersuffix) ~= "table"
if type(shield)
"" then size = nil end -- banner.all describes banners that apply to all multiple shields. local shieldBanner = bannerIsNotTable and banner or (banner[i] or banner.all or) -- Banner size is default if the corresponding entry -- in bannerSize table is not set. local shieldBannerSize = bannersizeIsNotTable and bannersize or (bannersize[i] or bannersize.all or defaultShieldSize) local shieldBannerSuffix = bannersuffix and (bannersuffixIsNotTable and bannersuffix or bannersuffix[i]) insert(shieldSpec,) end elseif shield ~= then if shieldsize
return shieldSpecend
local missingShields
local shieldExistsCache =
-- Return up to two booleans.-- The first boolean is false if `shield` does not exist, and true otherwise.-- If the first boolean is true, the second boolean is true if the shield is-- landscape (width >= height), and false otherwise.local function shieldExists(shield) local result = shieldExistsCache[shield] if result
local function render(shieldEntry, scale, showLink) local shield = shieldEntry.shield local banners = shieldEntry.banners local exists, landscape = shieldExists(shield[1]) if not exists then return end
local size if shield[2] then local width, height = mw.ustring.match(shield[2], "(%d*)x?(%d*)") width = tonumber(width) height = tonumber(height) local sizeparts = if width then insert(sizeparts, format("%d", width * scale)) end if height then insert(sizeparts, format("x%d", height * scale)) end size = concat(sizeparts) else size = format("%s%d", landscape, defaultShieldSize * scale) end local link = showLink and "" or "|link=" local shieldCode = format("", shield[1], size, link, shield[1]) if not banners[1] then return shieldCode end
for _,banner in ipairs(banners) do shieldCode = format("
%s", banner[1], defaultShieldSize, shieldCode) end return '
function p.shield(route, scale, showLink, mainShield) missingShields =
scale = 1
local rendered = for _,entry in ipairs(shieldSpec(route, mainShield)) do insert(rendered, render(entry, scale, showLink)) end return concat(rendered)end
function p.link(route) local abbr, errMsg = parser(route, 'abbr') if not abbr then end if route.nolink then return abbr, abbr end
local link = parser(route, 'link') or if link
return format("%s", link, abbr), abbrend
local function stateName(args) -- TODO transition local data = mw.loadData(statenameModuleName) local abbr = args.state or args.province local countryData = data[args.country] return countryData and countryData[abbr]end
-- Links/abbreviationslocal function routeText(route, frame) local link local type = route.type if not type or type
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 = routeText local parenText = name
return frame:expandTemplate else return routeText endend
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 = 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 = 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 routesend
local function prefix(to, num) if to and to ~= then return num
1 and or ' / 'end
function p._jctrdt(args, frame) local routes = parseArgs(args) local shields = local links = frame = frame or mw.getCurrentFrame for num,route in ipairs(routes) do if not (args.noshield or route.noshield) then local shield = p.shield(route) insert(shields, shield) end local prefix = prefix(route.to, num) if prefix ~= then insert(links, prefix) end insert(links, routeText(route, frame)) end local graphics = concat(shields) .. ' ' local linkText = concat(links) 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 = ' ' end local cityModule = require(cityModuleName) cities = citiesPrefix .. cityModule.city(args) end
return graphics .. linkText .. citiesend
function p.jctrdt(frame) -- Import module function to work with passed arguments local getArgs = require('Module:Arguments').getArgs local args = getArgs(frame,) return p._jctrdt(args, frame)end
return p