Модуль:Infobox
local p = {}
local function trim(s) if s == nil then return nil end s = tostring(s):match('^%s*(.-)%s*$') if s == then return nil end return s end
local function pick(args, keys) for _, k in ipairs(keys) do local v = trim(args[k]) if v then return v end end return nil end
local function pickNum(args, prefixes, idx) for _, prefix in ipairs(prefixes) do local v = trim(args[prefix .. idx]) if v then return v end end return nil end
local function hasAnyKeyNum(args, prefixes, idx) for _, prefix in ipairs(prefixes) do if args[prefix .. idx] ~= nil then return true end end return false end
-- Позволяет спискам (*, #, ;, :) и таблицам ({|) корректно парситься внутри
local function fixBlockWikitext(val) if not val then return nil end if val:match('^[%*#;:]') or val:match('^{|') then return '\n' .. val end return val end
local function formatImage(img, size, alt) if not img then return nil end if img:match('^%[%[') or img:match('^<') then return img end local name = img :gsub('^[Ff]ile:', ) :gsub('^[Фф]айл:', ) :gsub('^[Ii]mage:', ) local sz = trim(size) or '240px' if sz:match('^%d+$') then sz = sz .. 'px' end local out = 'alt=' .. alt end return out .. '' end
local function isYes(val) if not val then return false end val = val:lower() return val == 'yes' or val == 'y' or val == '1' or val == 'true' or val == 'да' end
function p.main(frame) local args = {} local parent = frame:getParent() if parent then for k, v in pairs(parent.args) do args[k] = v end end for k, v in pairs(frame.args) do args[k] = v end
local styles = frame:extensionTag{ name = 'templatestyles', args = { src = 'Infobox/styles.css' } }
local isChild = isYes(pick(args, { 'child', 'вложенный' })) local tbl = mw.html.create('table')
if isChild then tbl:addClass('infobox-subbox') else tbl:addClass('infobox') end
local theme = pick(args, { 'theme', 'тема' }) if theme and theme ~= 'neutral' and theme ~= 'default' then tbl:addClass('infobox-theme-' .. theme) end
local bodyClass = pick(args, { 'bodyclass', 'класс' }) if bodyClass then tbl:addClass(bodyClass) end
local bodyStyle = pick(args, { 'bodystyle', 'стиль' }) if bodyStyle then tbl:cssText(bodyStyle) end
local titleStyle = pick(args, { 'titlestyle', 'стиль_заголовка' }) local aboveStyle = pick(args, { 'abovestyle', 'стиль_верха' }) local subheaderStyle = pick(args, { 'subheaderstyle', 'стиль_подзаголовка' }) local headerStyle = pick(args, { 'headerstyle', 'стиль_заголовков' }) local labelStyle = pick(args, { 'labelstyle', 'стиль_меток' }) local dataStyle = pick(args, { 'datastyle', 'стиль_данных' }) local belowStyle = pick(args, { 'belowstyle', 'стиль_подвала' })
-- Заголовок (над рамкой) local title = pick(args, { 'title', 'заголовок' }) if title and not isChild then local cap = tbl:tag('caption'):addClass('infobox-title'):wikitext(title) if titleStyle then cap:cssText(titleStyle) end end -- Верхняя плашка внутри карточки (above) local above = pick(args, { 'above', 'верх', 'надзаголовок' }) if above then local th = tbl:tag('tr'):tag('th') :attr('colspan', 2) :addClass('infobox-above') :wikitext(above) if aboveStyle then th:cssText(aboveStyle) end end -- Подзаголовки for _, sh in ipairs({ pick(args, { 'subheader', 'подзаголовок', 'subheader1', 'подзаголовок1' }), pick(args, { 'subheader2', 'подзаголовок2' }) }) do if sh then local td = tbl:tag('tr'):tag('td') :attr('colspan', 2) :addClass('infobox-subheader') :wikitext(sh) if subheaderStyle then td:cssText(subheaderStyle) end end end -- Изображения (до 2 слотов) local imgSpecs = { { img = pick(args, { 'image', 'изображение', 'image1', 'изображение1' }), size = pick(args, { 'imagesize', 'размер_изображения', 'размер' }), alt = pick(args, { 'alt', 'альт' }), caption = pick(args, { 'caption', 'подпись', 'caption1', 'подпись1' }) }, { img = pick(args, { 'image2', 'изображение2' }), size = pick(args, { 'imagesize2', 'размер_изображения2' }), alt = pick(args, { 'alt2', 'альт2' }), caption = pick(args, { 'caption2', 'подпись2' }) } } for _, spec in ipairs(imgSpecs) do local renderedImg = formatImage(spec.img, spec.size, spec.alt) if renderedImg then local td = tbl:tag('tr'):tag('td') :attr('colspan', 2) :addClass('infobox-image') :wikitext(renderedImg) if spec.caption then td:tag('div'):addClass('infobox-caption'):wikitext(spec.caption) end end end -- Сбор всех пронумерованных строк local indicesMap, indices = {}, {} local patterns = { '^header(%d+)$', '^заголовок(%d+)$', '^раздел(%d+)$', '^секция(%d+)$', '^label(%d+)$', '^метка(%d+)$', '^data(%d+)$', '^текст(%d+)$', '^данные(%d+)$', '^rowclass(%d+)$', '^класс_строки(%d+)$' } for k, _ in pairs(args) do if type(k) == 'string' then for _, pat in ipairs(patterns) do local num = k:match(pat) if num then local n = tonumber(num) if n and not indicesMap[n] then indicesMap[n] = true table.insert(indices, n) end break end end end end table.sort(indices) local headerPrefixes = { 'header', 'заголовок', 'раздел', 'секция' } local labelPrefixes = { 'label', 'метка' } local dataPrefixes = { 'data', 'текст', 'данные' } local rowClassPrefixes = { 'rowclass', 'класс_строки' } local rows = {} for _, idx in ipairs(indices) do local h = pickNum(args, headerPrefixes, idx) local l = pickNum(args, labelPrefixes, idx) local d = pickNum(args, dataPrefixes, idx) local rc = pickNum(args, rowClassPrefixes, idx) local hasSlot = (l ~= nil) or hasAnyKeyNum(args, dataPrefixes, idx) table.insert(rows, { header = h, label = l, data = d, rowclass = rc, hasSlot = hasSlot }) end -- Проверка, нужно ли показывать заголовок секции (скрывает пустые секции) local autoHeaders = not isYes(pick(args, { 'showemptyheaders', 'показывать_пустые_разделы' })) local function shouldShowHeader(pos) if not autoHeaders then return true end local sawSlot = false for j = pos, #rows do if j > pos and rows[j].header then break end if rows[j].data then return true end if rows[j].hasSlot then sawSlot = true end end return not sawSlot end -- Вывод строк for i, row in ipairs(rows) do if row.header and shouldShowHeader(i) then local tr = tbl:tag('tr') if row.rowclass then tr:addClass(row.rowclass) end local th = tr:tag('th') :attr('colspan', 2) :addClass('infobox-header') :wikitext(row.header) if headerStyle then th:cssText(headerStyle) end end if row.data then local tr = tbl:tag('tr') if row.rowclass then tr:addClass(row.rowclass) end local text = fixBlockWikitext(row.data) if row.label then local th = tr:tag('th') :attr('scope', 'row') :addClass('infobox-label') :wikitext(row.label) if labelStyle then th:cssText(labelStyle) end local td = tr:tag('td') :addClass('infobox-data') :wikitext(text) if dataStyle then td:cssText(dataStyle) end else local td = tr:tag('td') :attr('colspan', 2) :addClass('infobox-full-data') :wikitext(text) if dataStyle then td:cssText(dataStyle) end end end end -- Подвал (below) local below = pick(args, { 'below', 'подвал', 'внизу' }) if below then local td = tbl:tag('tr'):tag('td') :attr('colspan', 2) :addClass('infobox-below') :wikitext(fixBlockWikitext(below)) if belowStyle then td:cssText(belowStyle) end end return styles .. tostring(tbl) end return p
local p = {}
local function trim(s)
if s == nil then return nil end
s = tostring(s):match('^%s*(.-)%s*$')
if s == '' then return nil end
return s
end
local function pick(args, keys)
for _, k in ipairs(keys) do
local v = trim(args[k])
if v then return v end
end
return nil
end
local function pickNum(args, prefixes, idx)
for _, prefix in ipairs(prefixes) do
local v = trim(args[prefix .. idx])
if v then return v end
end
return nil
end
local function hasAnyKeyNum(args, prefixes, idx)
for _, prefix in ipairs(prefixes) do
if args[prefix .. idx] ~= nil then
return true
end
end
return false
end
-- Позволяет спискам (*, #, ;, :) и таблицам ({|) корректно парситься внутри <td>
local function fixBlockWikitext(val)
if not val then return nil end
if val:match('^[%*#;:]') or val:match('^{|') then
return '\n' .. val
end
return val
end
local function formatImage(img, size, alt)
if not img then return nil end
if img:match('^%[%[') or img:match('^<') then
return img
end
local name = img
:gsub('^[Ff]ile:', '')
:gsub('^[Фф]айл:', '')
:gsub('^[Ii]mage:', '')
local sz = trim(size) or '240px'
if sz:match('^%d+$') then
sz = sz .. 'px'
end
local out = '[[Файл:' .. name .. '|' .. sz
if alt then
out = out .. '|alt=' .. alt
end
return out .. ']]'
end
local function isYes(val)
if not val then return false end
val = val:lower()
return val == 'yes' or val == 'y' or val == '1' or val == 'true' or val == 'да'
end
function p.main(frame)
local args = {}
local parent = frame:getParent()
if parent then
for k, v in pairs(parent.args) do
args[k] = v
end
end
for k, v in pairs(frame.args) do
args[k] = v
end
local styles = frame:extensionTag{
name = 'templatestyles',
args = { src = 'Infobox/styles.css' }
}
local isChild = isYes(pick(args, { 'child', 'вложенный' }))
local tbl = mw.html.create('table')
if isChild then
tbl:addClass('infobox-subbox')
else
tbl:addClass('infobox')
end
local theme = pick(args, { 'theme', 'тема' })
if theme and theme ~= 'neutral' and theme ~= 'default' then
tbl:addClass('infobox-theme-' .. theme)
end
local bodyClass = pick(args, { 'bodyclass', 'класс' })
if bodyClass then
tbl:addClass(bodyClass)
end
local bodyStyle = pick(args, { 'bodystyle', 'стиль' })
if bodyStyle then
tbl:cssText(bodyStyle)
end
local titleStyle = pick(args, { 'titlestyle', 'стиль_заголовка' })
local aboveStyle = pick(args, { 'abovestyle', 'стиль_верха' })
local subheaderStyle = pick(args, { 'subheaderstyle', 'стиль_подзаголовка' })
local headerStyle = pick(args, { 'headerstyle', 'стиль_заголовков' })
local labelStyle = pick(args, { 'labelstyle', 'стиль_меток' })
local dataStyle = pick(args, { 'datastyle', 'стиль_данных' })
local belowStyle = pick(args, { 'belowstyle', 'стиль_подвала' })
-- Заголовок <caption> (над рамкой)
local title = pick(args, { 'title', 'заголовок' })
if title and not isChild then
local cap = tbl:tag('caption'):addClass('infobox-title'):wikitext(title)
if titleStyle then cap:cssText(titleStyle) end
end
-- Верхняя плашка внутри карточки (above)
local above = pick(args, { 'above', 'верх', 'надзаголовок' })
if above then
local th = tbl:tag('tr'):tag('th')
:attr('colspan', 2)
:addClass('infobox-above')
:wikitext(above)
if aboveStyle then th:cssText(aboveStyle) end
end
-- Подзаголовки
for _, sh in ipairs({
pick(args, { 'subheader', 'подзаголовок', 'subheader1', 'подзаголовок1' }),
pick(args, { 'subheader2', 'подзаголовок2' })
}) do
if sh then
local td = tbl:tag('tr'):tag('td')
:attr('colspan', 2)
:addClass('infobox-subheader')
:wikitext(sh)
if subheaderStyle then td:cssText(subheaderStyle) end
end
end
-- Изображения (до 2 слотов)
local imgSpecs = {
{
img = pick(args, { 'image', 'изображение', 'image1', 'изображение1' }),
size = pick(args, { 'imagesize', 'размер_изображения', 'размер' }),
alt = pick(args, { 'alt', 'альт' }),
caption = pick(args, { 'caption', 'подпись', 'caption1', 'подпись1' })
},
{
img = pick(args, { 'image2', 'изображение2' }),
size = pick(args, { 'imagesize2', 'размер_изображения2' }),
alt = pick(args, { 'alt2', 'альт2' }),
caption = pick(args, { 'caption2', 'подпись2' })
}
}
for _, spec in ipairs(imgSpecs) do
local renderedImg = formatImage(spec.img, spec.size, spec.alt)
if renderedImg then
local td = tbl:tag('tr'):tag('td')
:attr('colspan', 2)
:addClass('infobox-image')
:wikitext(renderedImg)
if spec.caption then
td:tag('div'):addClass('infobox-caption'):wikitext(spec.caption)
end
end
end
-- Сбор всех пронумерованных строк
local indicesMap, indices = {}, {}
local patterns = {
'^header(%d+)$', '^заголовок(%d+)$', '^раздел(%d+)$', '^секция(%d+)$',
'^label(%d+)$', '^метка(%d+)$',
'^data(%d+)$', '^текст(%d+)$', '^данные(%d+)$',
'^rowclass(%d+)$', '^класс_строки(%d+)$'
}
for k, _ in pairs(args) do
if type(k) == 'string' then
for _, pat in ipairs(patterns) do
local num = k:match(pat)
if num then
local n = tonumber(num)
if n and not indicesMap[n] then
indicesMap[n] = true
table.insert(indices, n)
end
break
end
end
end
end
table.sort(indices)
local headerPrefixes = { 'header', 'заголовок', 'раздел', 'секция' }
local labelPrefixes = { 'label', 'метка' }
local dataPrefixes = { 'data', 'текст', 'данные' }
local rowClassPrefixes = { 'rowclass', 'класс_строки' }
local rows = {}
for _, idx in ipairs(indices) do
local h = pickNum(args, headerPrefixes, idx)
local l = pickNum(args, labelPrefixes, idx)
local d = pickNum(args, dataPrefixes, idx)
local rc = pickNum(args, rowClassPrefixes, idx)
local hasSlot = (l ~= nil) or hasAnyKeyNum(args, dataPrefixes, idx)
table.insert(rows, {
header = h,
label = l,
data = d,
rowclass = rc,
hasSlot = hasSlot
})
end
-- Проверка, нужно ли показывать заголовок секции (скрывает пустые секции)
local autoHeaders = not isYes(pick(args, { 'showemptyheaders', 'показывать_пустые_разделы' }))
local function shouldShowHeader(pos)
if not autoHeaders then return true end
local sawSlot = false
for j = pos, #rows do
if j > pos and rows[j].header then
break
end
if rows[j].data then
return true
end
if rows[j].hasSlot then
sawSlot = true
end
end
return not sawSlot
end
-- Вывод строк
for i, row in ipairs(rows) do
if row.header and shouldShowHeader(i) then
local tr = tbl:tag('tr')
if row.rowclass then tr:addClass(row.rowclass) end
local th = tr:tag('th')
:attr('colspan', 2)
:addClass('infobox-header')
:wikitext(row.header)
if headerStyle then th:cssText(headerStyle) end
end
if row.data then
local tr = tbl:tag('tr')
if row.rowclass then tr:addClass(row.rowclass) end
local text = fixBlockWikitext(row.data)
if row.label then
local th = tr:tag('th')
:attr('scope', 'row')
:addClass('infobox-label')
:wikitext(row.label)
if labelStyle then th:cssText(labelStyle) end
local td = tr:tag('td')
:addClass('infobox-data')
:wikitext(text)
if dataStyle then td:cssText(dataStyle) end
else
local td = tr:tag('td')
:attr('colspan', 2)
:addClass('infobox-full-data')
:wikitext(text)
if dataStyle then td:cssText(dataStyle) end
end
end
end
-- Подвал (below)
local below = pick(args, { 'below', 'подвал', 'внизу' })
if below then
local td = tbl:tag('tr'):tag('td')
:attr('colspan', 2)
:addClass('infobox-below')
:wikitext(fixBlockWikitext(below))
if belowStyle then td:cssText(belowStyle) end
end
return styles .. tostring(tbl)
end
return p