Difference between revisions of "Module: Infobox character"

From A Wiki of Ice and Fire
Jump to: navigation, search
(Undo revision 280687 by Abjiklam (talk))
(Tag: Undo)
 
(32 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
local infobox_image = require('Module:InfoboxImage').InfoboxImage
 
local infobox_image = require('Module:InfoboxImage').InfoboxImage
 
local br = require('Module:Separated entries').br
 
local br = require('Module:Separated entries').br
local date = require('Module:Date').main
+
local date = require('Module:Date')._format
 +
local dr = require('Module:Date range')._main
 +
local seasons = require('Module:Season')._seasons
  
 
-- Add a book or category in the order it should appear in the infobox.
 
-- Add a book or category in the order it should appear in the infobox.
 
-- The first element of each section is the section name.
 
-- The first element of each section is the section name.
local books = {
+
local books = require('Module:Book/data')
    asoiaf = {
 
        "A Song of Ice and Fire",
 
        agot = "A Game of Thrones",
 
        acok = "A Clash of Kings",
 
        asos = "A Storm of Swords",
 
        affc = "A Feast for Crows",
 
        adwd = "A Dance with Dragon",
 
        twow = "The Winds of Winter",
 
        ados = "A Dream of Spring",
 
    },
 
    todae = {
 
        "Tales of Dunk and Egg",
 
        thk = "The Hedge Knight",
 
        tss = "The Sworn Sword",
 
        tmk = "The Mystery Knight",
 
    },
 
    other = {
 
        "Other Books",
 
        twoiaf = "The World of Ice and Fire",
 
        fab    = "Fire and Blood",
 
        tpatq  = "The Princess and the Queen",
 
        trp    = "The Rogue Prince",
 
        tsotd  = "The Sons of the Dragon",
 
    },
 
}
 
  
 
local function generate_arms(arms)
 
local function generate_arms(arms)
Line 183: Line 160:
 
         suppressplaceholder = 'yes',
 
         suppressplaceholder = 'yes',
 
         caption    = br{args.caption, copyright},
 
         caption    = br{args.caption, copyright},
 +
    }
 +
   
 +
    ib.add_repeated_section{
 +
        header_prefix = 'office',
 +
        rows = {
 +
            {
 +
                rowtype  = 'rows',
 +
                argprefix = 'suffix',
 +
                rowstyle  = "background-color:#f8e9d0;font-weight:bold;",
 +
            },
 +
            {
 +
                rowtype  = 'row',
 +
                argprefix = 'status',
 +
                rowstyle  = "font-weight:bold;",
 +
            },
 +
            {
 +
                rowtype  = 'row',
 +
                argprefix = 'term',
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'alias',
 +
                singular  = "Alias",
 +
                plural    = "Aliases",
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'consort',
 +
                singular  = "Consort",
 +
                plural    = "Consorts",
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'king',
 +
                singular  = "King",
 +
                plural    = "Kings",
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'queen',
 +
                singular  = "Queen",
 +
                plural    = "Queens",
 +
            },
 +
            {
 +
                rowtype  = 'rows',
 +
                argprefix = 'superior',
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'predecessor',
 +
                singular  = "Predecessor",
 +
                plural    = "Predecessors",
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                argprefix = 'successor',
 +
                singular  = "Successor",
 +
                plural    = "Successors",
 +
            },
 +
        },
 
     }
 
     }
 
      
 
      
 
     -- important events
 
     -- important events
    local birth_date = args.birth_date
 
    birth_date = tonumber(birth_date) and date({birth_date}) or birth_date
 
 
     local born = br({
 
     local born = br({
         args.birth_name,
+
         date(args.birth_date),
 
         birth_date,
 
         birth_date,
 
         not is_empty(args.birth_place) and ("in " .. args.birth_place),
 
         not is_empty(args.birth_place) and ("in " .. args.birth_place),
 
     })
 
     })
    local knighted_date = args.knighted_date
 
    knighted_date = tonumber(knighted_date) and date({knighted_date}) or knighted_date
 
 
     local knighted = br({
 
     local knighted = br({
         knighted_date,
+
         date(args.knighted_date),
 
         not is_empty(args.knighted_by) and ("by " .. args.knighted_by),
 
         not is_empty(args.knighted_by) and ("by " .. args.knighted_by),
 
     })
 
     })
    local legitimized_date = args.legitimized_date
 
    legitimized_date = tonumber(legitimized_date) and date({legitimized_date}) or legitimized_date
 
 
     local legitimized = br({
 
     local legitimized = br({
         legitimized_date,
+
         date(args.legitimized_date),
 
         not is_empty(args.legitimized_by) and ("by " .. args.legitimized_by),
 
         not is_empty(args.legitimized_by) and ("by " .. args.legitimized_by),
 
     })
 
     })
    local death_date = args.death_date
 
    death_date = tonumber(death_date) and date({death_date}) or death_date
 
 
     local died = br({
 
     local died = br({
         death_date,
+
         date(args.death_date),
         args.death_place,
+
         not is_empty(args.death_place) and ("in " .. args.death_place),
 
     })
 
     })
 
      
 
      
     ib.add_section{ header = "Biographical information",
+
     ib.add_section{
                    rows = {
+
        header = "Biographical information",
                                {
+
        rows   = {
                                    label = "Born",
+
            {
                                    data  = born,
+
                label = "Born",
                                },
+
                data  = born,
                                {
+
            },
                                    label = "Knighted",
+
            {
                                    data  = knighted,
+
                label = "Knighted",
                                },
+
                data  = knighted,
                                {
+
            },
                                    label = "Legitimized",
+
            {
                                    data  = legitimized,
+
                label = "Legitimized",
                                },
+
                data  = legitimized,
                                {
+
            },
                                    label = "Died",
+
            {
                                    data  = died,
+
                label = "Died",
                                },
+
                data  = died,
                                {
+
            },
                                    rowtype  = 'list',
+
            {
                                    singular = "Occupation",
+
                rowtype  = 'list',
                                    plural  = "Occupations",
+
                singular = "Occupation",
                                    prefix  = 'occupation',
+
                plural  = "Occupations",
                                },
+
                prefix  = 'occupation',
                                {
+
            },
                                    rowtype  = 'list',
+
            {
                                    singular = "Title",
+
                rowtype  = 'list',
                                    plural  = "Titles",
+
                singular = (args.office or args.office1) and "Other title" or "Title",
                                    prefix  = 'title',
+
                plural  = (args.office or args.office1) and "Other titles" or "Titles",
                                },
+
                prefix  = 'title',
                                {
+
            },
                                    rowtype  = 'list',
+
            {
                                    singular = "Culture",
+
                rowtype  = 'list',
                                    plural  = "Cultures",
+
                singular = "Culture",
                                    prefix  = 'culture',
+
                plural  = "Cultures",
                                },
+
                prefix  = 'culture',
                                {
+
            },
                                    rowtype  = 'list',
+
            {
                                    singular = "Allegiance",
+
                rowtype  = 'list',
                                    plural  = "Allegiances",
+
                singular = "Allegiance",
                                    prefix  = 'allegiance',
+
                plural  = "Allegiances",
                                },
+
                prefix  = 'allegiance',
                          },
+
            },
                  }
+
        },
 +
    }
 +
    ib.add_section{
 +
        header = "Family",
 +
        rows  = {
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Spouse",
 +
                plural  = "Spouses",
 +
                prefix  = 'spouse',
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Paramour",
 +
                plural  = "Paramours",
 +
                prefix  = 'paramour',
 +
            },
 +
            {
 +
                rowtype  = 'row',
 +
                label = "Heir",
 +
                data  = args.heir,
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Child",
 +
                plural  = "Children",
 +
                prefix  = 'child',
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Bastard child",
 +
                plural  = "Bastards",
 +
                prefix  = 'bastard',
 +
            },
 +
            {
 +
                rowtype = 'rows',
 +
                prefix  = 'father',
 +
                default = "Father",
 +
            },
 +
            {
 +
                rowtype = 'rows',
 +
                prefix  = 'mother',
 +
                default = "Mother",
 +
            },
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Lover",
 +
                plural  = "Lovers",
 +
                prefix  = 'lover',
 +
            },
 +
        },
 +
    }
 +
   
 +
    local book_section = {
 +
        header = "In the books",
 +
        rows = {},
 +
    }
 +
    for _, collection in ipairs(books) do
 +
        local collection_row = {
 +
            rowtype = 'section',
 +
            header  = collection[1],
 +
            rows    = {},
 +
        }
 +
        for key, book in ipairs(collection) do
 +
            if key > 1 then
 +
                local book_row = {
 +
                    rowtype = 'row',
 +
                    label  = book.title,
 +
                    data    = args[book.data],
 +
                }
 +
                table.insert(collection_row.rows, book_row)
 +
            end
 +
        end
 +
        table.insert(book_section.rows, collection_row)
 +
    end
 +
    ib.add_section(book_section)
 +
   
 +
    ib.add_section{
 +
        header = "On HBO’s ''Game of Thrones''",
 +
        rows  = {
 +
            {
 +
                rowtype  = 'list',
 +
                singular = "Played by",
 +
                plural  = "Played by",
 +
                prefix  = 'played_by',
 +
            },
 +
            {
 +
                rowtype = 'row',
 +
                label  = "Appears on",
 +
                data    = args.seasons and seasons(args.seasons),
 +
            },
 +
        },
 +
    }
 
      
 
      
 
     return ib.make_infobox()
 
     return ib.make_infobox()

Latest revision as of 00:13, 24 August 2022

This module is a work in progress for a customized Infobox for characters.


local getArgs = require('Module:Arguments').getArgs
local ib = require('Module:Infobox maker')
local infobox_image = require('Module:InfoboxImage').InfoboxImage
local br = require('Module:Separated entries').br
local date = require('Module:Date')._format
local dr = require('Module:Date range')._main
local seasons = require('Module:Season')._seasons

-- Add a book or category in the order it should appear in the infobox.
-- The first element of each section is the section name.
local books = require('Module:Book/data')

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

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
    
    ib.initialize(args)
    
    --- General styling
    ib.add_classes{
        bodyclass  = "plainlist",
        titleclass = "",
        aboveclass = "",
        imageclass = "",
        belowclass = "",
    }
    
    ib.add_styles{
        bodystyle    = "",
        titlestyle   = "",
        abovestyle   = "",
        imagestyle   = "",
        captionstyle = "",
        headerstyle  = "background-color:#f8e9d0;border:none;",
        labelstyle   = "",
        datastyle    = "",
        belowstyle   = "",
    }
    
    ------
    -- "above" contains everything appearing above the picture
    -- that is, the name, titles and coats of arms
    ------
    local above
    
    -- Name with optional honorific prefix and suffix
    local honorific_prefix = ""
    local honorific_suffix = ""
    if not is_empty(args.honorific_prefix) then
        honorific_prefix = mw.html.create('span')
        honorific_prefix:css('font-size', '80%')
                        :css('font-weight', 'normal')
                        :wikitext(args.honorific_prefix)
    end
    if not is_empty(args.honorific_suffix) then
        honorific_suffix = mw.html.create('span')
        honorific_suffix:css('font-size', '80%')
                        :css('font-weight', 'normal')
                        :wikitext(args.honorific_suffix)
    end
    local name = br({
        tostring(honorific_prefix),
        args.name,
        tostring(honorific_suffix),
    })

    -- 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
        above = mw.html.create('div')
        above
            :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
        above
            :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
        above = mw.html.create('table')
        above
            :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
        above = name
    end
    ib.add_above{above=tostring(above)}
    
    ------
    -- image, caption and copyright
    ------
    -- copyright
    if args.copyright then
        local copyright = mw.html.create('span')
        copyright:css('font-size', '80%')
    end
    -- image and caption
    ib.add_image{
        image       = args.image,
        size        = args.image_size,
        maxsize     = '300px',
        sizedefault = 'frameless',
        alt         = args.alt,
        border      = 'yes',
        suppressplaceholder = 'yes',
        caption     = br{args.caption, copyright},
    }
    
    ib.add_repeated_section{
        header_prefix = 'office',
        rows = {
            {
                rowtype   = 'rows',
                argprefix = 'suffix',
                rowstyle  = "background-color:#f8e9d0;font-weight:bold;",
            },
            {
                rowtype   = 'row',
                argprefix = 'status',
                rowstyle  = "font-weight:bold;",
            },
            {
                rowtype   = 'row',
                argprefix = 'term',
            },
            {
                rowtype   = 'list',
                argprefix = 'alias',
                singular  = "Alias",
                plural    = "Aliases",
            },
            {
                rowtype   = 'list',
                argprefix = 'consort',
                singular  = "Consort",
                plural    = "Consorts",
            },
            {
                rowtype   = 'list',
                argprefix = 'king',
                singular  = "King",
                plural    = "Kings",
            },
            {
                rowtype   = 'list',
                argprefix = 'queen',
                singular  = "Queen",
                plural    = "Queens",
            },
            {
                rowtype   = 'rows',
                argprefix = 'superior',
            },
            {
                rowtype   = 'list',
                argprefix = 'predecessor',
                singular  = "Predecessor",
                plural    = "Predecessors",
            },
            {
                rowtype   = 'list',
                argprefix = 'successor',
                singular  = "Successor",
                plural    = "Successors",
            },
        },
    }
    
    -- important events
    local born = br({
        date(args.birth_date),
        birth_date,
        not is_empty(args.birth_place) and ("in " .. args.birth_place),
    })
    local knighted = br({
        date(args.knighted_date),
        not is_empty(args.knighted_by) and ("by " .. args.knighted_by),
    })
    local legitimized = br({
        date(args.legitimized_date),
        not is_empty(args.legitimized_by) and ("by " .. args.legitimized_by),
    })
    local died = br({
        date(args.death_date),
        not is_empty(args.death_place) and ("in " .. args.death_place),
    })
    
    ib.add_section{
        header = "Biographical information",
        rows   = {
            {
                label = "Born",
                data  = born,
            },
            {
                label = "Knighted",
                data  = knighted,
            },
            {
                label = "Legitimized",
                data  = legitimized,
            },
            {
                label = "Died",
                data  = died,
            },
            {
                rowtype  = 'list',
                singular = "Occupation",
                plural   = "Occupations",
                prefix   = 'occupation',
            },
            {
                rowtype  = 'list',
                singular = (args.office or args.office1) and "Other title" or "Title",
                plural   = (args.office or args.office1) and "Other titles" or "Titles",
                prefix   = 'title',
            },
            {
                rowtype  = 'list',
                singular = "Culture",
                plural   = "Cultures",
                prefix   = 'culture',
            },
            {
                rowtype  = 'list',
                singular = "Allegiance",
                plural   = "Allegiances",
                prefix   = 'allegiance',
            },
        },
    }
    ib.add_section{
        header = "Family",
        rows   = {
            {
                rowtype  = 'list',
                singular = "Spouse",
                plural   = "Spouses",
                prefix   = 'spouse',
            },
            {
                rowtype  = 'list',
                singular = "Paramour",
                plural   = "Paramours",
                prefix   = 'paramour',
            },
            {
                rowtype  = 'row',
                label = "Heir",
                data  = args.heir,
            },
            {
                rowtype  = 'list',
                singular = "Child",
                plural   = "Children",
                prefix   = 'child',
            },
            {
                rowtype  = 'list',
                singular = "Bastard child",
                plural   = "Bastards",
                prefix   = 'bastard',
            },
            {
                rowtype = 'rows',
                prefix  = 'father',
                default = "Father",
            },
            {
                rowtype = 'rows',
                prefix  = 'mother',
                default = "Mother",
            },
            {
                rowtype  = 'list',
                singular = "Lover",
                plural   = "Lovers",
                prefix   = 'lover',
            },
        },
    }
    
    local book_section = {
        header = "In the books",
        rows = {},
    }
    for _, collection in ipairs(books) do
        local collection_row = {
            rowtype = 'section',
            header  = collection[1],
            rows    = {},
        }
        for key, book in ipairs(collection) do
            if key > 1 then
                local book_row = {
                    rowtype = 'row',
                    label   = book.title,
                    data    = args[book.data],
                }
                table.insert(collection_row.rows, book_row)
            end
        end
        table.insert(book_section.rows, collection_row)
    end
    ib.add_section(book_section)
    
    ib.add_section{
        header = "On HBO’s ''Game of Thrones''",
        rows   = {
            {
                rowtype  = 'list',
                singular = "Played by",
                plural   = "Played by",
                prefix   = 'played_by',
            },
            {
                rowtype = 'row',
                label   = "Appears on",
                data    = args.seasons and seasons(args.seasons),
            },
        },
    }
    
    return ib.make_infobox()
end

return p