Module:Routelist row: Difference between revisions

Content deleted Content added
Undid revision 1005359708 by Fredddie (talk)
drop nowrap - was causing horizontal scrolling on List of Ontario provincial highways for 407
 
(22 intermediate revisions by one other user not shown)
Line 2:
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 frame = mw.getCurrentFrame()
 
local routeStates = { } -- Table with route statuses.
Line 30 ⟶ 32:
return routeStates.current
end
end
 
function getLength(args)
-- This function is passed the length fields from the {{routelist row}} invocation, and calculates the missing length from the other.
local math = require "Module:Math" -- This module contains functions needed later in this function.
local precision = math._precision -- The math._precision function provides the precision of a given string representing a number.
local round = math._precision_format -- This method rounds a given number to the given number of digits. In Lua, storing these functions locally results in more efficient execution.
local length = {} -- This table will store the computed lengths.
local km = args["length_km"] -- The kilometer length from the {{routelist row}} call.
local mi = args["length_mi"] -- The length in miles as passed to {{routelist row}}.
if not km then -- This signifies that a length in kilometers was not passed.
local n = tonumber(mi) -- The first step is to convert the miles (passed as a string from the template) into a number.
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.
local ten_mult = (n % 10 == 0) -- Rounding is handled differently if the input distance is a multiple of 10.
local prec = precision(mi) -- Retrieve the precision of the passed mile value (as a string).
if ten_mult and prec < 0 then -- If the distance is a multiple of 10 and a whole number...
prec = prec + 1 -- Add a digit to the precision.
end
length.km = round(tostring(n * 1.609344), tostring(prec)) -- Compute and round the length in kilometers, and store it in the length table.
else -- No mile value was passed
length.km = '—'
end
else -- If the length in kilometers was passed, the computed lengths table will simply contain the passed length.
local prec = precision(km)
length.km = round(km, tostring(prec))
end
if not mi then -- The same as above, but this time converting kilometers to mile if necessary.
local n = tonumber(km) -- Kilometers as a number
if n then -- If a kilometer value was passed:
local ten_mult = (n % 10 == 0) -- Rounding is handled differently if the input distance is a multiple of 10.
local prec = precision(km) -- Precision of the passed length
if ten_mult and prec < 0 then -- If the distance is a multiple of 10 and a whole number...
prec = prec + 1 -- Add a digit to the precision
end
length.mi = round(tostring(n / 1.609344), tostring(prec)) -- Compute and store the conversion into miles.
else -- If not:
length.mi = '—' -- Store a dash.
end
else -- And if the length in miles was passed:
local prec = precision(mi) -- Get the precision...
length.mi = round(mi, tostring(prec)) -- and format it appropriately
end
return length -- Return the length table with the computed lengths.
end
 
Line 148 ⟶ 107:
function dates(established, decommissioned, routeState, args)
-- This function displays the date columns.
local established_ref = args.established_ref or '' -- Reference for date established
local decommissioned_ref = args.decommissioned_ref or '' -- Reference for date decommissioned
if args.gazette == 'yes' then
return "|align=center|" .. formed(routeState, established, args.circa_established) ..
local established = args.established or "—"
established_ref .. "||align=center|" .. removed(routeState, decommissioned, args.circa_decommissioned) ..
local established_ref = args.established_ref or ''
decommissioned_ref
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 length(args)
local function length(args)
-- This function generate the length columns, with the appropriate conversions.
local mileskm = args["length_milength_km"] or '' -- Length in mileskilometers
local kilometersmi = args["length_kmlength_mi"] or '' -- Length in kilometersmiles
local ref = args["length_ref" ] or ''
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,
if mi == '' and km == '' then
return format("|align=right|—||align=right|—")
local lengthRef = args["length_ref"] or ''
elseif mi ~= '0' and km == '' then
local first, second
return format("|align=right|") .. mi .. ref .. format("||align=right|") .. frame:expandTemplate{ title = 'convert', args = { mi, "mi", "km", disp = "output number only"}}
if kilometers then
first = Lengths.km
second = Lengths.mi
else
return format("|align=right|") .. km .. ref .. format("||align=right|") .. frame:expandTemplate{ title = 'convert', args = { km, "km", "mi", disp = "output number only"}}
first = Lengths.mi
second = Lengths.km
end
return "|align=right|" .. first .. lengthRef .. "||align=right|" .. second -- which are then spliced in here and returned to the template.
end
 
Line 194 ⟶ 157:
else
return '' --create no cell
end
end
 
function gap(args)
local text = args.text or "''Number not designated''"
if notes then
return '|align=center colspan=7|' .. text --display notes in cell
else
return '|align=center colspan=6|' .. text --display notes in cell
end
end
Line 252 ⟶ 225:
local shield
if not noshield then
local shieldFile, second = parser(args, 'shieldlist') or parser(args, 'shield') or ''
if type(shieldFile) == nil or shieldFile == 'table' then
shield = ''
elseif type(shieldFile) == 'table' then
shieldFile, second = shieldFile[1], shieldFile[2]
end
Line 260 ⟶ 235:
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 ''
Line 279 ⟶ 256:
local sortedLink = format("<span data-sort-value=\"%s&#32;!\">%s</span>", sortkey, link)
local route = banner .. shield .. ' ' .. sortedLink
return '!scope="row" class="nowrap"|' .. route
end
 
Line 299 ⟶ 276:
local row = {rowdef, route, length, termini, localname, dates, notes}
return table.concat(row, '\n')
end
 
function p.gap(frame)
local args = getArgs(frame) -- Gather passed arguments into easy-to-use table
 
local routeState = getRouteState(established, decommissioned)
local anchor = args.anchor or sortkey(args)
local rowdef = routeState.row .. string.format(' id="%s"', anchor)
local route = route(args)
local gap = gap(args)
local row = {rowdef, route, gap}
return table.concat(row, '\n')
end