Module:Routelist row/sandbox 2
Appearance
Implements {{Routelist row}}
local p = {} -- Package to be exported
-- Change to "" upon deployment.
local moduleSuffix = ""
local lang = mw.getContentLanguage() -- Built-in locale for date formatting
local format = mw.ustring.format -- String formatting function
local insert = table.insert
local concat = table.concat
local util = require("Module:Road data/util")
local frame = mw.getCurrentFrame()
local parserModuleName = "Module:Road data/parser" .. moduleSuffix
local statenameModuleName = "Module:Jct/statename" .. moduleSuffix -- TODO transition
local concat = table.concat
local insert = table.insert
local format = mw.ustring.format
local trim = mw.text.trim
local parserModule = require(parserModuleName)
local parser = parserModule.parser
-- Shields
local defaultShieldSize = 32
local function addContextBanner(args, name, suffix, bannerSpec)
local bannerModule = 'Module:Road data/banners/' .. string.upper(args.country)
local shieldfield = name .. 'shield'
local shield = parser(args, shieldfield)
if shield == nil then
-- This route type does not define shield.
-- Find shield in the default banner table.
shield = parser(args, 'shield', name, bannerModule)
if shield and shield ~= '' then
if suffix == nil then
suffix = parser(args, '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, {shield, shieldSize})
end
end
local function bannerSpec(banner, bannerSize, bannerSuffix, route)
local banners = {}
if type(banner) == "table" then
local bannerSizeIsNotTable = type(bannerSize) ~= "table"
for i,filename in ipairs(banner) do
local bannersize = bannerSizeIsNotTable and bannerSize or bannerSize[i] or defaultShieldSize
insert(banners, {filename, bannersize})
end
elseif banner ~= '' then
insert(banners, {banner, bannerSize})
end
if args.dir then
addContextBanner(args, 'dir', bannerSuffix, banners)
end
if args.to then
addContextBanner(args, 'to', bannerSuffix, banners)
end
return banners
end
local function shieldSpec(args, mainShield, shieldList)
local shieldSpec = {}
local shield
if not shield then shield = parser(args, 'shieldlist') or parser(args, 'shield') or '' end
if shield == '' then return shieldSpec end
local orientation = parser(args, 'orientation')
local function size(args)
if orientation == "upright" then
return defaultShieldSize
else return "x" .. defaultShieldSize
end
end
local shieldsize = size(args)
local banner = parser(args, 'banner') or {}
local bannersize = defaultShieldSize
local bannersuffix = parser(args, 'bannersuffix')
local bannerIsNotTable = type(banner) ~= "table"
local bannersizeIsNotTable = type(bannersize) ~= "table"
local bannersuffixIsNotTable = type(bannersuffix) ~= "table"
if type(shield) == "table" then
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
return shieldSpec
end
local missingShields
local shieldExistsCache = {}
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
if height then
insert(sizeparts, format("x%d", height * scale))
end
size = concat(sizeparts)
else
size = format("%s%d", landscape and "x" or "", defaultShieldSize * scale)
end
local shieldCode = format("[[File:%s|%spx|link=|alt=]]", shield[1], size)
if not banners[1] then return shieldCode end
for _,banner in ipairs(banners) do
shieldCode = format("[[File:%s|%spx|link=|alt=]]<br>%s",
banner[1],
defaultShieldSize,
shieldCode)
end
return '<span style="display: inline-block; vertical-align: baseline; line-height: 0; text-align: center;">' .. shieldCode .. '</span>'
end
function p.shield(args, scale, showLink, mainShield, shieldList)
missingShields = {}
scale = scale or 1
local rendered = {}
for _,entry in ipairs(shieldSpec(args, mainShield, shieldList)) do
insert(rendered, render(entry, scale, showLink))
end
return concat(rendered), missingShields
end
-- Links/abbreviations
function p.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 format("[[%s|%s]]", link, abbr)
end
end
end
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
function p.locations(args, module, group)
module = module or ""
local modulearticle = module .. "article"
local moduleprefix = module .. "prefix"
local modulenameprefix = module .. "nameprefix"
local modulenamesuffix = module .. "namesuffix"
local warnings = {}
-- Region, for disambiguation
local region = parserModule.parser(args, "region", " common ")
if not region then
-- TODO transition
if args.region then
warnings.region = "region parameter is deprecated"
region = args.region
elseif args.country and (args.state or args.province) then
warnings.region = "Inferring region from country and state/province"
region = stateName(args)
end
end
local regionName
local regionText
if type(region) == "table" then
regionName = region.name
regionText = format("[[%s|%s]]", region.link, regionName)
elseif region then
regionName = region
regionText = format("[[%s]]", regionName)
end
args.region = regionName
local locations = parserModule.parser(args, "locations", " common ") or {}
-- Primary topic requires no specialization to supplied locations.
local primaryTopic = not locations and module == "jctint" and args.primary_topic ~= 'no'
if args.primary_topic then
-- TODO transition
warnings.primary_topic = "primary_topic parameter is deprecated"
end
-- Independent city
local indepCityText
if args.indep_city_special then
indepCityText = args.indep_city_special -- Overrides `indep_city` argument.
elseif args.indep_city then
local indepCity = args.indep_city
local spec = locations.indep_city
if spec then
local link = format("%s%s%s",
spec.linkprefix or "", indepCity, spec.linksuffix or "")
local name = format("%s%s%s",
spec[modulenameprefix] or spec.nameprefix or "",
indepCity,
spec[modulenamesuffix] or spec.namesuffix or "")
indepCityText = format("%s%s[[%s|%s]]",
spec[modulearticle] or spec.article or "",
spec[moduleprefix] or spec.prefix or "",
link, name)
else
-- TODO transition
warnings.indep_city = "Spec for indep_city parameter undefined in road data module"
local cityLink -- Wikilink for independent city
if primaryTopic then
cityLink = format('[[%s]]', indepCity)
else
-- Specialize independent city to the region.
cityLink = format('[[%s, %s|%s]]', indepCity, region, indepCity)
end
indepCityText = "[[Independent city|City]] of " .. cityLink
end
end
if indepCityText then
return {region = regionText, indep_city = indepCityText, warnings = warnings}
end
-- First-level subdivision, e.g., county
-- Name of the type of subdivision, e.g., "County" and "Parish"
local sub1name = args.sub1name -- TODO transition
local sub1Text
if args.sub1_special then
sub1Text = args.sub1_special -- Overrides `sub1` argument.
elseif args.sub1 then
local sub1 = args.sub1
local article
local link = sub1
local name = sub1
-- Type of first-level subdivision area, as a form of disambiguation
local sub1area = args.sub1area
if sub1area then
local sub1areaSpec = locations.sub1areas and locations.sub1areas[sub1area]
if sub1areaSpec then
article = sub1areaSpec[modulearticle] or sub1areaSpec.article or ""
link = format("%s%s%s",
sub1areaSpec.linkprefix or "", link, sub1areaSpec.linksuffix or "")
name = format("%s%s%s",
group and "" or sub1areaSpec[modulenameprefix] or sub1areaSpec.nameprefix or "",
name,
group and "" or sub1areaSpec[modulenamesuffix] or sub1areaSpec.namesuffix or "")
else
-- TODO report error
local errMsg = util.err(format("Undefined sub1area: %s", sub1area))
name = format("%s%s", name, errMsg)
end
end
if locations.sub1 then
local spec = locations.sub1
-- Prepend and append text from spec.
link = format("%s%s%s",
spec.linkprefix or "", link, spec.linksuffix or "")
name = format("%s%s%s",
spec[modulenameprefix] or spec.nameprefix or "",
name,
spec[modulenamesuffix] or spec.namesuffix or "")
sub1Text = format("%s[[%s|%s]]", article or "", link, name)
else
-- TODO transition
warnings.sub1 = "Spec for sub1 parameter undefined in road data module"
-- Add type (if specified) to wikilink for first-level subdivision.
local sub1Link = sub1name and trim(format("%s %s", sub1, sub1name)) or sub1
local sub1Name = module == "jcttop" and sub1Link or sub1
if primaryTopic then
sub1Text = format('[[%s|%s]]', sub1Link, sub1Name)
else
-- Specialize first-level subdivision, with type added, to the region.
sub1Text = format('[[%s, %s|%s]]', sub1Link, region, sub1Name)
end
end
end
-- Second-level subdivision, e.g., city and town
local sub2Text
if args.sub2_special then
sub2Text = args.sub2_special -- Overrides `sub2` argument.
elseif args.sub2 then
local sub2 = args.sub2
if sub2 == "none" then
sub2Text = "​" -- Zero-width space
elseif sub2 == " " then
-- TODO transition
warnings.sub2 = " argument for sub2 parameter is deprecated"
sub2Text = "​" -- Zero-width space
elseif primaryTopic then
-- TODO transition
sub2Text = format("[[%s]]", sub2)
else
local article
local link = sub2
local name = sub2
-- Type of area, e.g., city and village, as a form of disambiguation
local sub2area = args.sub2area --[[TODO transition]] or args.area
if sub2area then
local sub2areaSpec = locations.sub2areas and locations.sub2areas[sub2area]
if not sub2areaSpec then
-- TODO transition
warnings.sub2 =
format("Spec for area parameter '%s' undefined in road data module", sub2area)
local sub2areas = { -- table of different area types
city = {
linksuffix = " (city)",
jcttoparticle = "the ",
nameprefix = "City of "
},
town = {
linksuffix = " (town)",
jcttoparticle = "the ",
nameprefix = "Town of "
},
village = {
linksuffix = " (village)",
jcttoparticle = "the ",
nameprefix = "Village of "
},
community = {
linksuffix = " (community)",
jcttoparticle = "the ",
nameprefix = "Community of "
},
CDP = {
linksuffix = " (CDP)",
jcttoparticle = "the ",
nameprefix = "Community of "
},
hamlet = {
linksuffix = " (hamlet)",
jcttoparticle = "the ",
nameprefix = "Hamlet of "
},
["unorganized territory"] = {
linksuffix = " (unorganized territory)",
jcttoparticle = "the ",
nameprefix = "Unorganized Territory of "
},
township = {
linksuffix = " Township",
namesuffix = " Township",
}
}
sub2areaSpec = sub2areas[sub2area]
end
if sub2areaSpec then
article = sub2areaSpec[modulearticle] or sub2areaSpec.article or ""
link = format("%s%s%s",
sub2areaSpec.linkprefix or "", link, sub2areaSpec.linksuffix or "")
name = format("%s%s%s",
group and "" or sub2areaSpec[modulenameprefix] or sub2areaSpec.nameprefix or "",
name,
group and "" or sub2areaSpec[modulenamesuffix] or sub2areaSpec.namesuffix or "")
else
-- TODO report error
local errMsg = util.err(format("Undefined sub2area: %s", sub2area))
name = format("%s%s", name, errMsg)
end
end
if locations.sub2 then
local spec = locations.sub2
-- Prepend and append text from spec.
link = format("%s%s%s",
spec.linkprefix or "", link, spec.linksuffix or "")
name = format("%s%s%s",
spec[modulenameprefix] or spec.nameprefix or "",
name,
spec[modulenamesuffix] or spec.namesuffix or "")
else
-- TODO transition
warnings.sub2 = "Spec for sub2 parameter undefined in road data module"
-- Some second-level subdivisions are not unique in a given region.
-- `sub1dab` is the first-level subdivision to be used for disambiguation.
local sub1dab = args.sub1dab
if sub1dab then
sub1dab = sub1name and trim(format("%s %s", sub1dab, sub1name)) or sub1dab
link = format("%s, %s", link, sub1dab)
end
link = format("%s, %s", link, region) -- Add region to wikilink
end
sub2Text = format("%s[[%s|%s]]", article or "", link, name)
end
end
return {region = regionText, sub1 = sub1Text, sub2 = sub2Text, warnings = warnings}
end
--------------------------
--[[-
@type status
@field #string row: The start of the row, for this particular type (color)
@field #string established: The string to be output in the "Formed" column.
For future routes, "proposed" is displayed here.
Otherwise, display the year passed in the established parameter.
@field #string removed: The string to be output in the "Removed" column.
In the case of routeStates.former, the year that the route was
decommissioned is output instead.
]]
--[[-
Route statuses.
@list <#status>
]]
local routeStatuses = {
-- current routes
current = {
row = "|-",
removed = "current"
},
-- future routes
future = {
row = '|- style="background-color:#ffdead;" title="Future route"',
established = "proposed",
removed = "—"
},
-- former routes
former = {
row = '|- style="background-color:#d3d3d3;" title="Former route"'
},
-- routes marked as former by override
-- deprecated
formeroverride = {
row = '|- style="background-color:#d3d3d3;" title="Former route"',
removed = "—"
},
-- route with unknown status
unknown = {
row = "|-",
removed = "—"
}
}
--[[-
Return the route status.
@param #string established `established` argument passed to the module
@param #string decommissioned `decommissioned` argument passed to the module
@return #status the status of the route.
]]
local function getRouteStatus(established, decommissioned)
if decommissioned == 'yes' then
-- a former route with no decommission information
return routeStatuses.formeroverride
elseif decommissioned then
-- If the route is decommissioned, then it must be a former route.
return routeStatuses.former
elseif not established then
-- Without the establishment date, there is not enough information
-- to determine the status of the route.
return routeStatuses.unknown
elseif established == 'proposed' then
-- a future route
return routeStatuses.future
else
-- a current route
return routeStatuses.current
end
end
--[[-
A limited replacement for {{dts}}.
Derive the sort key from a given date.
@param #string date
@param #string circa "yes" if `date` is tagged as circa
@return #string true the hidden sort key, along with the year of the original date
@return #boolean false if the sort key cannot be derived
]]
local function dtsYearCore(date)
local year = lang:formatDate('Y', date) -- year for this date
if year == date then -- If the provided date is just the year,
-- tack on January 1 for the sort key to work right.
date = date .. "-01-01"
end
local month = lang:formatDate('m', date) -- month for this date
local day = lang:formatDate('d', date) -- day for this date
-- Create and store the formatted hidden sort key.
-- The year must be five digits, per convention.
local dtsStr = format("%05d-%02d-%02d", year, month, day)
-- Return the hidden sort key and the year for this date.
return {dtsStr, year}
end
local function dtsYear(date, circa)
local success, result = pcall(dtsYearCore, date)
if not success then
result = {
"00001-01-01",
util.err(format('Invalid date "%s".', date))
}
end
-- Generate the HTML code necessary for the hidden sort key.
local dtsStyle = format("style=\"white-space:nowrap;\" data-sort-value=\"%s\"", result[1])
local year = result[2]
if circa == 'yes' then -- If the date is tagged as circa,
-- add the circa abbreviation to the display. Derived from {{circa}}.
year = "<span style=\"white-space:nowrap;\"><abbr title=\"circa\">c.</abbr> " .. year .. "</span>"
end
return dtsStyle, year
end
--- Return formatting and output for a date column.
local function date(text, date, circa, ref)
-- Returns the text if specified, or the dtsYear-formatted date, and an em-dash.
local style, output
if text then
output = text
elseif date then
style, output = dtsYear(date, circa)
else
output = "—"
end
return format("|align=center %s|%s%s", style or "", output, ref)
end
--- Return output for the date columns for a given route.
local function dates(established, decommissioned, routeStatus, args)
local established_ref = args.established_ref or '' -- Reference for date established
local decommissioned_ref = args.decommissioned_ref or '' -- Reference for date decommissioned
return format("%s\n%s",
date(routeStatus.established, established, args.circa_established, established_ref),
date(routeStatus.removed, decommissioned, args.circa_decommissioned, decommissioned_ref))
end
--- Return output for the termini columns for a given route.
local function termini(args)
local beltway = args["beltway"]
if beltway then
-- The given route is a beltway.
-- `beltway` text will span both termini columns.
return "|colspan=2 align=center|" .. beltway
else
local terminus_a = args["terminus_a"] or '—' -- Southern or western terminus
local terminus_b = args["terminus_b"] or '—' -- Northern or eastern terminus
-- Fill in the termini columns
return '|' .. terminus_a .. '||' .. terminus_b
end
end
--- Return output for the length columns for a given route, with the appropriate conversions.
local function length(args)
local km = args["length_km"] or '' -- Length in kilometers
local mi = args["length_mi"] or '' -- Length in miles
local ref = args["length_ref" ] or ''
if mi == '' and km == '' then
return format("|align=right|—||align=right|—")
elseif mi ~= '0' and km == '' then
return format("|") .. frame:expandTemplate{ title = 'convert', args = { mi, "mi", "km", disp = "table"}}
else
return format("|") .. frame:expandTemplate{ title = 'convert', args = { km, "km", "mi", disp = "table"}}
end
end
--- Generate a "Local names" cell if necessary.
local function localname(args)
local enabled = args[1] or ''
if enabled == "local" then
local localName = args["local"] or ''
return "|" .. localName
else
return ''
end
end
--- Generate a "Notes" cell if necessary.
local function notes(notes)
if notes == 'none' then
return '| ' --create empty cell
elseif notes then
return '|' .. notes --display notes in cell
else
return '' --create no cell
end
end
--- Derive the sort key from a given route.
local function sortkey(abbr)
-- Split `abbr` into three possibly empty parts, with number in the middle.
local prefix, num, suffix = mw.ustring.match(abbr, "([^0-9]*)(%d*)(.*)")
-- If `abbr` does not contain a number, the entry appears at the bottom.
num = tonumber(num)
num = type(num) == "number" and format("%04d", num) or ""
-- The sort key is `abbr`, but with route number zero-padded to 4 digits
-- and prefix moved to the end.
return mw.text.trim(
mw.ustring.gsub(format("%s%s %s", num, suffix, prefix), " ", " "),
"- ")
end
local function route(args, shieldSize)
local link, abbr = p.link(args)
-- Use the sort key if already specified.
local sortkey = args.sortkey or sortkey(abbr or "")
local shield = p.shield(args)
if shield == nil then
return format('!scope="row" class="nowrap" data-sort-value="%s"|%s',
sortkey, link)
else
return format('!scope="row" class="nowrap" data-sort-value="%s"|%s %s',
sortkey, shield, link)
end
end
--- Derive the anchor from a given route.
local function anchor(routeType, routeNo)
-- Split `routeNo` into three possibly empty parts, with number in the middle.
local prefix, num, suffix = mw.ustring.match(routeNo, "([^0-9]*)(%d*)(.*)")
-- Zero-pad route number to 4 digits if `routeNo` does contain a number.
num = tonumber(num)
num = type(num) == "number" and format("%04d", num) or ""
-- The anchor is the concatenation of `type` and zero-padded `routeNo`.
return format("%s%s%s%s", routeType, prefix, num, suffix)
end
function p._row(args)
local established = args.established
local decommissioned = args.decommissioned
local routeStatus = getRouteStatus(established, decommissioned)
local anchor = args.anchor or anchor(args.type, args.route)
local rowdef = format('%s id="%s"', routeStatus.row, anchor)
local route = route(args)
local length = length(args)
local termini = termini(args)
local localname = localname(args)
local dates = dates(established, decommissioned, routeStatus, args)
local notes = notes(args.notes)
local row = {rowdef, route, length, termini, localname, dates, notes}
return concat(row, '\n')
end
function p.row(frame)
-- Import module function to work with passed arguments
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame) -- Gather passed arguments into easy-to-use table
return p._row(args);
end
return p