Module:DynamicYSWSList: Difference between revisions

gorkie's hope for a fix
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,'
             .. 'link=$.data[*].url,date=$.data[*].startDate'
             .. '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 errors and #errors > 0 then
         if #msgs > 0 then msg = msg .. '<br>(' .. table.concat(msgs, '; ') .. ')' end
            msg = msg .. '<br>(' .. table.concat(errors, '; ') .. ')'
        end
         return msg
         return msg
     end
     end


     local rows = { '=== All Hack Club Events ===' }
     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
         table.insert(rows, string.format('* [%s %s] %s (%s)',
         if not want or row.status == want then
            row.link or '#', row.name or 'Untitled', row.description or '', row.date or ''))
            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(rows, '\n'))
     return frame:preprocess(table.concat(out, '\n'))
end
end


return p
return p