Module:Routelist row: Difference between revisions

Content deleted Content added
Adding entry point functions for local names and notes
drop nowrap - was causing horizontal scrolling on List of Ontario provincial highways for 407
 
(62 intermediate revisions by 8 users not shown)
Line 1:
local p = { } -- Package to be exported
local getArgs = require('Module:Arguments').getArgs -- Import module function to work with passed arguments
local lang = mw.getContentLanguage() -- Retrieve built-in locale for date formatting
local format = mw.ustring.format -- String formatting function
local mw = require "Module:mw" -- Import module for HTML tag generation
local frame = mw.getCurrentFrame()
 
local routeStates = { } -- Table with route statuses.
--[[ The following tables include the following entries:
row: The start of the row, for this particular type (color)
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.
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.
]]--
routeStates.current = {row = "|- \n", removed = "presentcurrent"} -- Data for current routes
routeStates.future = {row = '|- style="background-color:#ffdead;" title="Future route"\n', removedestablished = "proposed", removed = "—"} -- Data for future routes
routeStates.former = {row = '|- style="background-color:#d3d3d3;" title="Former route"\n'} -- Data for former routes
routeStates.unknownformeroverride = {row = "'|- \nstyle="background-color:#d3d3d3;" title="Former route"', removed = "—"} -- Data for routeroutes marked as withformer unknownby statusoverride
routeStates.unknown = {row = "|-", removed = "—"} -- Data for route with unknown status
 
function getRouteState(argsestablished, decommissioned)
--[[ This function is passed the dates given for the established and decommissioned fields to the template.
It then returns the entry in the routeStates table corresponding to the status of the route.
]]--
if decommissioned == 'yes' then --If the decommissioned property just says "yes", then mark it as a former route and display default data.
local established = args.established -- Date passed to the module via the "established" parameter of {{routelist row}}
return routeStates.formeroverride
local decommissioned = args.decommissioned -- Date passed to the module via the "decommissioned" parameter of {{routelist row}}
if elseif decommissioned ~= '' then -- If the route is decommissioned, then it must be a former route.
return routeStates.former
elseif elseifnot established == '' then -- Without the establishment date, there is not enough information to determine the status of the route.
return routeStates.unknown
elseif lang:formatDate("Y-m-d")established <== established'proposed' then -- If the current"established date (from lang:formatDate("Y-m-d")) is less than the date ofstring establishment'proposed', then it must be a future route.
return routeStates.future
else -- If none of the first three conditions are true, then it must be a current route.
return routeStates.current
end
end
 
function getLengthdtsYearCore(argsdate, circa)
-- A limited replacement --for This{{dts}}. functionThis is passed thea lengthdate fieldsand derives a sort key from it. It returns a string with the {{routelisthidden row}}sort invocationkey, andalong calculateswith the missingyear lengthof fromthe theoriginal otherdate.
if not date then return false end -- If the date is an empty string, stop and go back to whence it came.
local math = require "Module:Math" -- This module contains functions needed later in this function.
local year = lang:formatDate('Y', date) -- This invocation of lang:formatDate returns just the year.
local precision = math._precision -- The math._precision function provides the precision of a given string representing a number.
if year == date then -- If the provided date is just the year:
local round = math._round -- This method rounds a given number to the given number of digits. In Lua, storing these functions locally results in more efficient execution.
date = date .. "-01-01" -- Tack on January 1 for the sort key to work right.
local length = {} -- This table will store the computed lengths.
end
local km = args["length_km"] -- The kilometer length from the {{routelist row}} call.
local month = lang:formatDate('m', date) -- Stores the month of the date.
local mi = args["length_mi"] -- The length in miles as passed to {{routelist row}}.
local day = lang:formatDate('d', date) -- Stores the day for this date.
if '' == km then -- This signifies that a length in kilometers was not passed.
local dtsStr = string.format("%05d-%02d-%02d", year, month, day) -- Create and store the formatted hidden sort key. The year must be five digits, per convention.
local n = tonumber(mi) -- The first step is to convert the miles (passed as a string from the template) into a number.
local spanParams = {style = "display:none; speak:none"} -- These CSS properties hide the sort key from normal view.
local prec = precision(mi) -- Retrieve the precision of the passed mile value (as a string).
local dtsSpan = mw.text.tag({name='span', content=dtsStr, attrs=spanParams}) -- This generates the HTML code necessary for the hidden sort key.
if n then -- If the passed mile value is an empty string, n will equal nil, which would make this statement false. Otherwise, the length in kilometers is computed and stored.
if circa == 'yes' then -- If the date is tagged as circa,
length.km = round(n * 1.609344, prec) -- Compute and round the length in kilometers, and store it in the length table.
return dtsSpan .. "<abbr title=\"circa\">c.</abbr><span style=\"white-space:nowrap;\">&thinsp;" .. year .. "</span>" -- Add the circa abbreviation to the display. Derived from {{circa}}
else -- No mile value was passed
else -- Otherwise,
length.km = 0
return dtsSpan .. year -- Return the hidden sort key concatenated with the year for this date.
end
end
else -- If the length in kilometers was passed, the computed lengths table will simply contain the passed length.
length.km = km
end
if '' == mi then -- The same as above, but this time converting kilometers to mile if necessary.
local n = tonumber(km) -- Kilometers as a number
local prec = precision(km) -- Precision of the passed length
if n then -- If a kilometer value was passed:
length.mi = round(n / 1.609344, prec) -- Compute and store the conversion into miles.
else -- If not:
length.mi = 0 -- Store 0.
end
else -- And if the length in miles was passed:
length.mi = mi -- Simply store it.
end
return length -- Return the length table with the computed lengths.
end
 
function dtsYear(date, circa)
local success, result = pcall(dtsYearCore, date, circa)
-- A limited replacement for {{dts}}. This is passed a date and derives a sort key from it. It returns a string with the hidden sort key, along with the year of the original date.
if success then
if date == '' then return false end -- If the date is an empty string, stop and go back to whence it came.
return result
local year = lang:formatDate('Y', date) -- This invocation of lang:formatDate returns just the year.
else
if year == date then -- If the provided date is just the year:
return string.format('%s<span class="error">Error: Invalid date "%s".</span>', circa and '<abbr title="circa">c.</abbr>&thinsp;' or '', date)
date = date .. "-01-01" -- Tack on January 1 for the sort key to work right.
end
local month = lang:formatDate('m', date) -- Stores the month of the date.
local day = lang:formatDate('d', date) -- Stores the day for this date.
local dtsStr = string.format("%05d-%02d-%02d", year, month, day) -- Create and store the formatted hidden sort key. The year must be five digits, per convention.
local spanParams = {style = "display:none; speak:none"} -- These CSS properties hide the sort key from normal view.
local dtsSpan = mw.text.tag({name='span', contents=dtsStr, params=spanParams}) -- This generates the HTML code necessary for the hidden sort key.
return dtsSpan .. year -- Return the hidden sort key concatenated with the year for this date
end
 
function sortkeyremoved(KeyrouteState, Typedecommissioned, Routecirca)
-- This function returns the proper value for the removed column.
-- This function return the sort key for the route (not to be confused with the previous function, which generates a sort key for the established and decommissioned dates.)
return routeState.removed or dtsYear(decommissioned, circa) -- Returns the removed attribute of the provided routeState table or, if empty, the dtsYear-formatted decommissioned date.
if Key ~= '' then -- If a sort key already exists:
return Key -- Simply return it.
else -- Otherwise:
local PaddedRoute = string.format('%04d', Route) -- This invocation is equivalent to the {{0000expr}} template. It zero-pads the given route number up to 4 digits.
return Type .. PaddedRoute -- Return the sort key for this route, composed of the type and zero-padded route number.
end
end
 
function p.rowcolorformed(framerouteState, established, circa)
-- This function determinesreturns if this is a future route, present route, or decommissionedthe route,proper andvalue colorsfor the rowformed appropriatelycolumn.
return routeState.established or dtsYear(established, circa) or "—" -- Returns 'proposed' if the route is proposed, the dtsYear-formatted established date if one was provided, or an em-dash.
local pframe = frame:getParent()
end
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
-- Import parameters from template
local established = args["established"] or '' -- Date established
local decommissioned = args["decommissioned"] or '' -- Date decommissioned
local dates = {["established"] = established, ["decommissioned"] = decommissioned} -- This table encapsulates the given dates into one value...
local routeState = getRouteState(dates) -- which is passed to getRouteState, which returns one of the entries in routeStates,
 
function sortkey(args)
return routeState.row -- whose row attribute is returned
-- This function return the sort key for the route (not to be confused with the previous function, which generates a sort key for the established and decommissioned dates.)
local key = args.sortkey
local type = args.type
local route = args.route or ''
if key then -- If a sort key already exists:
return key -- Simply return it.
else -- Otherwise:
local routeKey
local routeNum = tonumber(route)
if routeNum then
routeKey = string.format('%04d', route) -- This invocation is equivalent to the {{0000expr}} template. It zero-pads the given route number up to 4 digits.
else
local num, suffix = string.match(route, "(%d*)(.+)")
routeKey = (tonumber(num) and string.format('%04d', num) or '') .. suffix
end
return type .. routeKey -- Return the sort key for this route, composed of the type and zero-padded route number.
end
end
 
function p.termini(frameargs)
-- This function determines if this is a beltway or not, and displays the termini columns appropriately.
local beltway = args["beltway"] -- Text in this parameter will span both termini columns.
local pframe = frame:getParent()
local terminus_a = args["terminus_a"] or '—' -- Southern or western terminus
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local terminus_b = args["terminus_b"] or '—' -- Northern or eastern terminus
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
if beltway then
-- Import parameters from template
return "|colspan=2 local beltway align= args[center|"beltway"] or.. ''beltway -- TextThis intext thiswill, parameter willagain, span both termini columns.
else
local terminus_a = args["terminus_a"] or '–' -- Southern or western terminus
return '|' .. terminus_a local.. terminus_b'||' =.. args["terminus_b"] or '–' -- NorthernFill in orthe easterntermini terminuscolumns
end
end
 
function dates(established, decommissioned, routeState, args)
if beltway ~= '' then -- The template passes this function an empty string if {{{beltway}}} is not specified by the template user.
return "|colspan=2 align=center|" .. beltway -- This text will,function again,displays spanthe bothdate columns.
else
return '|' .. terminus_a .. '||' .. terminus_b -- Fill in the termini columns
if args.gazette == 'yes' then
end
local established = args.established or "—"
local established_ref = args.established_ref or ''
return "|align=center|" .. established .. established_ref
else
local established_ref = args.established_ref or '' -- Reference for date established
local decommissioned_ref = args.decommissioned_ref or '' -- Reference for date decommissioned
return "|align=center|" .. formed(routeState, established, args.circa_established) ..
established_ref .. "||align=center|" .. removed(routeState, decommissioned, args.circa_decommissioned) ..
decommissioned_ref
end
end
 
--- Return output for the length columns for a given route, with the appropriate conversions.
function removed(routeState, decommissioned)
local function length(args)
-- This function returns the proper value for the removed column.
local km = args["length_km"] or '' -- Length in kilometers
return routeState.removed or dtsYear(decommissioned) -- Returns the removed attribute of the provided routeState table or, if empty, the dtsYear-formatted decommissioned date.
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("|align=right|") .. mi .. ref .. format("||align=right|") .. frame:expandTemplate{ title = 'convert', args = { mi, "mi", "km", disp = "output number only"}}
else
return format("|align=right|") .. km .. ref .. format("||align=right|") .. frame:expandTemplate{ title = 'convert', args = { km, "km", "mi", disp = "output number only"}}
end
end
 
function formedlocalname(establishedargs)
-- This function returns thegenerates propera value"Local fornames" thecell formedif column.necessary
local enabled = args[1] or ''
return dtsYear(established) or "—" -- Returns the dtsYear-formatted established date or, if none was provided, an em-dash.
local localName = args["local"] or ''
if mw.text.trim(enabled) == "local" then
return "|" .. localName
else
return ''
end
end
 
function p.datesnotes(framenotes)
-- This function displaysgenerates a "Notes" thecell dateif columnsnecessary.
if notes == 'none' then
local pframe = frame:getParent()
return '| ' --create empty cell
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
elseif notes then
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
return '|' .. notes --display notes in cell
else
-- Import parameters from template
return '' --create no cell
local established = args["established"] or args["circa"] or '' -- Date established
end
local decommissioned = args["decommissioned"] or '' -- Date decommissioned
local dates = {["established"] = established, ["decommissioned"] = decommissioned} -- This table encapsulates the given dates into one value...
local routeState = getRouteState(dates) -- which is passed to getRouteState, which returns one of the entries in routeStates,
local output = "|align=center|" .. formed(established) .. "||align=center|" .. removed(routeState, decommissioned) -- which is passed to the removed function. This line generates the Wikitext for this part of the row.
return output
end
 
function p.lengthgap(frameargs)
local text = args.text or "''Number not designated''"
-- This function generate the length columns, with the appropriate conversions.
local pframe = frame:getParent()
if notes then
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
return '|align=center colspan=7|' .. text --display notes in cell
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
else
return '|align=center colspan=6|' .. text --display notes in cell
local miles = args["length_mi"] or '' -- Length in miles
end
local kilometers = args["length_km"] or '' -- Length in kilometers
local lengths = {length_mi = miles, length_km = kilometers} -- This time, we compile the lengths into a table,
local Lengths = getLength(lengths) -- which makes for an easy parameter. This function call will return the lengths in both miles and kilometers,
return "|align=right|" .. Lengths.mi .. "||align=right|" .. Lengths.km -- which are then spliced in here and returned to the template.
end
 
function p.sortkeyroute(frameargs)
-- This function generatesdisplays the route'sshield sortand keylink.
local pframeformat = frame:getParent()mw.ustring.format
local parserModule = require "Module:Road data/parser"
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local parser = parserModule.parser
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
local noshield = args.noshield
local SortKey = args["sortkey"] or '' -- If a sort key is given, store it here.
local bannerFile = parser(args, 'banner')
local Type = args["type"] or '' -- The route type (Interstate, State highway, etc.)
local banner
local Route = args["route"] or '' -- The route number.
if not noshield and bannerFile and bannerFile ~= '' then
local widthCode = parser(args, 'width') or 'square'
return sortkey(SortKey, Type, Route) -- And then return the sort key from this function to the template.
if widthCode == 'square' then
banner = format("[[File:%s|25px|link=|alt=]]", bannerFile)
elseif widthCode == 'expand' then
local route = args.route
if #route >= 3 then
banner = format("[[File:No image.svg|3px|link=|alt=]][[File:%s|25px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile)
else
banner = format("[[File:%s|25px|link=|alt=]]", bannerFile)
end
elseif widthCode == 'wide' then
banner = format("[[File:No image.svg|3px|link=|alt=]][[File:%s|25px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile)
elseif widthCode == 'MOSupp' then
local route = args.route
if #route >= 2 then
banner = format("[[File:No image.svg|3px|link=|alt=]][[File:%s|25px|link=|alt=]][[File:No image.svg|3px|link=|alt=]]", bannerFile)
else
banner = format("[[File:%s|25px|link=|alt=]]", bannerFile)
end
elseif widthCode == 'US1926' then
banner = format("[[File:%s|25px|link=|alt=]][[File:No image.svg|1px|link=|alt=]]", bannerFile)
elseif args.state == 'CA' then
local route = args.route
local type = args.type
if type == 'US-Bus' then
if #route >= 3 then
banner = format("[[File:No image.svg|2px|link=|alt=]][[File:%s|25px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile)
else
banner = format("[[File:%s|25px|link=|alt=]]", bannerFile)
end
elseif type == 'CA-Bus' or type == 'SR-Bus' then
if #route >= 3 then
banner = format("[[File:No image.svg|1px|link=|alt=]][[File:%s|25px|link=|alt=]][[File:No image.svg|2px|link=|alt=]]", bannerFile)
else
banner = format("[[File:%s|24px|link=|alt=]]", bannerFile)
end
end
end
banner = banner .. '<br>'
else
banner = ''
end
 
local shield
if not noshield then
local shieldFile, second = parser(args, 'shieldlist') or parser(args, 'shield') or ''
if shieldFile == nil or shieldFile == '' then
shield = ''
elseif type(shieldFile) == 'table' then
shieldFile, second = shieldFile[1], shieldFile[2]
end
if second and type(second) == 'string' then
local shield1 = format("[[File:%s|x25px|alt=|link=]]", shieldFile)
local shield2 = format("[[File:%s|x25px|alt=|link=]]", second)
shield = shield1 .. shield2
elseif shield == '' then
shield = ''
else
shield = shieldFile and format("[[File:%s|x25px|alt=|link=]]", shieldFile) or ''
end
else
shield = ''
end
local linkTarget = (not args.nolink) and parser(args, 'link')
local abbr = parser(args, 'abbr')
local link
if linkTarget then
link = format("[[%s|%s]]", linkTarget, abbr)
else
link = abbr
end
if not link then error("Type not in database: " .. args.type) end
local sortkey = sortkey(args)
local sortedLink = format("<span data-sort-value=\"%s&#32;!\">%s</span>", sortkey, link)
local route = banner .. shield .. ' ' .. sortedLink
return '|' .. route
end
 
function p.localnamerow(frame)
local args = getArgs(frame) -- Gather passed arguments into easy-to-use table
-- This function generates a "Local names" cell if necessary
local pframe = frame:getParent()
local established = args.established
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local decommissioned = args.decommissioned
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
local routeState = getRouteState(established, decommissioned)
local anchor = args.anchor or sortkey(args)
local enabled = args[1]
local rowdef = routeState.row .. string.format(' id="%s"', anchor)
local localName = args["local"] or ''
local route = route(args)
if enabled == "local" then
local length = length(args)
return "|" .. localName
local termini = termini(args)
else
local localname = localname(args)
return ''
local dates = dates(established, decommissioned, routeState, args)
end
local notesArg = args.notes
local notes = notes(notesArg)
local row = {rowdef, route, length, termini, localname, dates, notes}
return table.concat(row, '\n')
end
 
function p.notesgap(frame)
local args = getArgs(frame) -- Gather passed arguments into easy-to-use table
-- This function generates a "Notes" cell if necessary.
 
local pframe = frame:getParent()
local routeState = getRouteState(established, decommissioned)
local config = frame.args -- the arguments passed BY the template, in the wikitext of the template itself
local anchor = args.anchor or sortkey(args)
local args = pframe.args -- the arguments passed TO the template, in the wikitext that transcludes the template
local rowdef = routeState.row .. string.format(' id="%s"', anchor)
local notesroute = route(args["notes"] or '')
local gap = gap(args)
if notes ~= '' then
return '|' .. notes
local row = {rowdef, route, gap}
else
return table.concat(row, '\n')
return ''
end
end