Module:Jct: Difference between revisions

From IxWiki
Jump to navigation Jump to search
mNo edit summary
Tag: Reverted
m (Reverted edits by Insui (talk) to last revision by [[User:>Fredddie|>Fredddie]])
Tags: Rollback Reverted
Line 1: Line 1:
local p = {}
local p = {}
-- Change to "" upon deployment.
local moduleSuffix = ""


local parserModuleName = "Module:Road data/parser" .. moduleSuffix
local SANDBOX = false
local statenameModuleName = "Module:Jct/statename" .. moduleSuffix -- TODO transition
local SANDBOX_SUFFIX = SANDBOX and '/sandbox' or ''
local cityModuleName = "Module:Jct/city" .. moduleSuffix


local concat = table.concat
local concat = table.concat
local insert = table.insert
local insert = table.insert
local format = mw.ustring.format
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments
local trim = mw.text.trim
local parserModule = require("Module:Road data/parser" .. SANDBOX_SUFFIX)
 
local parserModule = require(parserModuleName)
local parser = parserModule.parser
local parser = parserModule.parser
local util = require("Module:Road data/util")


-- Shields
-- Shields
local defaultShieldSize = 24
local rdt


local function addContextBanner(route, name, suffix, bannerSpec)
local function size(args)
local bannerModule = 'Module:Road data/banners/' .. string.upper(route.country)
local country = args.country
local shieldfield = name .. 'shield'
local state = args.state or args.province or ''
local shield = parser(route, shieldfield)
local type = args.type
if shield == nil then
if rdt then
-- This route type does not define shield.
return 'x17'
-- Find shield in the default banner table.
elseif state == 'FL' then
shield = parser(route, 'shield', name, bannerModule)
if type == 'Toll' or type == 'FLTP' or type == 'HEFT' then
if shield and shield ~= '' then
return '20'
if suffix == nil then
elseif type == 'Both' then
suffix = parser(route, 'shield', 'suffix', bannerModule)
return '20', 'x20'
end
elseif state == 'NY' then
if type == 'NY 1927' or type == 'NY 1948' or (type == 'Parkway' and args.route == "Robert Moses") or (type == 'CR' and args.county == 'Erie') then
return '20'
end
elseif state == 'AB' then
if type == 'AB' or type == 'Hwy' or type == '2ndHwy' or type == 'TCH' then
return '18'
end
elseif state == 'NS' and type == 'Hwy' or type == 'TCH' then
return '18'
elseif state == 'ON' then
if type == 'ON' or type == 'Hwy' or type == 'Highway' or type == 'QEW' then
return '24x22'
elseif type == 'KLR' then
return '21'
else
local countyTypes = {CH = true, RH = true, District = true, Regional = true, County = true, Municipal = true}
if countyTypes[type] then
return '19'
end
end
if suffix and suffix ~= '' then
shield = shield .. " " .. suffix
end
shield = shield .. ".svg"
end
end
elseif state == 'QC' then
if type == 'QC' or type == 'Route' or type == 'A' or type == 'Autoroute' or type == 'TCH' or type == 'ON' then
return '18'
end
elseif state == 'SK' then
if type == 'Hwy' or type == 'SK' then
return 'x25'
end
elseif country == 'MEX' then
return 'x25'
elseif country == 'FRA' then
return 'x18'
elseif country == 'TUR' then
return 'x15'
end
end
if shield and shield ~= '' then
local shieldSize = defaultShieldSize
return 'x20'
-- Add banner plate.
end
insert(bannerSpec, {shield, shieldSize})
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
end
end
local function render(shield, size)
 
if frame:callParserFunction('#ifexist', 'Media:' .. shield, '1') ~= '' then
local function bannerSpec(banner, bannerSize, bannerSuffix, route)
return string.format("[[File:%s|%spx|link=|alt=]]", shield, size)
local banners = {}
else
if type(banner) == "table" then
args.shielderr = true
local bannerSizeIsNotTable = type(bannerSize) ~= "table"
local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
for i,filename in ipairs(banner) do
return mw.ustring.format("[[Category:Jct template errors|1 %s]]", page)
local bannersize = bannerSizeIsNotTable and bannerSize or bannerSize[i] or defaultShieldSize
insert(banners, {filename, bannersize})
end
end
elseif banner ~= '' then
insert(banners, {banner, bannerSize})
end
if route.dir then
addContextBanner(route, 'dir', bannerSuffix, banners)
end
end
if route.to then
local rendered = render(shield, firstSize)
addContextBanner(route, 'to', bannerSuffix, banners)
if second and type(second) == 'string' then
local size = secondSize or firstSize
rendered = rendered .. render(second, size)
end
end
 
return rendered
return banners
end
end


local function shieldSpec(route, mainShield, shieldList)
-- Links/abbreviations
local shieldSpec = {}
local function link(args)
 
local nolink = args.nolink
local shield
local abbr = parser(args, 'abbr')
local shieldto = parser(route, 'shieldto')
if nolink then
return abbr
if route.to then
if not shield then shield = shieldto or parser(route, 'shield') or '' end
else
else
if not shield then shield = parser(route, 'shield') or '' end
local link = parser(args, 'link')
end
if not link or link == '' then
if shield == '' then return shieldSpec end
return abbr
local orientation = parser(route, 'orientation')
else
 
return mw.ustring.format("<span class=\"nowrap\">[[%s|%s]]</span>", link, abbr)
local function size(route)
if orientation == "upright" then
return defaultShieldSize
else return "x" .. defaultShieldSize
end
end
end
end
end
local shieldsize = size(route)
local banner = parser(route, 'banner') or {}
local bannersize = defaultShieldSize
local bannersuffix = parser(route, 'bannersuffix')


local bannerIsNotTable = type(banner) ~= "table"
local function completeLink(args, num)
local bannersizeIsNotTable = type(bannersize) ~= "table"
local actualLink = link(args)
local bannersuffixIsNotTable = type(bannersuffix) ~= "table"
if not actualLink then
 
local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
if type(shield) == "table" then
actualLink = string.format("<span class=\"error\">Invalid type: %s</span>[[Category:Jct template errors|2 %s]]", args.type, page)
for i,filename in ipairs(shield) do
local size = shieldsize or shieldsize[i]
if size == "" 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, {
shield = {filename, size},
banners = bannerSpec(shieldBanner, shieldBannerSize, shieldBannerSuffix, route)
})
end
elseif shield ~= '' then
if shieldsize == "" then shieldsize = nil end
insert(shieldSpec, {
shield = {shield, shieldsize},
banners = bannerSpec(banner, bannersize,  bannersuffix, route)
})
end
end
 
local isTo = args.to
return shieldSpec
local prefix
end
if num == 1 then
 
if isTo then
local missingShields
prefix = "To "
 
else
local shieldExistsCache = {}
prefix = ''
 
local function render(shieldEntry, scale, showLink)
local shield = shieldEntry.shield
local banners = shieldEntry.banners
 
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
end
if height then
else
insert(sizeparts, format("x%d", height * scale))
if isTo then
prefix = " to "
else
prefix = " / "
end
end
size = concat(sizeparts)
else
size = format("%s%d", landscape and "x" or "", defaultShieldSize * scale)
end
end
local shieldCode = format("[[File:%s|%spx|link=|alt=]]", shield[1], size)
local suffix = {}
if not banners[1] then return shieldCode end
local dir = args.dir
 
if dir then
for _,banner in ipairs(banners) do
insert(suffix, ' ' .. string.lower(dir))
shieldCode = format("[[File:%s|%spx|link=|alt=]]<br>%s",
banner[1],
defaultShieldSize,
shieldCode)
end
end
return '<span style="display: inline-block; vertical-align: baseline; line-height: 0; text-align: center;">' .. shieldCode .. '</span>'
local name = args.name
end
if name then
 
insert(suffix, mw.ustring.format(' (%s)', name))
function p.shield(route, scale, showLink, mainShield, shieldList)
missingShields = {}
 
scale = scale or 1
 
local rendered = {}
for _,entry in ipairs(shieldSpec(route, mainShield, shieldList)) do
insert(rendered, render(entry, scale, showLink))
end
end
return concat(rendered), missingShields
return prefix .. actualLink .. concat(suffix)
end
end


-- Links/abbreviations
local function namedLink(args, num)
function p.link(route)
local actualLink = link(args)
local nolink = route.nolink
local name = args.name or ''
local abbr = parser(route, 'abbr')
local isTo = args.to
if nolink then
local prefix
return abbr
if num == 1 then
if isTo then
prefix = "To "
else
prefix = ''
end
else
else
local link = parser(route, 'link')
if isTo then
if not link or link == '' then
prefix = " to "
return abbr
else
prefix = " / "
end
end
local suffix = {}
local dir = args.dir
if name ~= '' then
if dir then
insert(suffix, mw.ustring.format(' (%s %s)', actualLink, dir))
else
else
return format("[[%s|%s]]", link, abbr)
insert(suffix, mw.ustring.format(' (%s)', actualLink))
end
end
else
insert(suffix, actualLink)
if dir then insert(suffix, ' ' .. string.lower(dir)) end
end
end
return prefix .. name .. concat(suffix)
end
end
-------------------------------------------


-- Links/abbreviations
local function banners(routes)
local function routeText(route, jctname, frame)
local format = string.format
local link
local firstRun = {}
local type = route.type
local hasBanner = false
if not type or type == '' then
for k,v in ipairs(routes) do
link = route.route
local banner
else
if v.shield == '' or v.shielderr then
link = p.link(route)
banner = false
else
banner = parser(v, 'banner') or ''
if banner and banner ~= '' then
hasBanner = true
end
end
insert(firstRun, banner)
end
end
 
if not hasBanner then return '' end
local dir = route.dir and ' ' .. string.lower(route.dir) or ''
local secondRun = {}
local routeText = link .. dir
for k,v in ipairs(routes) do
 
local bannerFile = firstRun[k]
local name = route.name
if not bannerFile then
if name and name ~= '' then
local mainText = jctname and name or routeText
elseif bannerFile == '' then
local parenText = jctname and routeText or name
local widthCode = parser(v, 'width') or 'square'
if type(widthCode) == 'number' then
return format('%s (%s)', mainText, parenText)
insert(secondRun, "[[File:No image wide.svg|" .. tostring(widthCode) .. "px|link=|alt=]]")
else
elseif widthCode == 'square' then
return routeText
insert(secondRun, "[[File:No image wide.svg|20px|link=|alt=]]")
elseif widthCode == 'expand' then
local route = v.route
local width = (#route >= 3) and '25' or '20'
insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", width))
elseif widthCode == 'wide' then
insert(secondRun, "[[File:No image wide.svg|25px|link=|alt=]]")
elseif widthCode == 'US1926' then
insert(secondRun, "[[File:No image wide.svg|21px|link=|alt=]]")
elseif widthCode == 'SD' then
local route = v.route
local width = (#route >= 3) and '23' or '20'
insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", width))
elseif (v.state == 'CA') or (v.type == 'CA') then
local route = v.route
local widths = {default = {'20', '25'}, I = {'20', '24'}, US = {'20', '23'}, SR = {'19', '22'}}
local width = widths[widthCode] or widths.default
local pixels = (#route >= 3) and width[2] or width[1]
insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", pixels))
end
else
local widthCode = parser(v, 'width') or 'square'
if widthCode == 'square' then
insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
elseif widthCode == 'expand' then
local route = v.route
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
end
elseif widthCode == 'wide' then
insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
elseif widthCode == 'SD' then
local route = v.route
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
end
elseif widthCode == 'MOSupp' then
local route = v.route
if #route >= 2 then
insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
end
elseif widthCode == 'US1926' then
insert(secondRun, format("[[File:%s|20px|link=|alt=]][[File:No image.svg|1px|link=|alt=]]", bannerFile))
elseif v.state == 'CA' then
local route = v.route
local type = v.type
if type == 'US-Bus' then
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
end
elseif type == 'CA-Bus' or type == 'SR-Bus' then
if #route >= 3 then
insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|19px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
else
insert(secondRun, format("[[File:%s|19px|link=|alt=]]", bannerFile))
end
end
end
end
end
end
return concat(secondRun) .. '<br>'
end
end


local function extra(args)
local function extra(args)
local extraTypes = mw.loadData('Module:Road data/extra')
local extraTypes = {rail = {default = "[[File:Rail Sign.svg|20px|alt=|link=]]",
                            CAN = {default = "[[File:Ontario M509.svg|20px|alt=|link=]]",
                                          QC = "[[File:Québec I-310.svg|20px|alt=|link=]]"},
                            CHL = "[[File:Chile IS-13b.svg|20px|alt=|link=]]",
                            IDN = "[[File:Indonesia New Road Sign Info 5A2.png|20px|alt=|link=]]",
                            JPN = "[[File:Japanese Road sign 125-C.svg|20px|alt=|link=]]",
                            MEX = "[[File:Mexico road sign estacion de ferrocarril.svg|20px|alt=|link=]]"},
                    ["light-rail"] = "[[File:Light Rail Sign.svg|20px|alt=|link=]]",
                    bus = {default = "[[File:Bus Sign.svg|20px|alt=|link=]]",
                          CAN = {default = "[[File:Ontario M506.svg|20px|alt=|link=]]",
                                          QC = "[[File:Québec I-315.svg|20px|alt=|link=]]"},
                          FRA = "[[File:France road sign C6.svg|20px|alt=|link=]]",
                          HRV = "[[File:Croatia road sign C44.svg|20px|alt=|link=]]",
                          HUN = "[[File:Hungary road sign E-039.svg|20px|alt=|link=]]",
                          ITA = "[[File:Italian traffic sign - fermata autobus.svg|20px|alt=|link=]]",
                          JPN = "[[File:Japanese Road sign 124-C.svg|20px|alt=|link=]]",
                          MEX = "[[File:Mexico road sign parada de autobus.svg|20px|alt=|link=]]",
                          NOR = "[[File:Norwegian-road-sign-508.1.svg|20px|alt=|link=]]",
                          URY = "[[File:Uruguay Road Sign I24.svg|20px|alt=|link=]]"},
                    ferry = {default = "[[File:Ferry Sign.svg|20px|alt=|link=]]",
                            CAN = "[[File:Ontario M508.svg|20px|alt=|link=]]",
                            CHL = "[[File:Chile IS-14b.svg|20px|alt=|link=]]",
                            FRA = "[[File:France road sign CE10.svg|20px|alt=|link=]]",
                            HRV = "[[File:Croatia road sign C49.svg|20px|alt=|link=]]",
                            ITA = "[[File:Italian traffic signs - auto su nave.svg|20px|alt=|link=]]"},
                    hospital = {default = "[[File:Hospital sign.svg|20px|alt=|link=]]",
                                AUS = "[[File:Western Australia MR-SM-1.svg|20px|alt=|link=]]",
                                AUT = "[[File:Hinweiszeichen 2.svg|20px|alt=|link=]]",
                                CAN = {default = "[[File:Québec I-280-1.svg|20px|alt=|link=]]",
                                      ON = "[[File:Ontario M401.svg|20px|alt=|link=]]"},
                                CHE = "[[File:CH-Hinweissignal-Spital.svg|20px|alt=|link=]]",
                                CHL = "[[File:Chile IS-1b.svg|20px|alt=|link=]]",
                                CZE = "[[File:IJ02cr.jpg|20px|alt=|link=]]",
                                ESP = "[[File:Spain traffic signal s23.svg|20px|alt=|link=]]",
                                FRA = "[[File:France road sign ID3.svg|20px|alt=|link=]]",
                                GBR = "[[File:UK traffic sign 827.2.svg|20px|alt=|link=]]",
                                GRC = "[[File:Traffic Sign GR - KOK 2009 - P-22.svg|20px|alt=|link=]]",
                                HUN = "[[File:Hungary road sign E-045.svg|20px|alt=|link=]]",
                                IDN = "[[File:Indonesian Road Sign d9a.png|20px|alt=|link=]]",
                                ISL = "[[File:Iceland road sign E01.12.svg|20px|alt=|link=]]",
                                ITA = "[[File:Italian traffic signs - ospedale.svg|20px|alt=|link=]]",
                                MEX = "[[File:Mexico road sign medico.svg|20px|alt=|link=]]",
                                POL = "[[File:Znak D-21.svg|20px|alt=|link=]]",
                                RUS = "[[File:7.2 Russian road sign.svg|20px|alt=|link=]]",
                                SVK = "[[File:Dopravná značka II5.svg|20px|alt=|link=]]",
                                TUR = "[[File:Turkish road sign 84.jpg|20px|alt=|link=]]",
                                UKR = "[[File:Ukraine road sign 6.2.gif|20px|alt=|link=]]",
                                URY = "[[File:Uruguay Road Sign I16.svg|20px|alt=|link=]]"},
                    airport = {default = "[[File:Airport Sign.svg|20px|alt=|link=]]",
                              AUS = "[[File:Western Australia MR-SM-11.svg|20px|alt=|link=]]",
                              CAN = {default = "[[File:Ontario M502.svg|20px|alt=|link=]]",
                                            QC = "[[File:Québec I-300-1.svg |20px|alt=|link=]]"},
                              CHL = "[[File:Chile IS-11b.svg|20px|alt=|link=]]",
                              GBR = "[[File:Aircraft Airport ecomo.svg|20px|alt=|link=]]",
                              HRV = "[[File:Croatia road sign C47.svg|20px|alt=|link=]]",
                              IDN = "[[File:Indonesia New Road Sign Info 5a4.png|20px|alt=|link=]]",
                              MEX = "[[File:Mexico road sign aeropuerto.svg|20px|alt=|link=]]",
                              NOR = "[[File:Norwegian-road-sign-771.0.svg|20px|alt=|link=]]",
                              TWN = "[[File:Legenda lotnisko.svg|20px|alt=|link=]]",
                              UKR = "[[File:Ukraine road sign 5.65.png|20px|alt=|link=]]",
                              URY = "[[File:Uruguay Road Sign I21.svg|20px|alt=|link=]]"},
toll = {default = "",
ESP = "[[File:Spain traffic signal r200.svg|18px|alt=|link=]]"}}
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
local size = defaultShieldSize .. 'px'
return ''
local countryIcon = extraIcon[args.country] or extraIcon.default
elseif type(extraIcon) == 'table' then
if type(countryIcon) == 'table' then
local extraIconT = extraIcon[args.country] or extraIcon.default
local localIcon = countryIcon[args.state] or countryIcon.default
if type(extraIconT) == 'table' then
return string.format("[[File:%s|%s|alt=|link=]]", localIcon, size)
return extraIconT[args.state] or extraIconT[args.province] or extraIconT.default
else
return extraIconT
end
else
else
return string.format("[[File:%s|%s|alt=|link=]]", countryIcon, size)
return extraIcon
end
end
end
end


local function parseArgs(args)
local function parseArgs(args)
local state = args.state or args.province or ''
local state = args.state or args.province
args.state = state
local country
local country
if args.country and args.country ~= '' then
if 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 routes = {}
local routeArgs = {}
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 252: Line 372:
route[v] = args[v .. routeCount]
route[v] = args[v .. routeCount]
end
end
if args.nolink then
if args.nolink then
route.nolink = args.nolink
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
 
insert(routes, route)
routeCount = routeCount + 1
routeCount = routeCount + 1
end
end
return routes
return routeArgs
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 = p.shield(route)
insert(shields, routeShield)
insert(shields, shield)
route.shield = routeShield
if missingShields[1] then
if args.jctname then
insert(allMissingShields, concat(missingShields, ' / '))
insert(links, namedLink(route, num))
end
else
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 graphics = concat(shields) .. extra(args) .. ' '
local bannerText = banners(routes)
local shieldText = concat(shields)
local linkText = concat(links)
local linkText = concat(links)
local graphics = (not(args.noshield) and (bannerText .. shieldText) or '') .. extra .. ' '
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
citiesPrefix = '&nbsp;'
roadStr = ' / ' .. road
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("§", '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 toErr then
-- Report invalid to errors.
addErrorMsg("&", 'error: Invalid "to" argument', errorMsg)
end
if args.road then
-- Report deprecated "road" warning.
addErrorMsg("∆", 'warning: "road" parameter is deprecated', errorMsg)
end
if args.rdt then
-- Report deprecated "rdt" warning.
addErrorMsg("£", 'warning: "rdt" 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 .. cities .. errorMsg
end
end


function p.jct(frame)
function p.jct(frame)
-- Import module function to work with passed arguments
local args = getArgs(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame, {removeBlanks = false})
return p._jct(args, frame)
return p._jct(args, frame)
end
function p._roadlink(args, frame)
local routes = parseArgs(args)
local links = {}
local typeErr = false
local toErr = false
frame = frame or mw.getCurrentFrame()
for num,route in ipairs(routes) do
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 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 = '&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 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 linkText .. cities
end
function p.roadlink(frame)
-- Import module function to work with passed arguments
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame, {removeBlanks = true})
return p._roadlink(args, frame)
end
end


return p
return p

Revision as of 14:41, 13 May 2024

Documentation for this module may be created at Module:Jct/doc

local p = {}

local SANDBOX = false
local SANDBOX_SUFFIX = SANDBOX and '/sandbox' or ''

local concat = table.concat
local insert = table.insert
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments
local parserModule = require("Module:Road data/parser" .. SANDBOX_SUFFIX)
local parser = parserModule.parser

-- Shields
local rdt

local function size(args)
	local country = args.country
	local state = args.state or args.province or ''
	local type = args.type
	if rdt then
		return 'x17'
	elseif state == 'FL' then
		if type == 'Toll' or type == 'FLTP' or type == 'HEFT' then
			return '20'
		elseif type == 'Both' then
			return '20', 'x20'
		end
	elseif state == 'NY' then
		if type == 'NY 1927' or type == 'NY 1948' or (type == 'Parkway' and args.route == "Robert Moses") or (type == 'CR' and args.county == 'Erie') then
			return '20'
		end
	elseif state == 'AB' then
		if type == 'AB' or type == 'Hwy' or type == '2ndHwy' or type == 'TCH' then
			return '18'
		end
	elseif state == 'NS' and type == 'Hwy' or type == 'TCH' then
		return '18'
	elseif state == 'ON' then
		if type == 'ON' or type == 'Hwy' or type == 'Highway' or type == 'QEW' then
			return '24x22'
		elseif type == 'KLR' then
			return '21'
		else
			local countyTypes = {CH = true, RH = true, District = true, Regional = true, County = true, Municipal = true}
			if countyTypes[type] then
				return '19'
			end
		end
	elseif state == 'QC' then
		if type == 'QC' or type == 'Route' or type == 'A' or type == 'Autoroute' or type == 'TCH' or type == 'ON' then
			return '18'
		end
	elseif state == 'SK' then
		if type == 'Hwy' or type == 'SK' then
			return 'x25'
		end
	elseif country == 'MEX' then
		return 'x25'
	elseif country == 'FRA' then
		return 'x18'
	elseif country == 'TUR' then
		return 'x15'
	end
	
	return 'x20'
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|%spx|link=|alt=]]", shield, size)
		else
			args.shielderr = true
			local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
			return mw.ustring.format("[[Category:Jct template errors|1 %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

-- Links/abbreviations
local function link(args)
	local nolink = args.nolink
	local abbr = parser(args, 'abbr')
	if nolink then
		return abbr
	else
		local link = parser(args, 'link')
		if not link or link == '' then
			return abbr
		else
			return mw.ustring.format("<span class=\"nowrap\">[[%s|%s]]</span>", link, abbr)
		end
	end
end

local function completeLink(args, num)
	local actualLink = link(args)
	if not actualLink then
		local page = mw.title.getCurrentTitle().prefixedText -- Get transcluding page's title
		actualLink = string.format("<span class=\"error\">Invalid type: %s</span>[[Category:Jct template errors|2 %s]]", args.type, page)
	end
	local isTo = args.to
	local prefix
	if num == 1 then
		if isTo then
			prefix = "To "
		else
			prefix = ''
		end
	else
		if isTo then
			prefix = " to "
		else
			prefix = " / "
		end
	end
	local suffix = {}
	local dir = args.dir
	if dir then
		insert(suffix, ' ' .. string.lower(dir))
	end
	local name = args.name
	if name then
		insert(suffix, mw.ustring.format(' (%s)', name))
	end
	return prefix .. actualLink .. concat(suffix)
end

local function namedLink(args, num)
	local actualLink = link(args)
	local name = args.name or ''
	local isTo = args.to
	local prefix
	if num == 1 then
		if isTo then
			prefix = "To "
		else
			prefix = ''
		end
	else
		if isTo then
			prefix = " to "
		else
			prefix = " / "
		end
	end
	local suffix = {}
	local dir = args.dir
	if name ~= '' then
		if dir then
			insert(suffix, mw.ustring.format(' (%s %s)', actualLink, dir))
		else
			insert(suffix, mw.ustring.format(' (%s)', actualLink))
		end
	else
		insert(suffix, actualLink)
		if dir then insert(suffix, ' ' .. string.lower(dir)) end
	end
	return prefix .. name .. concat(suffix)
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|link=|alt=]]")
			elseif widthCode == 'square' then
				insert(secondRun, "[[File:No image wide.svg|20px|link=|alt=]]")
			elseif widthCode == 'expand' then
				local route = v.route
				local width = (#route >= 3) and '25' or '20'
				insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", width))
			elseif widthCode == 'wide' then
				insert(secondRun, "[[File:No image wide.svg|25px|link=|alt=]]")
			elseif widthCode == 'US1926' then
				insert(secondRun, "[[File:No image wide.svg|21px|link=|alt=]]")
			elseif widthCode == 'SD' then
				local route = v.route
				local width = (#route >= 3) and '23' or '20'
				insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", width))
			elseif (v.state == 'CA') or (v.type == 'CA') then
				local route = v.route
				local widths = {default = {'20', '25'}, I = {'20', '24'}, US = {'20', '23'}, SR = {'19', '22'}}
				local width = widths[widthCode] or widths.default
				local pixels = (#route >= 3) and width[2] or width[1]
				insert(secondRun, format("[[File:No image wide.svg|%spx|link=|alt=]]", pixels))
			end
		else
			local widthCode = parser(v, 'width') or 'square'
			if widthCode == 'square' then
				insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
			elseif widthCode == 'expand' then
				local route = v.route
				if #route >= 3 then
					insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
				else
					insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
				end
			elseif widthCode == 'wide' then
				insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
			elseif widthCode == 'SD' then
				local route = v.route
				if #route >= 3 then
					insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
				else
					insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
				end
			elseif widthCode == 'MOSupp' then
				local route = v.route
				if #route >= 2 then
					insert(secondRun, format("[[File:No image.svg|2px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile))
				else
					insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
				end
			elseif widthCode == 'US1926' then
				insert(secondRun, format("[[File:%s|20px|link=|alt=]][[File:No image.svg|1px|link=|alt=]]", bannerFile))
			elseif v.state == 'CA' then
				local route = v.route
				local type = v.type
				if type == 'US-Bus' then
					if #route >= 3 then
						insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|20px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
					else
						insert(secondRun, format("[[File:%s|20px|link=|alt=]]", bannerFile))
					end
				elseif type == 'CA-Bus' or type == 'SR-Bus' then
					if #route >= 3 then
						insert(secondRun, format("[[File:No image.svg|1px|link=|alt=]][[File:%s|19px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile))
					else
						insert(secondRun, format("[[File:%s|19px|link=|alt=]]", bannerFile))
					end
				end
			end
		end
	end
	return concat(secondRun) .. '<br>'
end

local function extra(args)
	local extraTypes = {rail = {default = "[[File:Rail Sign.svg|20px|alt=|link=]]",
	                            CAN = {default = "[[File:Ontario M509.svg|20px|alt=|link=]]",
                                           QC = "[[File:Québec I-310.svg|20px|alt=|link=]]"},
	                            CHL = "[[File:Chile IS-13b.svg|20px|alt=|link=]]",
	                            IDN = "[[File:Indonesia New Road Sign Info 5A2.png|20px|alt=|link=]]",
	                            JPN = "[[File:Japanese Road sign 125-C.svg|20px|alt=|link=]]",
	                            MEX = "[[File:Mexico road sign estacion de ferrocarril.svg|20px|alt=|link=]]"},
	                    ["light-rail"] = "[[File:Light Rail Sign.svg|20px|alt=|link=]]",
	                    bus = {default = "[[File:Bus Sign.svg|20px|alt=|link=]]",
	                           CAN = {default = "[[File:Ontario M506.svg|20px|alt=|link=]]",
                                          QC = "[[File:Québec I-315.svg|20px|alt=|link=]]"},
	                           FRA = "[[File:France road sign C6.svg|20px|alt=|link=]]",
	                           HRV = "[[File:Croatia road sign C44.svg|20px|alt=|link=]]",
	                           HUN = "[[File:Hungary road sign E-039.svg|20px|alt=|link=]]",
	                           ITA = "[[File:Italian traffic sign - fermata autobus.svg|20px|alt=|link=]]",
	                           JPN = "[[File:Japanese Road sign 124-C.svg|20px|alt=|link=]]",
	                           MEX = "[[File:Mexico road sign parada de autobus.svg|20px|alt=|link=]]",
	                           NOR = "[[File:Norwegian-road-sign-508.1.svg|20px|alt=|link=]]",
	                           URY = "[[File:Uruguay Road Sign I24.svg|20px|alt=|link=]]"},
	                    ferry = {default = "[[File:Ferry Sign.svg|20px|alt=|link=]]",
	                             CAN = "[[File:Ontario M508.svg|20px|alt=|link=]]",
	                             CHL = "[[File:Chile IS-14b.svg|20px|alt=|link=]]",
	                             FRA = "[[File:France road sign CE10.svg|20px|alt=|link=]]",
	                             HRV = "[[File:Croatia road sign C49.svg|20px|alt=|link=]]",
	                             ITA = "[[File:Italian traffic signs - auto su nave.svg|20px|alt=|link=]]"},
	                    hospital = {default = "[[File:Hospital sign.svg|20px|alt=|link=]]",
	                                AUS = "[[File:Western Australia MR-SM-1.svg|20px|alt=|link=]]",
	                                AUT = "[[File:Hinweiszeichen 2.svg|20px|alt=|link=]]",
	                                CAN = {default = "[[File:Québec I-280-1.svg|20px|alt=|link=]]",
	                                       ON = "[[File:Ontario M401.svg|20px|alt=|link=]]"},
	                                CHE = "[[File:CH-Hinweissignal-Spital.svg|20px|alt=|link=]]",
	                                CHL = "[[File:Chile IS-1b.svg|20px|alt=|link=]]",
	                                CZE = "[[File:IJ02cr.jpg|20px|alt=|link=]]",
	                                ESP = "[[File:Spain traffic signal s23.svg|20px|alt=|link=]]",
	                                FRA = "[[File:France road sign ID3.svg|20px|alt=|link=]]",
	                                GBR = "[[File:UK traffic sign 827.2.svg|20px|alt=|link=]]",
	                                GRC = "[[File:Traffic Sign GR - KOK 2009 - P-22.svg|20px|alt=|link=]]",
	                                HUN = "[[File:Hungary road sign E-045.svg|20px|alt=|link=]]",
	                                IDN = "[[File:Indonesian Road Sign d9a.png|20px|alt=|link=]]",
	                                ISL = "[[File:Iceland road sign E01.12.svg|20px|alt=|link=]]",
	                                ITA = "[[File:Italian traffic signs - ospedale.svg|20px|alt=|link=]]",
	                                MEX = "[[File:Mexico road sign medico.svg|20px|alt=|link=]]",
	                                POL = "[[File:Znak D-21.svg|20px|alt=|link=]]",
	                                RUS = "[[File:7.2 Russian road sign.svg|20px|alt=|link=]]",
	                                SVK = "[[File:Dopravná značka II5.svg|20px|alt=|link=]]",
	                                TUR = "[[File:Turkish road sign 84.jpg|20px|alt=|link=]]",
	                                UKR = "[[File:Ukraine road sign 6.2.gif|20px|alt=|link=]]",
	                                URY = "[[File:Uruguay Road Sign I16.svg|20px|alt=|link=]]"},
	                    airport = {default = "[[File:Airport Sign.svg|20px|alt=|link=]]",
	                               AUS = "[[File:Western Australia MR-SM-11.svg|20px|alt=|link=]]",
	                               CAN = {default = "[[File:Ontario M502.svg|20px|alt=|link=]]",
                                             QC = "[[File:Québec I-300-1.svg |20px|alt=|link=]]"},
	                               CHL = "[[File:Chile IS-11b.svg|20px|alt=|link=]]",
	                               GBR = "[[File:Aircraft Airport ecomo.svg|20px|alt=|link=]]",
	                               HRV = "[[File:Croatia road sign C47.svg|20px|alt=|link=]]",
	                               IDN = "[[File:Indonesia New Road Sign Info 5a4.png|20px|alt=|link=]]",
	                               MEX = "[[File:Mexico road sign aeropuerto.svg|20px|alt=|link=]]",
	                               NOR = "[[File:Norwegian-road-sign-771.0.svg|20px|alt=|link=]]",
	                               TWN = "[[File:Legenda lotnisko.svg|20px|alt=|link=]]",
	                               UKR = "[[File:Ukraine road sign 5.65.png|20px|alt=|link=]]",
	                               URY = "[[File:Uruguay Road Sign I21.svg|20px|alt=|link=]]"},
						toll = {default = "",
									ESP = "[[File:Spain traffic signal r200.svg|18px|alt=|link=]]"}}
	
	local extraIcon = extraTypes[string.lower(args.extra or '')]
	if not extraIcon then
		return ''
	elseif type(extraIcon) == 'table' then
		local extraIconT = extraIcon[args.country] or extraIcon.default
		if type(extraIconT) == 'table' then
			return extraIconT[args.state] or extraIconT[args.province] or extraIconT.default
		else
			return extraIconT
		end
	else
		return extraIcon
	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)
	local args = getArgs(frame)
	return p._jct(args, frame)
end

return p