Module:Sandbox/BrandonXLF/1: Difference between revisions

Content deleted Content added
Faster?
Tag: Reverted
Restored revision 1241724286 by BrandonXLF (Restorer)
 
(45 intermediate revisions by the same user not shown)
Line 1:
-- Sandbox, do not delete
 
require('strict')
 
local p = {}
 
-- Parameters that can be numbered
local monthsAbbr = {
local numeredParams = {
'Jan',
service = true,
'Feb',
dest = true,
'Mar',
note = true
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
}
 
-- Labels for platform types
local months = {
local platformLabels = {
'January',
island = 'Island platform',
'February',
top = 'Side platform',
'March',
bottom = 'Side platform'
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
}
 
-- Generate the HTML to display a service
local function getMaxDay(month, year)
local function displayService(service, dest, note, dir)
local maxDay
if not service then return '' end
return '<div class="service">' ..
if month == 1
'<div class="' .. (dir ~= 'left' and 'hidden' or '') .. '">←</div>' ..
or month == 3
'<div>' .. service .. (dest and ' toward ' .. dest or '') .. (note and ' ' .. note or '') .. '</div>' ..
or month == 5
(dir == 'right' and '<div>→</div>' or '') ..
or month == 7
'</div>'
or month == 8
end
or month == 10
 
or month == 12
-- Generate the HTML to display a track (one or more services)
then
local function displayTrack(floorPart)
maxDay = 31
elseifif monthnot == 2floorPart.service then
return ''
if year % 400 == 0 then
maxDay = 29
elseif year % 100 == 0 then
maxDay = 28
elseif year % 4 == 0 then
maxDay = 29
else
maxDay = 28
end
else
maxDay = 30
end
floorPart.dest = floorPart.dest or {}
return maxDay
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 incrementDate(date)
local function displayFloorPart(floorPart, floorNum, partNum)
date.day = date.day + 1
local out = ''
 
if date.day > date.maxDay then
if floorPart.name then
date.day = 1
return '<div class="track"><div>' .. floorPart.name .. '</div></div>' ..
date.month = date.month + 1
'<div class="track"><div class="desc"><div>' .. (floorPart.desc or '') .. '</div>' .. displayTrack(floorPart) .. '</div></div>'
date.maxDay = getMaxDay(date.month, date.year)
end
if date.month > 12 then
if floorPart.platform then
date.month = 1
if not platformLabels[floorPart.platform] then
date.year = date.year + 1
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 createDate(year, month, day)
local function createArgTable(tbl)
return {
tbl = tbl or {}
day = day,
maxDay = getMaxDay(month, year),
tbl.max = 0
month = month,
tbl.count = 0
year = year
}
return tbl
end
 
-- 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 tbl[num] then
function p.main()
tbl[num] = value
local currentDate = os.date('!*t')
tbl.count = tbl.count + 1
local date = createDate(2010, 12, 1)
end
local out = '<table>'
end
local currentMonth
local currentYear
-- Process an argument with the format level_part_param[paramNum]
while true do
local function processArg(out, level, part, param, paramNum, value)
if date.year > currentDate.year then
level = tonumber(level)
break
part = tonumber(part)
paramNum = paramNum and tonumber(paramNum) or 1
addArg(out, level, createArgTable())
addArg(out[level], part, {})
if numeredParams[param] then
if not out[level][part][param] then
out[level][part][param] = createArgTable()
end
addArg(out[level][part][param], paramNum, value)
if date.year == currentDate.year
else
and date.month > currentDate.month
out[level][part][param] = value
then
end
break
end
-- Process the argument table
local function processArgs(args)
local out = createArgTable()
for i, v in pairs(args) do
if type(i) == 'number' then
addArg(out, i, createArgTable({ letter = v }))
else
local level, part, param, paramNum = i:match('(%d+)_(%d+)_([^%d]+)(%d*)')
if level and part and param then
processArg(out, level, part, param, paramNum, v)
end
end
if date.year == currentDate.year
and date.month == currentDate.month
and date.day > currentDate.day
then
break
end
if date.year ~= currentYear then
out = out .. '</td></tr></table><h2>' .. date.year .. '</h2><table>'
currentYear = date.year
end
if date.month ~= currentMonth then
out = out .. '</td></tr><tr><th scope="row" style="text-align:left;">' .. monthsAbbr[date.month] .. '</th><td>'
currentMonth = date.month
end
local dateString = date.year .. ' ' .. months[date.month] .. ' ' .. date.day
out = out .. '[[Wikipedia:Main Page history/' .. dateString .. '|' .. date.day .. ']]' .. '/[[Wikipedia:Main Page history/' .. dateString .. 'b|b]] '
incrementDate(date)
end
return out .. '</td></tr></table>'
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