Documentation for this module may be created at Module:CareerList/doc
local p = {}
-- загрузка конфигурационного файла
local cfg = mw.loadData('Module:CareerList/config')
-- функция для определения ссылки на сайт
local function getSiteLink(site, id)
if cfg[site] then
return cfg[site].url:gsub('$1', id)
end
end
-- функция для создания ячейки таблицы
local function makeCell(row, bg)
local cell = {}
local site = row.site:lower()
-- Ячейка 1
cell[1] = mw.html.create('td')
:css('background-color', cfg[site].bg[bg])
:wikitext(cfg[site].text)
-- Ячейка 2
cell[2] = mw.html.create('td')
:wikitext('[[' .. row.site .. '|' .. row.alias .. ']]')
-- Ячейка 3
cell[3] = mw.html.create('td')
:wikitext('[[' .. getSiteLink(site, row.id) .. '|' .. row.id .. ']]')
-- Ячейка 4
cell[4] = mw.html.create('td')
:wikitext(row.alias == '' and mw.title.getCurrentTitle().text or row.alias)
-- Ячейка 5
cell[5] = mw.html.create('td')
:wikitext(row.notes == '' and 'N/A' or row.notes)
return cell
end
function p.table(frame)
local list = frame.args.list
if not list then
return ''
end
-- Разбиение на строки
local rows = mw.text.split(list, '\n')
local cells = {}
-- Создание ячеек
for i, row in ipairs(rows) do
local values = mw.text.split(row, '|')
local bg = i % 2 == 0 and 2 or 1
cells[i] = makeCell({
site = values[1],
id = values[2],
alias = values[3] or '',
notes = values[4] or ''
}, bg)
end
-- Создание таблицы
local html = mw.html.create()
html
:tag('table')
:addClass('wikitable careerlist')
:css('background-color', '#F2F2F2')
:css('width', '100%')
:css('max-width', 'none')
:css('table-layout', 'fixed')
:wikitext('\n|-')
-- Заголовок таблицы
for i, header in ipairs(cfg.header) do
html
:tag('th')
:css('background-color', '#F2F2F2')
:wikitext(header)
end
-- Строки таблицы
for i, row in ipairs(cells) do
html:wikitext('\n|-')
for j, cell in ipairs(row) do
html:addChild(cell)
end
end
return tostring(html)
end
return p