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 transclusions
-- of Performance-devel and Video-devel for each object.
-- Usage: {{#invoke:ImportPerformances|import|source=PageWithJSON}}
local p = {}
-- Helper: safe tostring
local function s(v)
if v == nil then return nil end
return tostring(v)
end
function p.import(frame)
local args = frame.args or {}
local source = args.source or args[1]
if not source or source == "" then
return "Error: supply a 'source' page name containing JSON, e.g. |source=Module:MyData"
end
local titleObj = mw.title.new(source)
if not titleObj then
return "Error: invalid page name: " .. source
end
local raw = titleObj:getContent()
if not raw then
return "Error: could not read page content: " .. source
end
local ok, json = pcall(require, 'Module:JSON')
if not ok or not json or type(json.decode) ~= 'function' then
return "Error: Module:JSON not available or does not provide decode(). Install a JSON parser module."
end
local ok2, data = pcall(json.decode, raw)
if not ok2 then
return "Error decoding JSON: " .. tostring(data)
end
if type(data) ~= 'table' then
return "Error: JSON must be an array of objects"
end
local out = {}
local id = 0
for _, obj in ipairs(data) do
id = id + 1
-- Build args for Performance-devel. Only include keys that exist in JSON.
local perfArgs = {}
if obj.song ~= nil then perfArgs.song = s(obj.song) end
if obj.event ~= nil then perfArgs.event = s(obj.event) end
if obj.context ~= nil then
if type(obj.context) == 'table' then
-- Preserve empty strings so that table.concat yields '##' when required
local ctx = {}
for i, e in ipairs(obj.context) do ctx[i] = (e == nil and '' or s(e)) end
perfArgs.context = table.concat(ctx, '#')
else
perfArgs.context = s(obj.context)
end
end
if obj.date ~= nil then perfArgs.date = s(obj.date) end
if obj.type ~= nil then perfArgs["type"] = s(obj.type) end
if obj.pos ~= nil then perfArgs.pos = s(obj.pos) end
if obj["with"] ~= nil then perfArgs["with"] = s(obj["with"]) end
if obj.comment ~= nil then perfArgs.comment = s(obj.comment) end
-- local id passed as 'id' parameter to the templates
perfArgs.id = tostring(id)
-- Expand the Performance-devel template with the constructed args.
local perfW = frame:expandTemplate{ title = 'Performance-devel', args = perfArgs }
table.insert(out, perfW)
-- Helper to add videos. Duration may come from object or individual video entry.
local function addVideo(url, duration)
if not url or url == '' then return end
local vArgs = { id = tostring(id), url = s(url) }
if duration ~= nil and duration ~= '' then vArgs.duration = s(duration) end
local vW = frame:expandTemplate{ title = 'Video-devel', args = vArgs }
table.insert(out, vW)
end
-- Single top-level URL
if obj.url ~= nil and obj.url ~= '' then
addVideo(obj.url, obj.duration)
end
-- Multiple videos
if obj.videos ~= nil and type(obj.videos) == 'table' then
for _, v in ipairs(obj.videos) do
if v ~= nil then
local vurl = v.url or v["url"]
local vdur = v.duration or obj.duration
addVideo(vurl, vdur)
end
end
end
-- Separator blank line (keeps output readable and matches your example)
table.insert(out, "")
end
return table.concat(out, "\n\n")
end
return p