Module: Infobox maker

From A Wiki of Ice and Fire
Jump to: navigation, search
Module documentation

This module is there to help create Infoboxes using Lua syntax instead of Wiki markup.

Copy this to start a new infobox
local getArgs = require('Module:Arguments').getArgs
local ib = require('Module:Infobox maker')
-- Potentially useful:
-- local infobox_image = require('Module:InfoboxImage').InfoboxImage
-- local br = require('Module:Separated entries').br
-- local date = require('Module:Date').main

local function is_empty(s)
    -- This function checks whether a string is empty
    return s == nil or s == ''
end

local p = {}

function p.main(frame)
    -- The main function cleans up and formats all the arguments before
    -- passing them to the Infobox module.
    
    local args = getArgs(frame) -- arguments passed to this module. this contains all the template {{{parameters}}}
    
    ib.initialize(args)
    
    --- General styling
    ib.add_classes{
        bodyclass  = "",
        titleclass = "",
        aboveclass = "",
        imageclass = "",
        belowclass = "",
    }
    
    ib.add_styles{
        bodystyle    = "",
        titlestyle   = "",
        abovestyle   = "",
        imagestyle   = "",
        captionstyle = "",
        headerstyle  = "",
        labelstyle   = "",
        datastyle    = "",
        belowstyle   = "",
    }
    
    ib.add_title{
        title = "",
    }

    ib.add_above{
        above = "",
    }

    ib.add_subheader{ -- use as often as necessary
        subheader         = "",
        subheaderrowclass = "",
        subheaderclass    = "",
    }

    ib.add_image{ -- use as often as necessary
        image               = "",
        size                = "",
        maxsize             = "",
        sizedefault         = "",
        alt                 = "",
        border              = "", -- yes/no
        suppressplaceholder = "", -- yes/no
        caption             = "",
    }
    
    ib.add_below{
        below = "",
    }

    -----
    -- Main content
    -----
    
    ib.add_header{ -- adds a single header row
        header   = "",
        rowstyle = "",
        rowclass = "",
    }
    
    ib.add_row{ -- adds a single data row with optional label
        label    = "",
        data     = "",
        rowstyle = "",
        rowclass = "",
        class    = "",
    }
    
    ib.add_row_list{ -- adds a single row whose data is a list
        singular = "", -- singular label
        plural   = "", -- plural label
        prefix   = '', -- name of the parameter, without number
        rowstyle = "",
        rowclass = "",
        class    = "",
    }
    
    ib.add_section{  -- adds a section with a header that appears only if a data row is not empty
        header   = "",
        rowstyle = "", -- header's style
        rowclass = "", -- header's class
        rows     = {   -- add rows, list rows and headers in the order in which they should appear
            {
                rowtype  = 'row',
                label    = "",
                data     = "",
                rowstyle = "",
                rowclass = "",
                class    = "",
            },
            {
                rowtype  = 'list',
                singular = "",
                plural   = "",
                prefix   = '',
                rowstyle = "",
                rowclass = "",
                class    = "",
            },
            {
                rowtype  = 'header',
                header   = "",
                rowstyle = "",
                rowclass = "",
            },
        }
    }

    --- Categories
    ib.add_category{ -- use as often as necessary
        name  = "",
        key   = "",
    }
    
    return ib.make_infobox()
end

return p

local getArgs = require('Module:Arguments').getArgs
local tt = require('Module:TableTools')
local ib = require('Module:Infobox/sandbox').infobox
local ii = require('Module:InfoboxImage').InfoboxImage
local ul = require('Module:Ul').main
local listify = require('Module:Listify')

local function is_empty(s)
    -- This function checks whether a string is empty
    return s == nil or s == ''
end

local p = {}

p.init = {}
p.row_counter = 1
p.subheader_counter = 1
p.image_counter = 1
p.args = {}
p.categories = {}

function p.initialize(args)
    p.init = args
end

function p.add_category(args)
    local name = args.name or ""
    local key  = args.key or ""
    if not is_empty(name) then
        if is_empty(key) then
            table.insert(p.categories, "[[Category:" .. name .. "]]")
        else
            table.insert(p.categories, "[[Category:" .. name .. "|" .. key .. "]]")
        end
    end
end

function p.add_classes(args)
    for _, v in ipairs{
                        'bodyclass',
                        'titleclass',
                        'aboveclass',
                        'imageclass',
                        'belowclass',
                      } do
        if is_empty(p.args[v]) then
            p.args[v] = args[v]
        else
            p.args[v] = p.args[v] .. " " .. args[v]
        end
    end
end

function p.add_styles(args)
    for _, v in ipairs{
                        'bodystyle',
                        'titlestyle',
                        'abovestyle',
                        'imagestyle',
                        'captionstyle',
                        'headerstyle',
                        'labelstyle',
                        'datastyle',
                        'belowstyle',
                      } do
        if is_empty(p.args[v]) then
            p.args[v] = args[v]
        else
            p.args[v] = p.args[v] .. args[v]
        end
    end
end

function p.add_title(args)
    p.args.title = args.title
end

function p.add_above(args)
    p.args.above = args.above
end

function p.add_below(args)
    p.args.below = args.below
end

function p.add_subheader(args)
    local subheader = args.subheader or ""
    local n = tostring(p.subheader_counter)
    if not is_empty(subheader) then
        p.args['subheader'         .. n] = subheader
        p.args['subheaderrowclass' .. n] = args.subheaderrowclass
        p.args['subheaderclass'    .. n] = args.subheaderclass
        p.subheader_counter = p.subheader_counter + 1
        return true
    end
    return false
end

function p.add_image(args)
    local image = {
        image       = args.image,
        size        = args.size,
        maxsize     = args.maxsize,
        sizedefault = args.sizedefault,
        alt         = args.alt,
        border      = args.border,
        suppressplaceholder = args.suppressplaceholder,
    }
    local n = tostring(p.image_counter)
    p.args['image'         .. n] = ii{args=image}
    p.args['imagerowclass' .. n] = args.imagerowclass
    p.args['caption'       .. n] = args.caption
    p.image_counter = p.image_counter + 1
end

function p.add_row(args)
    local label    = args.label    or ""
    local data     = args.data     or ""
    local rowstyle = args.rowstyle or ""
    local rowclass = args.rowclass or ""
    local class    = args.class    or ""
    
    local n = tostring(p.row_counter)
    if not is_empty(data) then
        p.args['label'    .. n] = label
        p.args['data'     .. n] = data
        p.args['rowstyle' .. n] = rowstyle
        p.args['rowclass' .. n] = rowclass
        p.args['class'    .. n] = class
        p.row_counter = p.row_counter + 1
        return true
    end
    return false
end

function p.add_rows(args)
    local prefix       = args.prefix       or ""
    local default      = args.default       or ""
    local rowstyle     = args.rowstyle     or ""
    local rowclass     = args.rowclass     or ""
    local class        = args.class        or ""
    
    p.init[prefix .. '1'] = p.init[prefix .. '1'] or p.init[prefix]
    p.init[prefix .. '_label1'] = p.init[prefix .. '_label1'] or p.init[prefix .. '_label']
    local add = false
    local n = tt.affixNums(p.init, prefix)
    for _, i in ipairs(n) do
        add = p.add_row{
            label    = p.init[prefix .. tostring(i) .. '_label'] or default,
            data     = p.init[prefix .. tostring(i)],
            rowstyle = rowstyle,
            rowclass = rowclass,
            class    = class,
        } or add
    end
    return add
end

function p.add_row_list(args)
    local singular  = args.singular or ""
    local plural    = args.plural   or ""
    local prefix    = args.prefix   or ""
    local rowstyle  = args.rowstyle or ""
    local rowclass  = args.rowclass or ""
    local class     = args.class    or ""
    
    p.init[prefix .. '1'] = p.init[prefix .. '1'] or p.init[prefix]
    local n = tt.affixNums(p.init, prefix)
    local m = listify(p.init[prefix .. '1'])
    if #n > 1 then
        local list = {}
        for _, i in ipairs(n) do
            table.insert(list, p.init[prefix .. tostring(i)])
        end
        return p.add_row{
                            label    = plural,
                            data     = ul(list),
                            rowstyle = rowstyle,
                            rowclass = rowclass,
                            class    = class,
                        }
    elseif #m > 1 then 
        return p.add_row{
                            label    = plural,
                            data     = ul(m),
                            rowstyle = rowstyle,
                            rowclass = rowclass,
                            class    = class,
                        }
    else
        return p.add_row{
                            label    = singular,
                            data     = m[1],
                            rowstyle = rowstyle,
                            rowclass = rowclass,
                            class    = class,
                        }
    end
end

function p.add_header(args)
    local header   = args.header   or ""
    local rowstyle = args.rowstyle or ""
    local rowclass = args.rowclass or ""
    local n = tostring(p.row_counter)
    
    if not is_empty(header) then
        p.args['header'   .. n] = header
        p.args['rowstyle' .. n] = rowstyle
        p.args['rowclass' .. n] = rowclass
        p.row_counter = p.row_counter + 1
        return true
    end
    return false
end

function p.add_section(args)
    local header    = args.header    or ""
    local rowstyle  = args.rowstyle  or ""
    local rowclass  = args.rowclass  or ""
    local rows      = args.rows      or {}
    
    local add = false
    local h = tostring(p.row_counter)
    p.row_counter = p.row_counter + 1
    for i, row in ipairs(rows) do
        if row.rowtype == 'header' then
            add = p.add_header(row)   or add
        elseif row.rowtype == 'list' then
            add = p.add_row_list(row) or add
        elseif row.rowtype == 'rows' then
            add = p.add_rows(row)     or add
        elseif row.rowtype == 'section' then
            add = p.add_section(row)  or add
        else
            add = p.add_row(row)      or add
        end
    end
    if add == true then
        p.args['header'   .. h] = header
        p.args['rowstyle' .. h] = rowstyle
        p.args['rowclass' .. h] = rowclass
    end
    return add
end

function p.add_repeated_section(args)
    local header_prefix = args.header_prefix or ""
    local rowstyle  = args.rowstyle  or ""
    local rowclass  = args.rowclass  or ""
    local rows      = args.rows      or {}
    
    local add = true
    local counter = 1
    while add == true do
        local n  = tostring(counter)
        local section = {
            header   = p.init[header_prefix .. n],
            rowstyle = rowstyle,
            rowclass = rowclass,
            rows     = rows,
        }
        for _, row in ipairs(section.rows) do
            if row.rowtype == 'header' then
                row.header = p.init[header_prefix .. n .. row.argprefix]
            elseif row.rowtype == 'list' then
                row.prefix = header_prefix .. n .. row.argprefix
            elseif row.rowtype == 'rows' then
                row.prefix = header_prefix .. n .. row.argprefix
            else
                row.data   = p.init[header_prefix .. n .. row.argprefix]
            end
        end
        add = p.add_section(section)
        counter = counter + 1
    end
end

function p.make_infobox()
    return ib(p.args) .. mw.text.listToText(p.categories, '')
end

return p