Module:Infobox: Difference between revisions

No edit summary
No edit summary
 
Line 3: Line 3:
local origArgs = {}
local origArgs = {}
local root
local root
local empty_row_categories = {}
local category_in_empty_row_pattern = '%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*]]'
local has_rows = false
local has_rows = false
local lists = {
    plainlist_t = {
        patterns = {
            '^plainlist$',
            '%splainlist$',
            '^plainlist%s',
            '%splainlist%s'
        },
        found = false,
        styles = 'Plainlist/styles.css'
    },
    hlist_t = {
        patterns = {
            '^hlist$',
            '%shlist$',
            '^hlist%s',
            '%shlist%s'
        },
        found = false,
        styles = 'Hlist/styles.css'
    }
}
local function has_list_class(args_to_check)
    for _, list in pairs(lists) do
        if not list.found then
            for _, arg in pairs(args_to_check) do
                for _, pattern in ipairs(list.patterns) do
                    if mw.ustring.find(arg or '', pattern) then
                        list.found = true
                        break
                    end
                end
                if list.found then break end
            end
        end
    end
end
local function fixChildBoxes(sval, tt)
    if sval and sval:match('%S') then
        local s = mw.text.trim(sval)
        s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', '<!--FIXED-->\n%1')
        return s
    end
    return sval
end


-- Helper function: clean up table rows
local function cleanInfobox()
local function cleanInfobox()
     root = tostring(root)
     root = tostring(root)
Line 61: Line 13:
end
end


local function addRow(rowArgs)
-- Helper function: add a row to the infobox
     if rowArgs.data and rowArgs.data:match('%S') then
local function addRow(label, data)
     if data and data:match('%S') then
         has_rows = true
         has_rows = true
         local row = root:tag('tr')
         local row = root:tag('tr')
         if rowArgs.label then
         if label then
             row:tag('th')
             row:tag('th')
                 :attr('scope', 'row')
                 :attr('scope', 'row')
                 :addClass('infobox-label')
                 :addClass('infobox-label')
                 :wikitext(rowArgs.label)
                 :wikitext(label)
                 :done()
                 :done()
         end
         end
         row:tag('td')
         row:tag('td')
             :addClass('infobox-data')
             :addClass('infobox-data')
             :wikitext(fixChildBoxes(rowArgs.data, 'td'))
             :wikitext(data)
     end
     end
end
end


-- Helper function: render the title
local function renderTitle()
local function renderTitle()
     if args.title then
     if args.title then
Line 87: Line 41:
end
end


local function renderRows()
-- Helper function: render the image
    local rownums = {}
local function renderImage()
    for k, v in pairs(args) do
        if k:match('^data%d+$') then
            table.insert(rownums, tonumber(k:match('%d+')))
        end
    end
    table.sort(rownums)
    for _, num in ipairs(rownums) do
        addRow({
            label = args['label' .. num],
            data = args['data' .. num]
        })
    end
end
 
local function renderImages()
     if args.image then
     if args.image then
         local imageRow = root:tag('tr')
         local imageRow = root:tag('tr')
Line 113: Line 52:
end
end


local function preprocessArgs(frameArgs)
-- Main rendering logic
    for k, v in pairs(frameArgs) do
local function renderInfobox()
        args[k] = mw.text.trim(v)
    end
end
 
function p.infobox(frame)
    preprocessArgs(frame:getParent().args)
     root = mw.html.create('table')
     root = mw.html.create('table')
         :addClass('infobox')
         :addClass('infobox')
         :css('width', '350px')
         :css('width', '350px')
     renderTitle()
     renderTitle()
     renderImages()
     renderImage()
     renderRows()
 
     for i = 1, 50 do
        local label = args['label' .. i]
        local data = args['data' .. i]
        if not label and not data then break end
        addRow(label, data)
    end
 
     cleanInfobox()
     cleanInfobox()
end
-- Entry point for the module
function p.infobox(frame)
    origArgs = frame:getParent().args
    args = {}
    -- Preprocess arguments
    for k, v in pairs(origArgs) do
        args[k] = mw.text.trim(v)
    end
    renderInfobox()
     return tostring(root)
     return tostring(root)
end
end


return p
return p