Module:ImportPerformances
From Angelina Jordan Wiki
Documentation for this module may be created at Module:ImportPerformances/doc
-- Module:ImportPerformances
-- Reads a wiki page containing JSON (an array of objects) and emits literal transclusion calls
-- of Performance-devel and Video-devel for each object.
-- Usage: {{#invoke:ImportPerformances|import|source=PageWithJSON}}
local p = {}
-- helper to build raw template calls
local function makeTemplate(name, args)
local parts = {}
for k, v in pairs(args) do
if v ~= nil and v ~= '' then
table.insert(parts, '|' .. k .. '=' .. v)
end
end
return '{{' .. name .. table.concat(parts, '') .. '}}'
end
function p.import(frame)
local source = frame.args.source
if not source then
return "Error: missing 'source' argument"
end
local title = mw.title.new(source)
if not title then
return "Error: invalid source page"
end
local raw = title:getContent()
if not raw then
return "Error: no content on source page"
end
-- Decode JSON
local data
if mw.text.jsonDecode then
data = mw.text.jsonDecode(raw)
else
local ok, JSON = pcall(require, 'Module:JSON')
if not ok then
return "Error: JSON decoding not available"
end
data = JSON.decode(raw)
end
if type(data) ~= "table" then
return "Error: JSON did not decode to table"
end
local out = {}
local idCounter, prevSong = 0, nil
for _, entry in ipairs(data) do
idCounter = idCounter + 1
-- Insert heading when song changes
if entry.song ~= prevSong then
table.insert(out, "=== " .. entry.song .. " ===")
prevSong = entry.song
end
-- Build context string
local context = entry.context and table.concat(entry.context, "#") or ""
-- Performance template
local perfArgs = {
song = entry.song,
event = entry.event,
context = context,
date = entry.date,
type = entry.type,
pos = entry.pos,
["with"] = entry["with"],
comment = entry.comment,
id = tostring(idCounter)
}
table.insert(out, makeTemplate('Performance-devel', perfArgs))
-- Videos
local videos = {}
if entry.url then
table.insert(videos, {url = entry.url, duration = entry.duration, quality = entry.quality})
end
if entry.videos then
for _, v in ipairs(entry.videos) do
table.insert(videos, {url = v.url, duration = v.duration, quality = v.quality})
end
end
for _, v in ipairs(videos) do
local vArgs = { id = tostring(idCounter), url = v.url, duration = v.duration, quality = v.quality }
table.insert(out, makeTemplate('Video-devel', vArgs))
end
end
return '<pre>' .. table.concat(out, '\n') .. '</pre>'
end
return p