Modul:Team appearances list/show
Tampilan
-- For testing, show results using data from [[Modul:Team appearances list/data]].
local Collection = {}
Collection.__index = Collection
do
function Collection:add(item)
if item ~= nil then
self.n = self.n + 1
self[self.n] = item
end
end
function Collection:join(sep)
return table.concat(self, sep)
end
function Collection:sort(comp)
table.sort(self, comp)
end
function Collection.new()
return setmetatable({n = 0}, Collection)
end
end
local function get_page_content(page_title)
local t = mw.title.new(page_title)
if t then
local content = t:getContent()
if content then
if content:sub(-1) ~= '\n' then
content = content .. '\n'
end
return content
end
end
error('Could not read wikitext from "[[' .. page_title .. ']]".', 0)
end
local function make_data(modname)
-- Return a list of tables for each competition/team in the order used in
-- the data module, based on a hope that the data module is consistently
-- indented with one tab before competitions and two or more before teams,
-- and quotes (") are used for strings (not apostrophes).
local content = get_page_content(modname)
local lnum = 0
local current_games
local items = Collection.new()
for line in string.gmatch(content, '(.-)\n') do
lnum = lnum + 1
local indent, title = line:match('^(\t+)%["(.-)"%]')
if indent then
if indent == '\t' then
current_games = title
else
items:add({
competition = current_games,
team = title,
line = lnum,
})
end
end
end
return items
end
local function main(frame)
local title = frame.args[1]
if title == nil or title == '' then
return 'Error: Parameter 1 must specify name of data module'
end
local sandbox = title:find('sandbox', 1, true) and '/sandbox' or ''
local lister = require('Modul:Team appearances list' .. sandbox)._main
local lines = Collection.new()
local current_games
for _, item in ipairs(make_data(title)) do
if current_games ~= item.competition then
current_games = item.competition
lines:add('==' .. current_games .. '==')
end
lines:add(item.team .. ' (line ' .. item.line .. ') ' .. lister(item))
end
return lines:join('\n')
end
return { main = main }