Jump to content

Module:Routelist row/sandbox 2

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fredddie (talk | contribs) at 09:01, 31 July 2021 (everything but the route function is from sandbox 1). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {} -- Package to be exported

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 roadDataModule = require("Module:Road data/sandbox")
local util = require("Module:Road data/util")

local shieldScale = 2

--[[-
@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>&thinsp;" .. 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 mi = args["length_mi"] -- Length in miles
	local km = args["length_km"] -- Length in kilometers
	-- Convert lengths.
	local lengths = util.convertLengths({mi = mi, km = km}, '—')

	local lengthRef = args["length_ref"] or ''
	return format("|align=right|%s%s%s||align=right|%s",
		lengths[lengths.orig], lengthRef, lengths.error or "", lengths[lengths.comp])
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), "&nbsp;", " "),
		"- ")
end

function route(args)
	-- This function displays the shield and link.
	local format = mw.ustring.format	
	local parserModule = require "Module:Road data/parser"
	local parser = parserModule.parser
	
	local noshield = args.noshield
	local bannerFile = parser(args, 'banner')
	local banner
	if not noshield and bannerFile and bannerFile ~= '' then
		local widthCode = parser(args, 'width') or 'square'
		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, 'shield')
		if 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
		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 '!scope="row" class="nowrap"|' .. route
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