Module:Sandbox/BrandonXLF/1: Difference between revisions

Content deleted Content added
No edit summary
Restored revision 1241724286 by BrandonXLF (Restorer)
 
(39 intermediate revisions by the same user not shown)
Line 5:
local p = {}
 
-- Parameters that can be numbered
local function displayFloorPart(floorPart)
local numeredParams = {
if floorPart.name then
service = true,
return floorPart.name .. ' ' .. floorPart.desc
dest = true,
elseif floorPart.dir then
note = true
return floorPart.dir .. ' ' .. floorPart.desc
}
else
 
return 'Invalid floor part! Must have either <code>x_y_name</code> or <code>x_y_dir</code>.'
-- Labels for platform types
local platformLabels = {
island = 'Island platform',
top = 'Side platform',
bottom = 'Side platform'
}
 
-- Generate the HTML to display a service
local function displayService(service, dest, note, dir)
if not service then return '' end
return '<div class="service">' ..
'<div class="' .. (dir ~= 'left' and 'hidden' or '') .. '">←</div>' ..
'<div>' .. service .. (dest and ' toward ' .. dest or '') .. (note and ' ' .. note or '') .. '</div>' ..
(dir == 'right' and '<div>→</div>' or '') ..
'</div>'
end
 
-- Generate the HTML to display a track (one or more services)
local function displayTrack(floorPart)
if not floorPart.service then
return ''
end
floorPart.dest = floorPart.dest or {}
floorPart.note = floorPart.note or {}
local out = ''
for i = 1, floorPart.service.max do
out = out .. displayService(floorPart.service[i], floorPart.dest[i], floorPart.note[i], floorPart.dir)
end
return out
end
 
-- Generate the HTML to generate a "row" of the floor
local function displayFloor(stationFloor, number)
local function displayFloorPart(floorPart, floorNum, partNum)
local out = ''
if floorPart.name then
out = out .. stationFloor.letter
return '<div class="track"><div>' .. floorPart.name .. '</div></div>' ..
'<div class="track"><div class="desc"><div>' .. (floorPart.desc or '') .. '</div>' .. displayTrack(floorPart) .. '</div></div>'
end
if floorPart.platform then
if not platformLabels[floorPart.platform] then
return '<div class="part-error">Invalid platform type!</div>'
end
return '<div class="platform ' .. floorPart.platform .. '">' ..
platformLabels[floorPart.platform] ..
(floorPart.accessible and ' ' .. mw.getCurrentFrame():expandTemplate{ title = 'access icon' } or '') ..
(floorPart.note and ', ' .. floorPart.note or '') ..
'</div>'
end
 
local prefix = floorNum .. '_' .. partNum .. '_'
return '<div class="part-error">Invalid floor part! Must have either <code>' .. prefix .. 'name</code> or <code>' .. prefix .. 'platform</code></div>.'
end
 
-- Generate the HTML to display a floor
local function displayFloor(stationFloor, num, last)
local out = '<div class="floor' .. (last and ' last' or '') .. '" style="grid-row: span ' .. stationFloor.count .. ';">' .. stationFloor.letter .. '</div>'
 
for i = 1, stationFloor.max do
out = out .. displayFloorPart(stationFloor[i], num, i)
end
return out
end
 
-- Create a table that can hold numbered arguments
local function createArgTable(tbl)
tbl = tbl or {}
tbl.max = 0
local function processArg(out, level, levelPart, param, value)
tbl.count = 0
local level = tonumber(level)
local levelPart = tonumber(levelPart)
return tbl
if level > out.max then
end
out.max = level
 
-- Add an argument number to a table of numbered arguments
local function addArg(tbl, num, value)
if num > tbl.max then
tbl.max = num
end
if not outtbl[levelnum] then
outtbl[levelnum] = { max = 0 }value
tbl.count = tbl.count + 1
end
end
-- Process an argument with the format level_part_param[paramNum]
if levelPart > out[level].max then
local function processArg(out, level, part, param, paramNum, value)
out[level].max = levelPart
level = tonumber(level)
end
part = tonumber(part)
paramNum = paramNum and tonumber(paramNum) or 1
addArg(out, level, createArgTable())
if not out[level][levelPart] then
addArg(out[level][levelPart], =part, {})
end
out[level][levelPart]if numeredParams[param] = valuethen
if not out[level][part][param] then
out[level][part][param] = createArgTable()
end
addArg(out[level][part][param], paramNum, value)
else
out[level][part][param] = value
end
end
-- Process the argument table
local function processArgs(args)
local out = { max = 0 }createArgTable()
for i, v in pairs(args) do
if type(i) == 'number' then
addArg(out, i, createArgTable({ letter = v }))
local level = tonumber(i)
if not out[level] then
out[level] = { max = 0 }
end
out[level].letter = v
else
local level, levelPartpart, param, paramNum = i:match('(%d+)_(%d+)_(.[^%d]+)(%d*)')
if level and part and param then
processArg(out, level, levelPartpart, param, paramNum, v)
end
end
Line 74 ⟶ 146:
end
 
-- Main function, called by wikitext
 
function p.main(frame)
local sortedArgs = processArgs(frame.args)
local out = ''frame:extensionTag{
name = 'templatestyles',
args = { src = 'User:BrandonXLF/styles.css' }
}
out = out .. '<div class="station-layout">'
for i = 1, sortedArgs.max do
out = out .. displayFloor(sortedArgs[i], i, i == sortedArgs.max)
end
return out .. '</div>'
end