Module: Infobox character/name

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

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

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local infobox_image = require('Module:InfoboxImage').InfoboxImage
local br = require('Module:Separated entries').br
local roman = require('Module:Roman').main
local coa = require('Module:Coa/core').main

local function split(inputstr, sep)
  sep = sep or '%s'
  local t = {}
  for field, s in mw.ustring.gmatch(inputstr, "([^" .. sep .. "]*)(" .. sep .. "?)") do
    table.insert(t, field)
    if s == "" then
      return t
    end
  end
end

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 coa{
    arms,
    format = 'image',
    size = 'infobox',
  }
end

local p = {}

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

  local template = args.nametemplate or nil
  local regnalnumber = args.regnalnumber or ""
  local lastfirst = yesno(args.lastfirst) -- whether last name comes before first name

  local name_parts = {}
  name_parts.fullname = args.name or args.fullname or ""
  name_parts.title = args.title or ""
  name_parts.nickname = args.nickname or ""
  name_parts.firstname = args.firstname or ""
  name_parts.particle = args.particle or ""
  name_parts.lastname = args.lastname or ""
  name_parts.epithet = args.epithet or ""
  name_parts.romanregnalnumber = roman({ regnalnumber }) or ""

  local category = "Character pages with new syntax name"

  if template == nil then
    if not is_empty(name_parts.firstname) then
      if not is_empty(name_parts.lastname) then
        if lastfirst then
          template = 'title/nickname-lastname-particle-firstname-romanregnalnumber/epithet'
        else
          template = 'title/nickname-firstname-romanregnalnumber-particle-lastname/epithet'
        end
      else
        template = 'title/nickname-firstname-epithet/'
      end
    elseif not is_empty(name_parts.lastname) then
      template = '/nickname-title-particle-lastname/epithet'
    elseif not is_empty(name_parts.nickname) then
      template = '/title-nickname/epithet'
    elseif not is_empty(name_parts.title) then
      template = '/title/epithet'
    elseif not is_empty(name_parts.epithet) then
      template = '/epithet/'
    elseif not is_empty(name_parts.fullname) then
      template = '/fullname/'
      category = "Character pages with old syntax name"
    end
  end

  local above_template, core_template, below_template = unpack(split(template, '/'))

  local above = ''
  for _, part in pairs(split(above_template, '-')) do
    if not is_empty(name_parts[part]) then
      above = above .. ' ' .. name_parts[part]
    end
  end

  local core = ''
  for _, part in pairs(split(core_template, '-')) do
    if not is_empty(name_parts[part]) then
      core = core .. ' ' .. name_parts[part]
    end
  end

  local below = ''
  for _, part in pairs(split(below_template, '-')) do
    if not is_empty(name_parts[part]) then
      below = below .. ' ' .. name_parts[part]
    end
  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(below)
  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) One or two arms
  if 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))
    -- 2) No arms
  else
    complete_name = name
  end

  return tostring(complete_name) .. "[[Category:" .. category .. "]]"
end

return p