Difference between revisions of "Module:Jctbtm/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>Happy5214
(Create sandbox version of Module:Jctbtm)
 
m (19 revisions imported)
 
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
  
local colors = {trans = "#dff9f9", concur = "#ddffdd", closed = "#d3d3d3", incomplete = "#ffdddd",
+
local types = mw.loadData("Module:Road data/RJL types")
                unbuilt = "#ffdead", hov = "#ffff99", toll = "#dcdcfe", etc = "#dcdcfe"}
 
  
local descriptions = {trans = "[[Route number|Route]] transition", concur = "[[Concurrency (road)|Concurrency]] terminus",
+
local row
                      closed = "Closed/former", incomplete = "[[Interchange (road)#Complete and incomplete interchanges|Incomplete access]]",
 
                      unbuilt = "Unopened", hov = "[[HOV|HOV only]]", toll = "Tolled", etc = "[[Electronic toll collection|ETC]]"}
 
  
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)
    return keys
+
return keys
 
end
 
end
  
local function createLegend(key)
+
local function createLegend(keys)
    local insert = table.insert
+
local legend = row:tag("li"):tag("ul")
    local format = mw.ustring.format
+
:addClass('hlist')
    local legend = {'<div class="hlist" style="margin-left: 1.6em; text-align: center; font-size:90%;">'}
+
:css("font-size", "90%")
    for k,v in ipairs(key) do
+
for _,v in ipairs(keys) do
        if colors[v] then
+
local type = types[v]
            insert(legend, format('*<span style="border:1px solid #000; background-color:%s; color:%s;">&nbsp;&nbsp;&nbsp;&nbsp;</span>&nbsp; %s', colors[v], colors[v], descriptions[v]))
+
if type then
        end
+
local key = legend:tag('li')
    end
+
key:tag('div')
    insert(legend, "</div>")
+
:css("display", "inline-block")
    return table.concat(legend, "\n")
+
: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
 
end
  
 
function p.jctbtm(frame)
 
function p.jctbtm(frame)
    local pframe = frame:getParent()
+
return p._jctbtm(require('Module:Arguments').getArgs(frame))
    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
 
   
 
    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.
 
    local tabdef = '|-\n|colspan = ' .. cols .. ' class="wikitable hlist" style="text-align: center; background-color:#f2f2f2;"|' -- Define the table.
 
   
 
    local conversion = ''
 
    local conv = args.conv or 'yes'
 
    if conv == 'yes' then
 
        conversion = "1.000&nbsp;mi = 1.609&nbsp;km; 1.000&nbsp;km = 0.621&nbsp;mi<br>"
 
    end
 
   
 
    local text = tabdef .. conversion
 
   
 
    local key = parameterParser(args)
 
    if key[1] then
 
        local legend = createLegend(key)
 
        text = text .. legend
 
    end
 
   
 
    local keyParam = args.key or ''
 
    if keyParam ~= '' then -- This is a deprecated parameter
 
        local page = mw.title.getCurrentTitle()
 
        local pagename = page.prefixedText
 
        text = text .. string.format("[[Category:Jctbtm temporary tracking category|# %s]]", pagename)
 
    end
 
   
 
    local notes = args.notes or args.key or ''
 
    if notes ~= '' then
 
        text = text .. notes -- If additional notes are provided, display them.
 
    end
 
   
 
    if text == tabdef then
 
        return "|}"
 
    else
 
        return text .. "\n|}"
 
    end
 
 
end
 
end
  
 
return p
 
return p

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