Module:DynamicYSWSList

Revision as of 12:59, 5 November 2026 by U080F22CTJN (talk | contribs)

Documentation for this module may be created at Module:DynamicYSWSList/doc

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)
    local data, errors = mw.ext.externalData.getExternalData{
        url = 'https://hackclub.com/api/v1/events',
        format = 'json with jsonpath',
        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
        local msgs = {}
        for _, m in pairs(errors or {}) do msgs[#msgs + 1] = m end
        local msg = '<span class="error">Error fetch failed</span>'
        if #msgs > 0 then msg = msg .. '<br>(' .. table.concat(msgs, '; ') .. ')' end
        return msg
    end

    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
        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
    out[#out + 1] = '|}'

    return frame:preprocess(table.concat(out, '\n'))
end

return p