Module:Sandbox/BrandonXLF/1: Difference between revisions

Content deleted Content added
No edit summary
Restored revision 1241724286 by BrandonXLF (Restorer)
 
(557 intermediate revisions by the same user not shown)
Line 1:
-- Sandbox, do not delete
-- Keep Here
--[=[
{{#invoke:Language/name/data/ISO 639-3 data extraction tool|ISO_639_3_extract|file-date=20170217}}
 
require('strict')
reads a local copy of iso-639-3_Name_Index_YYYYMMDD.tab where (YYYYMMDD is the release date). Download that file
in zip form from http://www-01.sil.org/iso639-3/download.asp (use the UTF-8 zip)
 
local p = {}
useful lines in the file have the form:
 
<id>\t<name>\t<inverted name>\n
-- Parameters that can be numbered
where:
local numeredParams = {
<id> is the three-character ISO 639-3 language code
service = true,
<name> is the language 'name'
dest = true,
<inverted name> is the language in 'last-name, first-name(s)' form; this part ignored
note = true
}
 
-- 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">' ..
like this:
'<div class="' .. (dir ~= 'left' and 'hidden' or '') .. '">←</div>' ..
aaq Eastern Abnaki Abnaki, Eastern
'<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)
when a language code has more than one name, the code is repeated for each additional name:
local function displayTrack(floorPart)
rar Cook Islands Maori Maori, Cook Islands
if not floorPart.service then
rar Rarotongan Rarotongan
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 displayFloorPart(floorPart, floorNum, partNum)
local out = ''
if floorPart.name then
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 .. '_'
function p.ISO_639_3_extract (frame)
local page = mw.title.getCurrentTitle(); -- get a page object for this page
return '<div class="part-error">Invalid floor part! Must have either <code>' .. prefix .. 'name</code> or <code>' .. prefix .. 'platform</code></div>.'
local content = page:getContent(); -- get unparsed content
end
local lang_table = {}; -- languages go here
 
-- Generate the HTML to display a floor
local code;
local function displayFloor(stationFloor, num, last)
local names;
local out = '<div class="floor' .. (last and ' last' or '') .. '" style="grid-row: span ' .. stationFloor.count .. ';">' .. stationFloor.letter .. '</div>'
 
for i = 1, stationFloor.max do
local file_date = 'File-Date: ' .. frame.args["file-date"]; -- set the file date line from |file-date=
out = out .. displayFloorPart(stationFloor[i], num, i)
end
return out
end
 
-- Create a table that can hold numbered arguments
for code, name in mw.ustring.gmatch (content, '(%a+)') do -- get code and 'forward' name
local function createArgTable(tbl)
if code then
tbl = tbl or {}
if string.find (lang_table[#lang_table] or '', '^%[\"' .. code) then -- if this is an additional name for code ('or' empty string for first time when lang_table[#lang_table] is nil)
lang_table[#lang_table] = mw.ustring.gsub (lang_table[#lang_table], '}$', ''); -- remove trailing brace from previous name
tbl.max = 0
lang_table[#lang_table] = lang_table[#lang_table] .. ', \"' .. name .. '\"}'; -- add this name with new brace
tbl.count = 0
else
table.insert (lang_table, "[\"" .. code .. "\"] = {\"" .. name .. "\"}"); -- make new table entry
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
tbl[num] = value
tbl.count = tbl.count + 1
end
end
-- Process an argument with the format level_part_param[paramNum]
local function processArg(out, level, part, param, paramNum, value)
level = tonumber(level)
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)
else
out[level][part][param] = value
end
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
elseif not code then
table.insert (lang_table, "[\"error\"] = {" .. record .. "}"); -- code should never be nil, but inserting an error entry in the final output can be helpful
end
end
-- make pretty output
return out
return "<br /><pre>-- " .. file_date .. "<br />return {<br />&#9;" .. table.concat (lang_table, ',<br />&#9;') .. "<br />&#9;}<br />" .. "</pre>";
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
 
return p;