Module:Jctbtm/sandbox

From blackwiki
Jump to navigation Jump to search

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

local p = {}

local types = mw.loadData("Module:Road data/RJL types")

local row

local warningTypes = {
	col = "C",
	key = "#"
}
local warnings = {}

--- Compute the number of columns in the junction list table.
local function numColumns(args)
	if args.col then
		warnings.col = "col parameter is deprecated"
		return args.col
	end
	-- Minimum number of columns: destination and notes
	local cols = 2
	if args.altunit then
		-- One additional column for alternate unit.
		cols = cols + 1
	elseif args.length ~= "off" then
		-- Two additional columns for unit and its conversion.
		cols = cols + 2
	end
	if args[1] == "old" then
		-- Two additional columns for old and new exit numbers
		cols = cols + 2
	elseif args[1] == "exit" then
		-- One additional column for exit number
		cols = cols + 1
	end
	if args[2] == "name" then
		-- One additional column for named interchange
		cols = cols + 1
	end
	if args.region_col then
		-- One additional column for region
		cols = cols + 1
	end
	if not args.indep_city then
		if not args.sub1 then
			-- One additional column for first-level subdivision
			cols = cols + 1
		end
		if not args.sub2 then
			-- One additional column for second-level subdivision
			cols = cols + 1
		end
	end
	return cols
end

local function parameterParser(args)
	local keysParam = args.keys
	if not keysParam then return {} end
	local keys = mw.text.split(keysParam, ",")
	table.sort(keys)
	return keys
end

local function createLegend(keys)
	local legend = row:tag("li"):tag("ul")
		:addClass('hlist')
		:css("font-size", "90%")
	for _,v in ipairs(keys) do
		local type = types[v]
		if type then
			local key = legend:tag('li')
			key:tag('div')
				:css("display", "inline-block")
				:css("vertical-align", "bottom")
				:css("width", "20px")
				:css("height", "20px")
				:css('border', '1px solid #000')
				:css('background-color', type.color)
			key:wikitext(type.jctbtm)
		end
	end
end

function p._jctbtm(args)
	local cols = numColumns(args)
	local root = mw.html.create()
	-- Define the footer row.
	row = root:tag('th')
		:attr("scope", "row")
		:attr('colspan', cols)
		:addClass("plainlist")
		:css("text-align", "center")
		:tag("ul")

	if args.length ~= "off" and (args.conv or 'yes') == 'yes' then
		local conv = row:tag("li"):tag("ul"):addClass('hlist')
		conv:tag("li"):wikitext("1.000 mi = 1.609 km")
		conv:tag("li"):wikitext("1.000 km = 0.621 mi")
	end

	local keys = parameterParser(args)
	if #keys > 0 then createLegend(keys) end

	local keyParam = args.key
	if keyParam then -- This is a deprecated parameter
		warnings.key = "key parameter is deprecated"
	end

	local notes = args.notes or args.key
	if notes then
		-- Display additional notes if provided.
		row:tag("li"):wikitext(notes)
	end

	-- Prepare warning.
	local warningCode
	for key,msg in pairs(warnings) do
		warningCode = warningCode or warningTypes[key]
	end
	local warning
	if warningCode then
		local page = mw.title.getCurrentTitle()
		local pagename = page.prefixedText
		warning = string.format("[[Category:Jctbtm temporary tracking category|%s %s]]",
			warningCode, pagename)
	else
		warning = ""
	end

	if #row.nodes == 0 then
		return '|-\n|}' .. warning
	end
	return tostring(root) .. '\n|-\n|}' .. warning
end

function p.jctbtm(frame)
	return p._jctbtm(require('Module:Arguments').getArgs(frame))
end

return p