Difference between revisions of "Module:Jctbtm/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Jackmcbarn
(various simplifications)
m (19 revisions imported)
 
(5 intermediate revisions by one other user not shown)
Line 5: Line 5:
 
local row
 
local row
  
local columns = {   -- Constants for how many columns different list types should have.
+
local warningTypes = {
default = 6,   -- default
+
col = "C",
exit = 7,      -- default + exit number
+
key = "#"
old = 8,        -- default + exit number + old exit number
 
 
}
 
}
 +
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 function parameterParser(args)
 
local keysParam = args.keys
 
local keysParam = args.keys
if not(keysParam) then return {} end
+
if not keysParam then return {} end
 
local keys = mw.text.split(keysParam, ",")
 
local keys = mw.text.split(keysParam, ",")
 
table.sort(keys)
 
table.sort(keys)
Line 19: Line 62:
 
end
 
end
  
local function createLegend(key)
+
local function createLegend(keys)
local legend = row:tag('div'):addClass('hlist'):cssText("margin-left:1.6em;text-align:center;font-size:90%"):tag('ul')
+
local legend = row:tag("li"):tag("ul")
for k,v in ipairs(key) do
+
:addClass('hlist')
 +
:css("font-size", "90%")
 +
for _,v in ipairs(keys) do
 
local type = types[v]
 
local type = types[v]
 
if type then
 
if type then
legend:tag('li'):tag('span'):css('border', '1px solid #000'):css('background-color', type.color):css('color', type.color):wikitext("    "):done():wikitext("  "):wikitext(type.jctbtm)
+
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
 
end
Line 30: Line 83:
  
 
function p._jctbtm(args)
 
function p._jctbtm(args)
 +
local cols = numColumns(args)
 
local root = mw.html.create()
 
local root = mw.html.create()
row = root:tag('tr'):tag('td')
+
-- Define the footer row.
local cols = args.col or columns[args[1]] or columns.default -- Compute the number of columns, either from an explicit parameter, or by looking at the columns table.
+
row = root:tag('th')
row:attr('colspan', cols):addClass('wikitable hlist'):css("text-align", "center"):css("background-color", "#f2f2f2") -- Define the footer.
+
:attr("scope", "row")
+
:attr('colspan', cols)
if (args.conv or 'yes') == 'yes' then
+
:addClass("plainlist")
row:wikitext("1.000&nbsp;mi = 1.609&nbsp;km; 1.000&nbsp;km = 0.621&nbsp;mi<br>")
+
: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&nbsp;mi = 1.609&nbsp;km")
 +
conv:tag("li"):wikitext("1.000&nbsp;km = 0.621&nbsp;mi")
 
end
 
end
+
 
local key = parameterParser(args)
+
local keys = parameterParser(args)
if key[1] then createLegend(key) end
+
if #keys > 0 then createLegend(keys) end
+
 
 
local keyParam = args.key
 
local keyParam = args.key
 
if keyParam then -- This is a deprecated parameter
 
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 page = mw.title.getCurrentTitle()
 
local pagename = page.prefixedText
 
local pagename = page.prefixedText
row:wikitext(string.format("[[Category:Jctbtm temporary tracking category|# %s]]", pagename))
+
warning = string.format("[[Category:Jctbtm temporary tracking category|%s %s]]",
 +
warningCode, pagename)
 +
else
 +
warning = ""
 
end
 
end
+
 
row:wikitext(args.notes or args.key) -- If additional notes are provided, display them.
 
 
 
if #row.nodes == 0 then
 
if #row.nodes == 0 then
return '|-\n|}'
+
return '|-\n|}' .. warning
else
 
return tostring(root) .. '\n|-\n|}'
 
 
end
 
end
 +
return tostring(root) .. '\n|-\n|}' .. warning
 
end
 
end
  

Latest revision as of 05:49, 27 September 2020

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&nbsp;mi = 1.609&nbsp;km")
		conv:tag("li"):wikitext("1.000&nbsp;km = 0.621&nbsp;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