پودمان:InfoboxPerson/Core: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۴: | خط ۴: | ||
-- Module:InfoboxPerson/Core | -- Module:InfoboxPerson/Core | ||
-- WikiAlborz Infobox Engine | -- WikiAlborz Infobox Engine | ||
-- Version 1. | -- Version 1.1 | ||
-- Medal support added | |||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
| خط ۴۸: | خط ۴۹: | ||
value = mw.text.trim(value) | value = mw.text.trim(value) | ||
if value == '' then | if value == '' then | ||
return nil | return nil | ||
end | end | ||
-- Remove invisible Unicode characters | -- Remove invisible Unicode characters | ||
value = mw.ustring.gsub( | value = mw.ustring.gsub( | ||
value, | |||
'[\226\128\139\226\128\141\239\187\191]', | |||
'' | |||
) | ) | ||
if value == '' then | if value == '' then | ||
return nil | return nil | ||
end | end | ||
return value | return value | ||
| خط ۹۱: | خط ۹۶: | ||
return | return | ||
end | end | ||
for key, value in pairs(source) do | for key, value in pairs(source) do | ||
| خط ۹۶: | خط ۱۰۲: | ||
key = normalize(key) | key = normalize(key) | ||
value = normalize(value) | value = normalize(value) | ||
if key and value then | if key and value then | ||
| خط ۱۱۴: | خط ۱۲۱: | ||
local args = {} | local args = {} | ||
local parent = frame:getParent() | local parent = frame:getParent() | ||
if parent then | if parent then | ||
readArguments(parent.args, args) | readArguments(parent.args, args) | ||
end | end | ||
readArguments(frame.args, args) | readArguments(frame.args, args) | ||
return args | return args | ||
| خط ۱۳۶: | خط ۱۴۷: | ||
local args = {} | local args = {} | ||
for key, value in pairs(rawArgs) do | for key, value in pairs(rawArgs) do | ||
local canonical = lookup[key] | local canonical = lookup[key] | ||
if canonical then | if canonical then | ||
args[canonical] = value | args[canonical] = value | ||
else | else | ||
args[key] = value | args[key] = value | ||
end | end | ||
end | end | ||
return args | return args | ||
| خط ۱۷۳: | خط ۱۹۱: | ||
local title = args.name | local title = args.name | ||
if not hasValue(title) then | if not hasValue(title) then | ||
| خط ۱۹۸: | خط ۲۱۷: | ||
local image = args.image | local image = args.image | ||
if not hasValue(image) then | if not hasValue(image) then | ||
| خط ۲۱۷: | خط ۲۳۷: | ||
']]' | ']]' | ||
) | ) | ||
| خط ۲۹۰: | خط ۳۱۱: | ||
---------------------------------------------------------- | |||
-- ADD MEDALS | |||
---------------------------------------------------------- | |||
local function addMedals(box, args) | |||
local medals = args.medaltemplates | |||
if not hasValue(medals) then | |||
medals = args.medals | |||
end | |||
if not hasValue(medals) then | |||
return | |||
end | |||
local frame = mw.getCurrentFrame() | |||
local content = frame:expandTemplate{ | |||
title = 'Infobox medal templates', | |||
args = { | |||
medals = medals, | |||
title = args.medaltemplates_title or '', | |||
expand = 'yes' | |||
} | |||
} | |||
box:tag('tr') | |||
:tag('td') | |||
:attr('colspan','2') | |||
:addClass('person-infobox-medals') | |||
:wikitext(content) | |||
end | |||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- RENDER LAYOUT | -- RENDER LAYOUT | ||
| خط ۳۰۶: | خط ۳۷۰: | ||
local item = layout[i] | local item = layout[i] | ||
local label = item[1] | local label = item[1] | ||
| خط ۳۲۰: | خط ۳۸۵: | ||
end | end | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- CHECK GROUP TRIGGER | -- CHECK GROUP TRIGGER | ||
| خط ۴۲۸: | خط ۴۹۶: | ||
end | end | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- RENDER INFOBOX | -- RENDER INFOBOX | ||
| خط ۴۳۸: | خط ۵۰۳: | ||
local box = createBox() | local box = createBox() | ||
| خط ۴۴۴: | خط ۵۱۰: | ||
args | args | ||
) | ) | ||
| خط ۴۵۰: | خط ۵۱۷: | ||
args | args | ||
) | ) | ||
| خط ۴۵۶: | خط ۵۲۴: | ||
args | args | ||
) | ) | ||
-- Render medal templates inside the same infobox | |||
addMedals( | |||
box, | |||
args | |||
) | |||
| خط ۴۶۲: | خط ۵۳۹: | ||
end | end | ||
---------------------------------------------------------- | ---------------------------------------------------------- | ||
-- MAIN ENTRY POINT | -- MAIN ENTRY POINT | ||
نسخهٔ ۱۳ ژوئیهٔ ۲۰۲۶، ساعت ۱۳:۰۵
توضیحات این پودمان میتواند در پودمان:InfoboxPerson/Core/توضیحات قرار گیرد.
local p = {}
----------------------------------------------------------
-- Module:InfoboxPerson/Core
-- WikiAlborz Infobox Engine
-- Version 1.1
-- Medal support added
----------------------------------------------------------
local html = mw.html
local data = require('Module:InfoboxPerson/Data')
local lookup = data.lookup
local layouts = data.layouts
local groups = data.groups
local defaults = data.defaults
----------------------------------------------------------
-- LOAD TEMPLATESTYLES
----------------------------------------------------------
local function loadStyles()
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles',
args = {
src = 'Module:InfoboxPerson/styles.css'
}
}
end
----------------------------------------------------------
-- 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
-- Remove invisible Unicode characters
value = mw.ustring.gsub(
value,
'[\226\128\139\226\128\141\239\187\191]',
''
)
if value == '' then
return nil
end
return value
end
----------------------------------------------------------
-- HAS VALUE
----------------------------------------------------------
local function hasValue(value)
return normalize(value) ~= nil
end
----------------------------------------------------------
-- READ ARGUMENT TABLE
----------------------------------------------------------
local function readArguments(source, target)
if not source then
return
end
for key, value in pairs(source) do
key = normalize(key)
value = normalize(value)
if key and value then
target[key] = value
end
end
end
----------------------------------------------------------
-- COLLECT ARGUMENTS
----------------------------------------------------------
local function collectArgs(frame)
local args = {}
local parent = frame:getParent()
if parent then
readArguments(parent.args, args)
end
readArguments(frame.args, args)
return args
end
----------------------------------------------------------
-- RESOLVE ALIASES
----------------------------------------------------------
local function resolveArgs(rawArgs)
local args = {}
for key, value in pairs(rawArgs) do
local canonical = lookup[key]
if canonical then
args[canonical] = value
else
args[key] = value
end
end
return args
end
----------------------------------------------------------
-- CREATE INFOBOX
----------------------------------------------------------
local function createBox()
return html.create('table')
:addClass('infobox')
:addClass('person-infobox')
end
----------------------------------------------------------
-- ADD TITLE
----------------------------------------------------------
local function addTitle(box, args)
local title = args.name
if not hasValue(title) then
title = mw.title.getCurrentTitle().text
end
box:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('person-infobox-title')
:wikitext(title)
end
----------------------------------------------------------
-- ADD IMAGE
----------------------------------------------------------
local function addImage(box, args)
local image = args.image
if not hasValue(image) then
return
end
box:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('person-infobox-image')
:wikitext(
'[[File:' ..
image ..
'|' ..
(defaults.image_size or '250px') ..
']]'
)
if hasValue(args.caption) then
box:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('person-infobox-caption')
:wikitext(args.caption)
end
end
----------------------------------------------------------
-- ADD HEADER
----------------------------------------------------------
local function addHeader(box, title)
if not hasValue(title) then
return
end
box:tag('tr')
:tag('th')
:attr('colspan', '2')
:addClass('person-infobox-header')
:wikitext(title)
end
----------------------------------------------------------
-- ADD ROW
----------------------------------------------------------
local function addRow(box, label, value)
if not hasValue(value) then
return false
end
local row = box:tag('tr')
row:tag('th')
:addClass('person-infobox-label')
:wikitext(label)
row:tag('td')
:addClass('person-infobox-value')
:wikitext(value)
return true
end
----------------------------------------------------------
-- ADD MEDALS
----------------------------------------------------------
local function addMedals(box, args)
local medals = args.medaltemplates
if not hasValue(medals) then
medals = args.medals
end
if not hasValue(medals) then
return
end
local frame = mw.getCurrentFrame()
local content = frame:expandTemplate{
title = 'Infobox medal templates',
args = {
medals = medals,
title = args.medaltemplates_title or '',
expand = 'yes'
}
}
box:tag('tr')
:tag('td')
:attr('colspan','2')
:addClass('person-infobox-medals')
:wikitext(content)
end
----------------------------------------------------------
-- RENDER LAYOUT
----------------------------------------------------------
local function renderLayout(box, layout, args)
if not layout then
return
end
for i = 1, #layout do
local item = layout[i]
local label = item[1]
local field = item[2]
addRow(
box,
label,
args[field]
)
end
end
----------------------------------------------------------
-- CHECK GROUP TRIGGER
----------------------------------------------------------
local function checkTrigger(group, args)
if not group.trigger then
return true
end
for i = 1, #group.trigger do
local field = group.trigger[i]
if hasValue(args[field]) then
return true
end
end
return false
end
----------------------------------------------------------
-- RENDER GROUP
----------------------------------------------------------
local function renderGroup(box, group, args)
if not checkTrigger(group, args) then
return
end
if hasValue(group.title) then
addHeader(
box,
group.title
)
end
renderLayout(
box,
layouts[group.layout],
args
)
end
----------------------------------------------------------
-- RENDER ALL GROUPS
----------------------------------------------------------
local function renderGroups(box, args)
for i = 1, #groups do
renderGroup(
box,
groups[i],
args
)
end
end
----------------------------------------------------------
-- PREPARE ARGUMENTS
----------------------------------------------------------
local function prepareArgs(args)
for key, value in pairs(defaults) do
if args[key] == nil then
args[key] = value
end
end
return args
end
----------------------------------------------------------
-- RENDER INFOBOX
----------------------------------------------------------
local function render(args)
local box = createBox()
addTitle(
box,
args
)
addImage(
box,
args
)
renderGroups(
box,
args
)
-- Render medal templates inside the same infobox
addMedals(
box,
args
)
return loadStyles()
.. tostring(box)
end
----------------------------------------------------------
-- MAIN ENTRY POINT
----------------------------------------------------------
function p.main(frame)
local rawArgs = collectArgs(frame)
local args = resolveArgs(rawArgs)
args = prepareArgs(args)
return render(args)
end
----------------------------------------------------------
-- EXPORT
----------------------------------------------------------
return p