پودمان:InfoboxPerson
ظاهر
توضیحات این پودمان میتواند در پودمان:InfoboxPerson/توضیحات قرار گیرد.
local p = {}
--==================================================
-- Module:InfoboxPerson
-- Core Person Infobox
--==================================================
local html = mw.html
----------------------------------------------------
-- NORMALIZE
----------------------------------------------------
local function normalize(value)
if value == nil then
return nil
end
value = tostring(value)
value = mw.text.trim(value)
if value == "" then
return nil
end
value = mw.ustring.gsub(
value,
"[\u200C\u200D\uFEFF]",
""
)
return value
end
----------------------------------------------------
-- GET ARGUMENTS
----------------------------------------------------
local function getArgs(frame)
local args = {}
local function read(tbl)
if not tbl then
return
end
for k,v in pairs(tbl) do
local key = normalize(k)
local value = normalize(v)
if key and value then
args[key] = value
end
end
end
local parent = frame:getParent()
if parent then
read(parent.args)
end
read(frame.args)
return args
end
----------------------------------------------------
-- ALIASES
----------------------------------------------------
local aliases = {
name = {
"نام",
"name"
},
image = {
"تصویر",
"پرونده",
"image"
},
caption = {
"توضیح تصویر",
"caption"
},
fullname = {
"نام کامل",
"fullname"
},
birth_date = {
"زادروز",
"تاریخ تولد",
"birth_date"
},
birth_place = {
"زادگاه",
"محل تولد",
"birth_place"
},
death_date = {
"درگذشته",
"تاریخ درگذشت",
"death_date"
},
nationality = {
"ملیت",
"nationality"
},
occupation = {
"پیشه",
"شغل",
"occupation"
},
field = {
"زمینه فعالیت",
"حوزه فعالیت",
"field"
},
education = {
"تحصیلات",
"education"
}
}
--==================================================
-- RESOLVE ALIASES
--==================================================
local function resolveArgs(args)
local result = {}
for key, list in pairs(aliases) do
for i = 1, #list do
local value = args[list[i]]
if value then
result[key] = value
break
end
end
end
-- نگهداری پارامترهای تخصصی
-- برای ماژولهای فرزند
for key, value in pairs(args) do
if not result[key] then
result[key] = value
end
end
return result
end
----------------------------------------------------
-- CHECK VALUE
----------------------------------------------------
local function hasValue(value)
return normalize(value) ~= nil
end
----------------------------------------------------
-- CREATE INFOBOX
----------------------------------------------------
local function createBox()
local box = html.create("table")
box
:addClass("infobox")
:addClass("person-infobox")
return box
end
----------------------------------------------------
-- ADD TITLE
----------------------------------------------------
local function addTitle(box,args)
local title = args.name
if not title then
title = mw.title.getCurrentTitle().text
end
box:tag("tr")
:tag("th")
:attr("colspan","2")
:css({
["background-color"]="#2C5F9E",
color="#FFFFFF",
["text-align"]="center",
["font-size"]="120%",
padding="8px"
})
:wikitext(title)
end
----------------------------------------------------
-- ADD IMAGE
----------------------------------------------------
local function addImage(box,args)
if not hasValue(args.image) then
return
end
box:tag("tr")
:tag("td")
:attr("colspan","2")
:css({
["text-align"]="center",
padding="5px"
})
:wikitext(
"[[File:" ..
args.image ..
"|250px]]"
)
if hasValue(args.caption) then
box:tag("tr")
:tag("td")
:attr("colspan","2")
:css({
["text-align"]="center",
["font-size"]="90%"
})
:wikitext(args.caption)
end
end
----------------------------------------------------
-- ADD HEADER
----------------------------------------------------
local function addHeader(box,text)
box:tag("tr")
:tag("th")
:attr("colspan","2")
:css({
["background-color"]="#2C5F9E",
color="#FFFFFF",
["text-align"]="center"
})
:wikitext(text)
end
----------------------------------------------------
-- ADD ROW
----------------------------------------------------
local function addRow(box,label,value)
if not hasValue(value) then
return false
end
box:tag("tr")
:tag("th")
:wikitext(label)
box:tag("tr")
:tag("td")
:wikitext(value)
return true
end
--==================================================
-- PERSONAL SECTION
--==================================================
local function addPersonal(box,args)
local hasData =
hasValue(args.fullname)
or hasValue(args.birth_date)
or hasValue(args.birth_place)
or hasValue(args.death_date)
or hasValue(args.nationality)
if not hasData then
return
end
addHeader(
box,
"اطلاعات شخصی"
)
addRow(
box,
"نام کامل",
args.fullname
)
addRow(
box,
"زاده",
args.birth_date
)
addRow(
box,
"زادگاه",
args.birth_place
)
addRow(
box,
"درگذشته",
args.death_date
)
addRow(
box,
"ملیت",
args.nationality
)
end
----------------------------------------------------
-- CAREER SECTION
----------------------------------------------------
local function addCareer(box,args)
local hasData =
hasValue(args.occupation)
or hasValue(args.field)
or hasValue(args.education)
if not hasData then
return
end
addHeader(
box,
"زندگی و فعالیت"
)
addRow(
box,
"پیشه",
args.occupation
)
addRow(
box,
"زمینه فعالیت",
args.field
)
addRow(
box,
"تحصیلات",
args.education
)
end
----------------------------------------------------
-- LOAD EXTENSION MODULES
----------------------------------------------------
local function loadSection(moduleName, box, args)
local ok, module = pcall(
require,
moduleName
)
if ok and module and module.render then
module.render(
box,
args
)
end
end
----------------------------------------------------
-- RENDER
----------------------------------------------------
local function render(args)
local box = createBox()
addTitle(
box,
args
)
addImage(
box,
args
)
addPersonal(
box,
args
)
addCareer(
box,
args
)
--------------------------------------------------
-- تخصصها
--------------------------------------------------
loadSection(
"Module:InfoboxPerson/Sport",
box,
args
)
loadSection(
"Module:InfoboxPerson/Artist",
box,
args
)
loadSection(
"Module:InfoboxPerson/Scientist",
box,
args
)
return tostring(box)
end
----------------------------------------------------
-- ENTRY POINT
----------------------------------------------------
function p.main(frame)
local args = resolveArgs(
getArgs(frame)
)
return render(args)
end
return p