Module:CareerList: Difference between revisions
Jump to navigation
Jump to search
PeaceDeadC (talk | contribs) No edit summary |
PeaceDeadC (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
-- Load the configuration file |
|||
function p.getSiteConfig(siteName) |
|||
local cfg = mw.loadData('Module:CareerList/config') |
|||
⚫ | |||
-- Function to build a row of the table |
|||
error('Unknown site: ' .. siteName) |
|||
function buildRow(site, id, alias, notes, index) |
|||
-- Determine the row color |
|||
local color |
|||
⚫ | |||
color = (index % 2 == 1) and cfg.colors.website1 or cfg.colors.website2 |
|||
⚫ | |||
color = (index % 2 == 1) and cfg.colors.studio1 or cfg.colors.studio2 |
|||
end |
end |
||
⚫ | |||
-- Build the row |
|||
⚫ | |||
cfg.rowTemplate, |
|||
⚫ | |||
site == 'Website' and cfg.colors.websiteBg or cfg.colors.studioBg, |
|||
⚫ | |||
cfg.siteUrls[site][1], |
|||
cfg.siteUrls[site][2]:gsub('$1', id), |
|||
⚫ | |||
⚫ | |||
) |
|||
⚫ | |||
end |
end |
||
-- Main function |
|||
function p.makeLink(siteName, id) |
|||
⚫ | |||
local siteConfig = p.getSiteConfig(siteName) |
|||
-- Get the template parameters |
|||
local url = siteConfig.url:gsub('%$1', id) |
|||
⚫ | |||
return string.format('[[%s|%s]]', url, siteName) |
|||
end |
|||
-- Get the list parameter |
|||
function p.getCellBgColor(siteName) |
|||
local siteConfig = p.getSiteConfig(siteName) |
|||
return siteConfig.type == 'studio' and '#DADADA' or '#E7E7E7' |
|||
end |
|||
function p.getTableHtml(args) |
|||
local list = args.list or error('List not specified') |
local list = args.list or error('List not specified') |
||
-- Split the list into rows |
|||
local rows = mw.text.split(list, '%s*' .. cfg.rowSeparator .. '%s*') |
|||
local siteName, id, alias, notes |
|||
if type(item) == 'table' then |
|||
-- Build the table |
|||
siteName = item.site or error('Site not specified') |
|||
local index = 1 |
|||
id = item.id or error('ID not specified') |
|||
local tableContent = '' |
|||
⚫ | |||
for _, row in ipairs(rows) do |
|||
⚫ | |||
local params = mw.text.split(row, '%s*|%s*') |
|||
⚫ | |||
if #params >= 3 then |
|||
⚫ | |||
⚫ | |||
local alias = params[3] |
|||
local notes = params[4] or '' |
|||
tableContent = tableContent .. buildRow(site, id, alias, notes, index) |
|||
⚫ | |||
end |
end |
||
local link = p.makeLink(siteName, id) |
|||
local cellBgColor = p.getCellBgColor(siteName) |
|||
local rowBgColor = #rows % 2 == 0 and '#FFFFFF' or '#F3F3F3' |
|||
table.insert(rows, string.format([[ |
|||
<tr style="background-color:%s;"> |
|||
<td style="background-color:%s;">%s</td> |
|||
<td style="background-color:%s;">%s</td> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
]], rowBgColor, cellBgColor, siteName, cellBgColor, link, alias, notes)) |
|||
end |
end |
||
⚫ | |||
-- Build the table |
|||
<table style="width:100%%;border-collapse:collapse;"> |
|||
local tableHtml = string.format(cfg.tableTemplate, tableContent) |
|||
<tr style="background-color:#F2F2F2;"> |
|||
<th style="background-color:#F2F2F2;">Website/Studio</th> |
|||
<th style="background-color:#F2F2F2;">Link</th> |
|||
<th style="background-color:#F2F2F2;">Alias</th> |
|||
<th style="background-color:#F2F2F2;">Notes</th> |
|||
</tr> |
|||
⚫ | |||
⚫ | |||
]], table.concat(rows)) |
|||
return tableHtml |
return tableHtml |
||
end |
|||
⚫ | |||
⚫ | |||
return p.getTableHtml(args) |
|||
end |
end |
||
Revision as of 21:04, 12 March 2023
Documentation for this module may be created at Module:CareerList/doc
local p = {} -- Load the configuration file local cfg = mw.loadData('Module:CareerList/config') -- Function to build a row of the table function buildRow(site, id, alias, notes, index) -- Determine the row color local color if site == 'Website' then color = (index % 2 == 1) and cfg.colors.website1 or cfg.colors.website2 else color = (index % 2 == 1) and cfg.colors.studio1 or cfg.colors.studio2 end -- Build the row local row = string.format( cfg.rowTemplate, color, site == 'Website' and cfg.colors.websiteBg or cfg.colors.studioBg, site, cfg.siteUrls[site][1], cfg.siteUrls[site][2]:gsub('$1', id), alias == '' and mw.title.getCurrentTitle().text or alias, notes == '' and 'N/A' or notes ) return row end -- Main function function p.main(frame) -- Get the template parameters local args = frame.args -- Get the list parameter local list = args.list or error('List not specified') -- Split the list into rows local rows = mw.text.split(list, '%s*' .. cfg.rowSeparator .. '%s*') -- Build the table local index = 1 local tableContent = '' for _, row in ipairs(rows) do local params = mw.text.split(row, '%s*|%s*') if #params >= 3 then local site = params[1] local id = params[2] local alias = params[3] local notes = params[4] or '' tableContent = tableContent .. buildRow(site, id, alias, notes, index) index = index + 1 end end -- Build the table local tableHtml = string.format(cfg.tableTemplate, tableContent) return tableHtml end return p