Difference between revisions of "Module:Jctbtm/sandbox"
Jump to navigation
Jump to search
blackwiki>Jackmcbarn (various simplifications) |
blackwiki>Chinissai (Better calculation of number of columns.) |
||
| Line 5: | Line 5: | ||
local row | local row | ||
| − | local | + | 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 | ||
| + | else | ||
| + | -- 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 | + | 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( | + | local function createLegend(keys) |
| − | local legend = row:tag('div'):addClass('hlist'): | + | local legend = row:tag("li"):tag('div') |
| − | for | + | :addClass('hlist') |
| + | :css("text-align", "center") | ||
| + | :css("font-size", "90%") | ||
| + | :tag("ul") | ||
| + | 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(" ") | + | local key = legend:tag('li') |
| + | key:tag('span') | ||
| + | :css('border', '1px solid #000') | ||
| + | :css('background-color', type.color) | ||
| + | :css('color', type.color) | ||
| + | :wikitext(" ") | ||
| + | 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(' | + | -- Define the footer row. |
| − | + | row = root:tag('th') | |
| − | + | :attr("scope", "row") | |
| − | + | :attr('colspan', cols) | |
| + | :addClass("plainlist") | ||
| + | :css("text-align", "center") | ||
| + | :tag("ul") | ||
| + | |||
if (args.conv or 'yes') == 'yes' then | if (args.conv or 'yes') == 'yes' then | ||
| − | row:wikitext("1.000 mi = 1.609 km; 1.000 km = 0.621 mi | + | row:tag("li") |
| + | :wikitext("1.000 mi = 1.609 km; 1.000 km = 0.621 mi") | ||
end | end | ||
| − | + | ||
| − | local | + | local keys = parameterParser(args) |
| − | if | + | 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 | ||
| − | local | + | warnings.key = "key parameter is deprecated" |
| − | + | end | |
| − | row: | + | |
| + | local notes = args.notes or args.key | ||
| + | if notes then | ||
| + | -- Display additional notes if provided. | ||
| + | row:tag("li"):wikitext(notes) | ||
end | end | ||
| − | + | ||
| − | + | -- Prepare warning. | |
| − | + | local warning | |
| + | if #warnings > 0 then | ||
| + | local warningCode | ||
| + | for key,msg in pairs(warnings) do | ||
| + | warningCode = warningCode or warningTypes[key] | ||
| + | end | ||
| + | if warningCode then | ||
| + | local page = mw.title.getCurrentTitle() | ||
| + | local pagename = page.prefixedText | ||
| + | warning = string.format("[[Category:Jctbtm temporary tracking category|# %s]]", pagename) | ||
| + | else | ||
| + | warning = "" | ||
| + | end | ||
| + | else | ||
| + | warning = "" | ||
| + | end | ||
| + | |||
if #row.nodes == 0 then | if #row.nodes == 0 then | ||
| − | return '|-\n|}' | + | return '|-\n|}' .. warning |
| − | |||
| − | |||
end | end | ||
| + | return tostring(root) .. '\n|-\n|}' .. warning | ||
end | end | ||
Revision as of 22:35, 14 May 2016
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
else
-- 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('div')
:addClass('hlist')
:css("text-align", "center")
:css("font-size", "90%")
:tag("ul")
for _,v in ipairs(keys) do
local type = types[v]
if type then
local key = legend:tag('li')
key:tag('span')
:css('border', '1px solid #000')
:css('background-color', type.color)
:css('color', type.color)
:wikitext(" ")
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.conv or 'yes') == 'yes' then
row:tag("li")
:wikitext("1.000 mi = 1.609 km; 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 warning
if #warnings > 0 then
local warningCode
for key,msg in pairs(warnings) do
warningCode = warningCode or warningTypes[key]
end
if warningCode then
local page = mw.title.getCurrentTitle()
local pagename = page.prefixedText
warning = string.format("[[Category:Jctbtm temporary tracking category|# %s]]", pagename)
else
warning = ""
end
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