Difference between revisions of "Module: Infobox character/name"
From A Wiki of Ice and Fire
(Track "none") |
|||
(14 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
local br = require('Module:Separated entries').br | local br = require('Module:Separated entries').br | ||
local roman = require('Module:Roman').main | local roman = require('Module:Roman').main | ||
+ | local coa = require('Module:Coa/core').main | ||
local function split(inputstr, sep) | local function split(inputstr, sep) | ||
+ | -- This function splits a string by the given separating character | ||
sep = sep or '%s' | sep = sep or '%s' | ||
local t = {} | local t = {} | ||
Line 21: | Line 23: | ||
end | end | ||
− | local function generate_arms(arms) | + | local function overspecified_arms(arms) |
+ | arms = mw.ustring.lower(arms) | ||
+ | local patterns = { | ||
+ | "^%[%[file:", | ||
+ | "%|.*%]%]$", | ||
+ | "^house ", | ||
+ | "^coin of", | ||
+ | "%.png$", | ||
+ | "%.svg$", | ||
+ | "%.jpe?g$", | ||
+ | "^none", | ||
+ | } | ||
+ | for _, pattern in pairs(patterns) do | ||
+ | if mw.ustring.find(arms, pattern) then | ||
+ | return "[[Category:Character pages with overspecified arms]]" | ||
+ | end | ||
+ | end | ||
+ | return "" | ||
+ | end | ||
+ | |||
+ | local function generate_arms(arms, size) | ||
--This function generates small coat of arms icons | --This function generates small coat of arms icons | ||
− | return | + | return coa{ |
− | + | arms, | |
− | + | format = 'image', | |
− | + | size = size or 'infobox-l', | |
− | + | } .. overspecified_arms(arms) | |
− | |||
− | |||
− | }) | ||
end | end | ||
Line 36: | Line 55: | ||
function p.main(frame) | function p.main(frame) | ||
+ | local MAX_ARMS = 5 | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
+ | args.arms1 = args.arms1 or args.arms | ||
local template = args.nametemplate or nil | local template = args.nametemplate or nil | ||
Line 52: | Line 73: | ||
name_parts.romanregnalnumber = roman({ regnalnumber }) or "" | name_parts.romanregnalnumber = roman({ regnalnumber }) or "" | ||
− | if template | + | local categories = {"Character pages with new syntax name"} |
+ | |||
+ | if is_empty(template) then | ||
if not is_empty(name_parts.firstname) then | if not is_empty(name_parts.firstname) then | ||
if not is_empty(name_parts.lastname) then | if not is_empty(name_parts.lastname) then | ||
Line 63: | Line 86: | ||
template = 'title/nickname-firstname-epithet/' | template = 'title/nickname-firstname-epithet/' | ||
end | end | ||
− | elseif is_empty(name_parts.lastname) then | + | elseif not is_empty(name_parts.lastname) then |
template = '/nickname-title-particle-lastname/epithet' | template = '/nickname-title-particle-lastname/epithet' | ||
− | elseif is_empty(name_parts.nickname) then | + | elseif not is_empty(name_parts.nickname) then |
template = '/title-nickname/epithet' | template = '/title-nickname/epithet' | ||
− | elseif is_empty(name_parts.title) then | + | elseif not is_empty(name_parts.title) then |
template = '/title/epithet' | template = '/title/epithet' | ||
− | elseif is_empty(name_parts.epithet) then | + | elseif not is_empty(name_parts.epithet) then |
template = '/epithet/' | template = '/epithet/' | ||
− | elseif is_empty(name_parts.fullname) then | + | elseif not is_empty(name_parts.fullname) then |
template = '/fullname/' | template = '/fullname/' | ||
+ | categories = {"Character pages with old syntax name"} | ||
end | end | ||
+ | else | ||
+ | table.insert(categories, "Character pages with custom nametemplate") | ||
end | end | ||
− | local above_template, core_template, below_template = | + | -- Parse name template to get the above, core, and below texts |
− | + | local above_template, core_template, below_template = unpack(split(template, '/')) | |
local above = '' | local above = '' | ||
for _, part in pairs(split(above_template, '-')) do | for _, part in pairs(split(above_template, '-')) do | ||
− | if name_parts[part] then | + | if not is_empty(name_parts[part]) then |
above = above .. ' ' .. name_parts[part] | above = above .. ' ' .. name_parts[part] | ||
end | end | ||
end | end | ||
− | |||
local core = '' | local core = '' | ||
for _, part in pairs(split(core_template, '-')) do | for _, part in pairs(split(core_template, '-')) do | ||
− | if name_parts[part] then | + | if not is_empty(name_parts[part]) then |
core = core .. ' ' .. name_parts[part] | core = core .. ' ' .. name_parts[part] | ||
end | end | ||
end | end | ||
− | |||
local below = '' | local below = '' | ||
for _, part in pairs(split(below_template, '-')) do | for _, part in pairs(split(below_template, '-')) do | ||
− | if name_parts[part] then | + | if not is_empty(name_parts[part]) then |
below = below .. ' ' .. name_parts[part] | below = below .. ' ' .. name_parts[part] | ||
end | end | ||
end | end | ||
− | + | -- Build above and below texts and full name | |
local above_span = "" | local above_span = "" | ||
if not is_empty(above) then | if not is_empty(above) then | ||
Line 106: | Line 130: | ||
:wikitext(above) | :wikitext(above) | ||
end | end | ||
− | |||
local below_span = "" | local below_span = "" | ||
if not is_empty(below) then | if not is_empty(below) then | ||
Line 114: | Line 137: | ||
:wikitext(below) | :wikitext(below) | ||
end | end | ||
− | |||
local name = br({ | local name = br({ | ||
tostring(above_span), | tostring(above_span), | ||
Line 123: | Line 145: | ||
local complete_name | local complete_name | ||
-- The module looks different based on the number of arms to display | -- The module looks different based on the number of arms to display | ||
− | -- | + | if args.arms1 and args.arms2 and args.arms3 then |
− | + | -- Three or more arms | |
− | local arms1 = args.arms1 | + | complete_name = mw.html.create('div') |
− | + | complete_name:css('text-align', 'justify') | |
+ | :css('text-justify', 'distribute-all-lines') | ||
+ | local coasize | ||
+ | if is_empty(args.arms4) then | ||
+ | coasize = 'infobox-m' | ||
+ | table.insert(categories, "Character pages with 3 coats of arms") | ||
+ | else | ||
+ | coasize = 'infobox-s' | ||
+ | table.insert(categories, "Character pages with 4+ coats of arms") | ||
+ | end | ||
+ | for i=1,MAX_ARMS do | ||
+ | local arms = args['arms' .. tostring(i)] | ||
+ | if arms then | ||
+ | complete_name:wikitext(generate_arms(arms, coasize)) | ||
+ | :wikitext(" ") | ||
+ | end | ||
+ | end | ||
+ | complete_name:tag('span') | ||
+ | :css('display', 'inline-block') | ||
+ | :css('width', '100%') | ||
+ | :css('text-align', 'center') | ||
+ | :wikitext(name) | ||
+ | elseif args.arms1 then | ||
+ | -- One or two arms | ||
+ | local arms1 = args.arms1 | ||
+ | local arms2 | ||
+ | if is_empty(args.arms2) then | ||
+ | arms2 = args.arms1 | ||
+ | table.insert(categories, "Character pages with 1 coat of arms") | ||
+ | else | ||
+ | arms2 = args.arms2 | ||
+ | table.insert(categories, "Character pages with 2 coats of arms") | ||
+ | end | ||
complete_name = mw.html.create('table') | complete_name = mw.html.create('table') | ||
complete_name:css('width', '100%') | complete_name:css('width', '100%') | ||
− | :tag('tr'):tag('td'):css('vertical-align', 'top') | + | :tag('tr') |
+ | :tag('td'):css('vertical-align', 'top') | ||
:css('text-align', 'left') | :css('text-align', 'left') | ||
:css('width', '50px') | :css('width', '50px') | ||
Line 143: | Line 198: | ||
:css('width', '50px') | :css('width', '50px') | ||
:wikitext(generate_arms(arms2)) | :wikitext(generate_arms(arms2)) | ||
− | |||
else | else | ||
+ | -- No arms | ||
complete_name = name | complete_name = name | ||
+ | if categories[1] == "Character pages with new syntax name" then | ||
+ | table.insert(categories, "Character pages with 0 coats of arms") | ||
+ | end | ||
end | end | ||
− | + | -- Add all categories | |
+ | complete_name = tostring(complete_name) | ||
+ | for _, category in pairs(categories) do | ||
+ | complete_name = complete_name .. "[[Category:" .. category .. "]]" | ||
+ | end | ||
+ | return complete_name | ||
end | end | ||
return p | return p |
Revision as of 23:37, 21 January 2023
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)
-- This function splits a string by the given separating character
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 overspecified_arms(arms)
arms = mw.ustring.lower(arms)
local patterns = {
"^%[%[file:",
"%|.*%]%]$",
"^house ",
"^coin of",
"%.png$",
"%.svg$",
"%.jpe?g$",
"^none",
}
for _, pattern in pairs(patterns) do
if mw.ustring.find(arms, pattern) then
return "[[Category:Character pages with overspecified arms]]"
end
end
return ""
end
local function generate_arms(arms, size)
--This function generates small coat of arms icons
return coa{
arms,
format = 'image',
size = size or 'infobox-l',
} .. overspecified_arms(arms)
end
local p = {}
function p.main(frame)
local MAX_ARMS = 5
local args = getArgs(frame)
args.arms1 = args.arms1 or args.arms
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 categories = {"Character pages with new syntax name"}
if is_empty(template) 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/'
categories = {"Character pages with old syntax name"}
end
else
table.insert(categories, "Character pages with custom nametemplate")
end
-- Parse name template to get the above, core, and below texts
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
-- Build above and below texts and full name
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
if args.arms1 and args.arms2 and args.arms3 then
-- Three or more arms
complete_name = mw.html.create('div')
complete_name:css('text-align', 'justify')
:css('text-justify', 'distribute-all-lines')
local coasize
if is_empty(args.arms4) then
coasize = 'infobox-m'
table.insert(categories, "Character pages with 3 coats of arms")
else
coasize = 'infobox-s'
table.insert(categories, "Character pages with 4+ coats of arms")
end
for i=1,MAX_ARMS do
local arms = args['arms' .. tostring(i)]
if arms then
complete_name:wikitext(generate_arms(arms, coasize))
:wikitext(" ")
end
end
complete_name:tag('span')
:css('display', 'inline-block')
:css('width', '100%')
:css('text-align', 'center')
:wikitext(name)
elseif args.arms1 then
-- One or two arms
local arms1 = args.arms1
local arms2
if is_empty(args.arms2) then
arms2 = args.arms1
table.insert(categories, "Character pages with 1 coat of arms")
else
arms2 = args.arms2
table.insert(categories, "Character pages with 2 coats of arms")
end
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))
else
-- No arms
complete_name = name
if categories[1] == "Character pages with new syntax name" then
table.insert(categories, "Character pages with 0 coats of arms")
end
end
-- Add all categories
complete_name = tostring(complete_name)
for _, category in pairs(categories) do
complete_name = complete_name .. "[[Category:" .. category .. "]]"
end
return complete_name
end
return p