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

Модуль:Infobox: различия между версиями

Материал из YastreWiki
мНет описания правки
Нет описания правки
Строка 2: Строка 2:


local function clean(val)
local function clean(val)
     if val == '' or val == nil then return nil end
     if val == nil then
     local result = mw.text.trim(val)
        return nil
     result = result:gsub('\r\n?', '\n')
    end
     result = result:gsub('\n', '<br>')
 
     return result
     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
end


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


     local args = frame:getParent().args
     local args = frame:getParent().args
Строка 29: Строка 49:
             :attr('colspan', '2')
             :attr('colspan', '2')
             :addClass('infobox-image')
             :addClass('infobox-image')
           
 
         local images = mw.text.split(imageParam, '%s*;%s*')
         local images = mw.text.split(imageParam, '%s*;%s*')
         local captions = {}
         local captions = {}
Строка 35: Строка 55:
             captions = mw.text.split(captionParam, '%s*;%s*')
             captions = mw.text.split(captionParam, '%s*;%s*')
         end
         end
       
 
         for i, imgName in ipairs(images) do
         for i, imgName in ipairs(images) do
             local trimmedImg = mw.text.trim(imgName)
             local trimmedImg = mw.text.trim(imgName)
Строка 41: Строка 61:
                 local wrapper = imageCell:tag('div')
                 local wrapper = imageCell:tag('div')
                     :addClass('infobox-image-wrapper')
                     :addClass('infobox-image-wrapper')
               
 
                 wrapper:tag('div')
                 wrapper:tag('div')
                     :wikitext(string.format('[[Файл:%s|150px]]', trimmedImg))
                     :wikitext(string.format('[[Файл:%s|150px]]', trimmedImg))
               
 
                 local currentCaption = clean(captions[i])
                 local currentCaption = clean(captions[i])
                 if currentCaption then
                 if currentCaption then
Строка 76: Строка 96:
     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
    addCustomFields()


     return styles .. tostring(root)
     return styles .. tostring(root)

Версия от 10:52, 16 апреля 2026

Для документации этого модуля может быть создана страница Модуль: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 root = mw.html.create('table')
    root:addClass('infobox')

    root:tag('caption')
        :addClass('infobox-title')
        :wikitext(title)

    if imageParam then
        local imageRow = root: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 = root: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['ссылки'])

    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

    addCustomFields()

    return styles .. tostring(root)
end

return p