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

local p = {}
local mw = require('mw')

function p.getConfig(siteName)
    local siteConfig = mw.loadData("Module:CareerList/config")[siteName]
    if not siteConfig then
        error(string.format("Site '%s' not found in configuration file", siteName))
    end
    return siteConfig
end

function p.makeUrl(siteName, id)
    local siteConfig = p.getConfig(siteName)
    local url = siteConfig.url
    local variable = siteConfig.variable
    local idPlaceholder = string.find(url, variable)
    if idPlaceholder == nil then
        error(string.format("No id placeholder found in URL for site '%s'", siteName))
    end
    return string.sub(url, 1, idPlaceholder - 1) .. id .. string.sub(url, idPlaceholder + string.len(variable))
end

function p.makeTable(frame)
    local data = mw.html.create("table")
    data
        :addClass("wikitable")
        :addClass("sortable")
        :addClass("career-new")
        :tag("tr")
            :tag("th")
                :wikitext(frame.args.title or "")
            :done()
    for _, row in ipairs(frame.args.list or {}) do
        local siteName = row.site
        local id = row.id
        local alias = row.alias or ""
        local notes = row.notes or ""
        local url = p.makeUrl(siteName, id)
        data
            :tag("tr")
                :tag("td")
                    :wikitext(row.site)
                :done()
                :tag("td")
                    :wikitext(row.id)
                :done()
                :tag("td")
                    :wikitext(row.alias)
                :done()
                :tag("td")
                    :wikitext(row.notes)
                :done()
    end
    return tostring(data)
end

function p.main(frame)
    return p.makeTable(frame)
end

return p