Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

Модуль:Infobox

Материал из YastreWiki

Для документации этого модуля может быть создана страница Модуль:Infobox/doc

local p = {}

local function clean(val)
    if val == nil then
        return nil
    end

    local trimmed = mw.text.trim(val)
    if trimmed == '' then
        return nil
    end

    trimmed = trimmed:gsub('\r\n', '\n'):gsub('\r', '\n')
    return trimmed:gsub('\n', '<br>')
end

local customFieldPrefixes = { 'поле', 'field', 'custom' }
local customLabelSuffixes = { 'название', 'Название', 'label', 'Label', 'name', 'Name' }
local CUSTOM_FIELD_LIMIT = 20

local function addCustomLabel(key, args)
    for _, suffix in ipairs(customLabelSuffixes) do
        local label = clean(args[key .. suffix])
        if label then
            return label
        end
    end
    return nil
end

function p.render(frame)
    local styles = frame:extensionTag('templatestyles', '', { src = 'Шаблон:Инфобокс/styles.css' })

    local args = frame:getParent().args
    local title = clean(args['название']) or mw.title.getCurrentTitle().text
    local imageParam = clean(args['изображение'])
    local captionParam = clean(args['подпись'])

    local container = mw.html.create('div')
        :addClass('infobox')

    container:tag('div')
        :addClass('infobox-title')
        :wikitext(title)

    local tableRoot = container:tag('table')
        :addClass('infobox-table')

    if imageParam then
        local imageRow = tableRoot:tag('tr'):addClass('infobox-image-row')
        local imageCell = imageRow:tag('td')
            :attr('colspan', '2')
            :addClass('infobox-image')

        local images = mw.text.split(imageParam, '%s*;%s*')
        local captions = {}
        if captionParam then
            captions = mw.text.split(captionParam, '%s*;%s*')
        end

        for i, imgName in ipairs(images) do
            local trimmedImg = mw.text.trim(imgName)
            if trimmedImg ~= '' then
                local wrapper = imageCell:tag('div')
                    :addClass('infobox-image-wrapper')

                wrapper:tag('div')
                    :wikitext(string.format('[[Файл:%s|150px]]', trimmedImg))

                local currentCaption = clean(captions[i])
                if currentCaption then
                    wrapper:tag('div')
                        :addClass('infobox-caption')
                        :wikitext(currentCaption)
                end
            end
        end
    end

    local function addRow(label, data)
        local val = clean(data)
        if val then
            local row = tableRoot:tag('tr'):addClass('infobox-row')
            row:tag('th')
                :addClass('infobox-label')
                :wikitext(label)
            row:tag('td')
                :addClass('infobox-data')
                :wikitext(val)
        end
    end

    addRow('Настоящее имя', args['имя'])
    addRow('Описание', args['описание'])
    addRow('Статус', args['статус'])

    addRow('Дата создания', args['дата создания'])
    addRow('Подписчики', args['подписчики'])
    addRow('Просмотры', args['просмотры'])
    addRow('Ссылки', args['ссылки'])
    addRow('Первая версия', args['первая версия'])
    addRow('Последняя версия', args['последняя версия'])
    addRow('Лицензия', args['лицензия'])
    addRow('Языки программирования', args['языки программирования'])

    local function addCustomFields()
        for _, prefix in ipairs(customFieldPrefixes) do
            for i = 1, CUSTOM_FIELD_LIMIT do
                local key = prefix .. i
                local rawValue = args[key]
                if rawValue then
                    local cleanedValue = clean(rawValue)
                    if cleanedValue then
                        local label = addCustomLabel(key, args)
                        if label then
                            addRow(label, rawValue)
                        end
                    end
                end
            end
        end
    end

    local function addBracketFields()
        for key, rawValue in pairs(args) do
            if type(key) == 'string' then
                local base, label = key:match('^([^%(%)]+)%s*%((.-)%)$')
                if base and rawValue and mw.text.trim(rawValue) ~= '' then
                    local labelText = clean(label)
                    if not labelText then
                        labelText = clean(base)
                    end
                    if labelText then
                        addRow(labelText, rawValue)
                    end
                end
            end
        end
    end

    addCustomFields()
    addBracketFields()

    local footerText = args['подвал']
    if footerText and mw.text.trim(footerText) ~= '' then
        container:tag('div')
            :addClass('infobox-footer-block')
            :wikitext(footerText)
    end

    return styles .. tostring(container)
end

return p