Module:DynamicYSWSList: Difference between revisions
U080F22CTJN (talk | contribs) gorkie's hope for a fix |
U080F22CTJN (talk | contribs) mNo edit summary |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- one cell: collapse newlines (they break rows), nowiki anything markup-shaped | |||
local function cell(s) | |||
if s == nil or s == '' then return '—' end | |||
return mw.text.nowiki((tostring(s):gsub('[\r\n]+', ' '))) | |||
end | |||
function p.fetchlist(frame) | function p.fetchlist(frame) | ||
| Line 5: | Line 11: | ||
url = 'https://hackclub.com/api/v1/events', | url = 'https://hackclub.com/api/v1/events', | ||
format = 'json with jsonpath', | format = 'json with jsonpath', | ||
data = 'name=$.data[*].name,description=$.data[*].description,' | data = 'name=$.data[*].name,description=$.data[*].description,link=$.data[*].url,' | ||
.. ' | .. 'start=$.data[*].startDate,finish=$.data[*].endDate,status=$.data[*].status' | ||
} | } | ||
if not data or #data == 0 then | if not data or #data == 0 then | ||
local msgs = {} | |||
for _, m in pairs(errors or {}) do msgs[#msgs + 1] = m end | |||
local msg = '<span class="error">Error fetch failed</span>' | local msg = '<span class="error">Error fetch failed</span>' | ||
if | if #msgs > 0 then msg = msg .. '<br>(' .. table.concat(msgs, '; ') .. ')' end | ||
return msg | return msg | ||
end | end | ||
local | local want = frame.args and frame.args.status -- optional, e.g. |status=ongoing | ||
local out = { | |||
'{| class="wikitable sortable"', | |||
'! Event !! Description !! Status !! Starts !! Ends' | |||
} | |||
for _, row in ipairs(data) do | for _, row in ipairs(data) do | ||
if not want or row.status == want then | |||
local name = cell(row.name) | |||
local title = (row.link and row.link ~= '') and ('[' .. row.link .. ' ' .. name .. ']') or name | |||
out[#out + 1] = '|-\n| ' .. title | |||
.. '\n| ' .. cell(row.description) | |||
.. '\n| ' .. cell(row.status) | |||
.. '\n| ' .. cell(row.start) | |||
.. '\n| ' .. cell(row.finish) | |||
end | |||
end | end | ||
out[#out + 1] = '|}' | |||
return frame:preprocess(table.concat( | return frame:preprocess(table.concat(out, '\n')) | ||
end | end | ||
return p | return p | ||