Difference between revisions of "Module: Infobox character/name"

From A Wiki of Ice and Fire
Jump to: navigation, search
Line 25: Line 25:
 
     local args = getArgs(frame)
 
     local args = getArgs(frame)
  
     local fullname = args.fullname
+
     local fullname = args.fullname or ""
     local title = args.title
+
     local title = args.title or ""
     local nickname = args.nickname
+
     local nickname = args.nickname or ""
     local firstname = args.firstname
+
     local firstname = args.firstname or ""
     local particle = args.particle
+
     local particle = args.particle or ""
     local lastname = args.lastname
+
     local lastname = args.lastname or ""
     local epithet = args.epithet
+
     local epithet = args.epithet or ""
  
 
     local above
 
     local above

Revision as of 18:06, 15 September 2022

Documentation for this module may be created at Module:Infobox character/name/doc

local getArgs = require('Module:Arguments').getArgs
local infobox_image = require('Module:InfoboxImage').InfoboxImage
local br = require('Module:Separated entries').br

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

local function generate_arms(arms)
    --This function generates small coat of arms icons
    return infobox_image({
        args = {
            image  = arms,
            size   = '50x70px',
            border = 'no',
            suppressplaceholder = 'no',
        }
    })
end

local p = {}

function p.main(frame)
    local args = getArgs(frame)

    local fullname = args.fullname or ""
    local title = args.title or ""
    local nickname = args.nickname or ""
    local firstname = args.firstname or ""
    local particle = args.particle or ""
    local lastname = args.lastname or ""
    local epithet = args.epithet or ""

    local above
    local core
    local below
    if not is_empty(firstname) then
        above = title
        core = nickname .. ' ' .. firstname .. ' ' .. particle .. ' ' .. lastname
        below = epithet
    elseif not is_empty(lastname) then
        core = nickname .. ' ' .. title .. ' ' .. particle .. ' ' .. lastname
        below = epithet
    elseif not is_empty(nickname) then
        core = nickname
        below = epithet
    elseif not is_empty(title) then
        core = title
        below = epithet
    elseif not is_empty(title) then
        core = epithet
    elseif not is_empty(fullname) then
        core = fullname
    end

    local above_span = ""
    if not is_empty(above) then
        above_span = mw.html.create('span')
        above_span:css('font-size', '80%')
                  :css('font-weight', 'normal')
                  :wikitext(above)
    end

    local below_span = ""
    if not is_empty(below) then
        below_span = mw.html.create('span')
        below_span:css('font-size', '80%')
                  :css('font-weight', 'normal')
                  :wikitext(above)
    end

    local name = br({
        tostring(above_span),
        core,
        tostring(below_span),
    })

    local complete_name
    -- The module looks different based on the number of arms to display
    -- 1) Between 3 and 5 arms
    if (args.arms or args.arms1) and args.arms2 and args.arms3 then
        complete_name = mw.html.create('div')
        complete_name:css('text-align', 'justify')
                     :css('text-justify', 'distribute-all-lines')
        for i=1,5 do
            local arms
            if i ~= 1 then
                arms = args['arms' .. tostring(i)]
            else
                arms = args.arms1 or args.arms
            end
            if arms then
                above:wikitext(generate_arms(arms))
                     :wikitext(" ")
            end
        end
        complete_name:tag('span'):css('display', 'inline-block')
                                 :css('width', '100%')
                                 :css('text-align', 'center')
                                 :wikitext(name)
    -- 2) One or 2 arms
    elseif args.arms or args.arms1 then
        local arms1 = args.arms1 or args.arms
        local arms2 = args.arms2 or arms1
        complete_name = mw.html.create('table')
        complete_name:css('width', '100%')
                     :tag('tr'):tag('td'):css('vertical-align', 'top')
                                         :css('text-align', 'left')
                                         :css('width', '50px')
                                         :wikitext(generate_arms(arms1))
                                         :done()
                               :tag('td'):css('vertical-align', 'middle')
                                         :css('text-align', 'center')
                                         :css('padding-bottom', '5px')
                                         :wikitext(name)
                                         :done()
                               :tag('td'):css('vertical-align', 'top')
                                         :css('text-align', 'right')
                                         :css('width', '50px')
                                         :wikitext(generate_arms(arms2))
    -- 3) No arms
    else
        complete_name = name
    end

    return tostring(complete_name)
end

return p