پودمان:AlborzSports: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش برچسب: واگردانی دستی |
جزبدون خلاصۀ ویرایش |
||
| خط ۳: | خط ۳: | ||
-- Wiki Alborz Sports Information System | -- Wiki Alborz Sports Information System | ||
-- Core Engine | -- Core Engine | ||
-- Version 2 | -- Version 2.1 | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۶۰: | خط ۶۰: | ||
local args = {} | local args = {} | ||
for k,v in pairs( | local source = frame | ||
local parent = frame:getParent() | |||
if parent and next(parent.args) then | |||
source = parent | |||
end | |||
for k,v in pairs(source.args) do | |||
local key = mw.text.trim(k) | local key = mw.text.trim(k) | ||
| خط ۷۳: | خط ۸۵: | ||
end | end | ||
return args | return args | ||
| خط ۱۰۹: | خط ۱۲۲: | ||
if not entity then | if not entity then | ||
entity = data.defaults.entity_type | entity = data.defaults.entity_type | ||
end | end | ||
if data.entities[entity] then | if data.entities[entity] then | ||
return entity | return entity | ||
end | end | ||
return "player" | return "player" | ||
| خط ۱۲۸: | خط ۱۴۷: | ||
if args.name then | if args.name then | ||
return args.name | return args.name | ||
end | end | ||
if data.entities[entity] then | if data.entities[entity] then | ||
return data.entities[entity].title | return data.entities[entity].title | ||
end | end | ||
return data.meta.title | return data.meta.title | ||
| خط ۱۴۵: | خط ۱۷۰: | ||
if not args.image then | if not args.image then | ||
return nil | return nil | ||
end | end | ||
local caption = args.caption or "" | local width = | ||
args.image_width or DEFAULT_IMAGE_WIDTH | |||
local caption = | |||
args.caption or "" | |||
local image = { | local image = { | ||
| خط ۱۶۳: | خط ۱۹۵: | ||
} | } | ||
return image | return image | ||
| خط ۱۷۶: | خط ۲۰۹: | ||
local fields = {} | local fields = {} | ||
for _,layout in ipairs(layouts) do | for _,layout in ipairs(layouts) do | ||
local label = layout[1] | local label = layout[1] | ||
local key = layout[2] | local key = layout[2] | ||
local value = args[key] | local value = args[key] | ||
value = clean(value) | value = clean(value) | ||
if value then | if value then | ||
table.insert(fields,{ | table.insert(fields,{ | ||
| خط ۱۹۶: | خط ۲۳۵: | ||
}) | }) | ||
end | end | ||
end | end | ||
return fields | return fields | ||
| خط ۲۱۴: | خط ۲۵۶: | ||
local groups = {} | local groups = {} | ||
local entityData = data.entities[entity] | |||
local entityData = | |||
data.entities[entity] | |||
if not entityData then | if not entityData then | ||
return groups | return groups | ||
end | end | ||
| خط ۲۲۴: | خط ۲۷۲: | ||
local layout = data.layouts[layoutName] | local layout = | ||
data.layouts[layoutName] | |||
| خط ۲۳۰: | خط ۲۷۹: | ||
local fields = buildFields(layout,args) | local fields = | ||
buildFields(layout,args) | |||
| خط ۲۳۶: | خط ۲۸۷: | ||
table.insert(groups,{ | |||
name = | |||
data.layoutTitles[layoutName] | |||
or layoutName, | |||
fields = fields | |||
}) | |||
end | end | ||
| خط ۲۶۸: | خط ۳۲۲: | ||
box.title = getTitle(entity,args) | box.title = | ||
getTitle(entity,args) | |||
local image = | |||
buildImage(args) | |||
| خط ۲۸۲: | خط ۳۴۰: | ||
box.groups = buildGroups(entity,args) | box.groups = | ||
buildGroups(entity,args) | |||
| خط ۳۱۶: | خط ۳۷۵: | ||
local result = {} | local result = {} | ||
table.insert( | |||
result, | |||
'<table class="infobox sport-infobox">' | |||
) | |||
| خط ۳۲۶: | خط ۳۸۸: | ||
if box.title then | if box.title then | ||
table.insert(result, | |||
table.insert( | |||
result, | |||
'<tr class="infobox-title">' .. | '<tr class="infobox-title">' .. | ||
| خط ۳۳۴: | خط ۳۹۸: | ||
) | ) | ||
end | end | ||
| خط ۳۵۲: | خط ۴۱۷: | ||
local size = image.size or DEFAULT_IMAGE_WIDTH | local size = | ||
image.size or DEFAULT_IMAGE_WIDTH | |||
local caption = image.caption or "" | local caption = | ||
image.caption or "" | |||
table.insert(result, | |||
table.insert( | |||
result, | |||
'<tr>' .. | '<tr>' .. | ||
| خط ۳۶۹: | خط ۴۳۸: | ||
']]' | ']]' | ||
) | |||
| خط ۳۷۶: | خط ۴۴۵: | ||
table.insert(result, | table.insert( | ||
result, | |||
'<br /><small>' .. | '<br /><small>' .. | ||
| خط ۳۸۸: | خط ۴۵۸: | ||
table.insert(result, | |||
table.insert( | |||
result, | |||
'</td></tr>' | '</td></tr>' | ||
| خط ۴۰۷: | خط ۴۷۹: | ||
table.insert(result, | table.insert( | ||
result, | |||
'<tr class="infobox-header">' .. | '<tr class="infobox-header">' .. | ||
| خط ۴۱۷: | خط ۴۸۹: | ||
'</th></tr>' | '</th></tr>' | ||
) | ) | ||
| خط ۴۲۶: | خط ۴۹۷: | ||
table.insert( | |||
result, | |||
'<tr>' .. | '<tr>' .. | ||
| خط ۴۴۰: | خط ۵۱۲: | ||
'</tr>' | '</tr>' | ||
) | ) | ||
| خط ۴۵۶: | خط ۵۲۷: | ||
------------------------------------------------------------ | ------------------------------------------------------------ | ||
table.insert(result, | table.insert( | ||
result, | |||
'</table>' | '</table>' | ||
| خط ۴۷۷: | خط ۵۴۹: | ||
local args = getArgs(frame) | local args = | ||
getArgs(frame) | |||
args = | |||
normalize(args) | |||
local entity = | |||
getEntity(args) | |||
local box = buildInfobox(entity,args) | |||
local box = | |||
buildInfobox( | |||
entity, | |||
args | |||
) | |||
| خط ۵۱۴: | خط ۵۹۴: | ||
function p.test(frame) | function p.test(frame) | ||
local args = getArgs(frame) | local args = | ||
getArgs(frame) | |||
args = | |||
normalize(args) | |||
local entityData = data.entities[entity] | local entity = | ||
getEntity(args) | |||
local entityData = | |||
data.entities[entity] | |||
| خط ۵۳۰: | خط ۶۱۸: | ||
end | end | ||
| خط ۵۳۶: | خط ۶۲۵: | ||
table.insert(result, | table.insert( | ||
result, | |||
"Entity: " .. entity | "Entity: " .. entity | ||
| خط ۵۴۴: | خط ۶۳۴: | ||
table.insert(result, | table.insert( | ||
result, | |||
"Title: " .. entityData.title | "Title: " .. entityData.title | ||
| خط ۵۵۲: | خط ۶۴۳: | ||
table.insert(result, | table.insert( | ||
result, | |||
"Layouts: " .. | "Layouts: " .. | ||
table.concat( | |||
table.concat(entityData.layouts,", ") | entityData.layouts, | ||
", " | |||
) | |||
) | ) | ||
return table.concat(result,"\n") | return table.concat( | ||
result, | |||
"\n" | |||
) | |||
end | end | ||
| خط ۵۷۴: | خط ۶۷۱: | ||
local result = {} | local result = {} | ||
| خط ۵۷۹: | خط ۶۷۷: | ||
table.insert(result, | table.insert( | ||
result, | |||
key .. " : " .. value.title | key .. " : " .. value.title | ||
| خط ۵۸۹: | خط ۶۸۹: | ||
return table.concat(result,"\n") | |||
return table.concat( | |||
result, | |||
"\n" | |||
) | |||
end | end | ||
نسخهٔ ۴ اوت ۲۰۲۶، ساعت ۱۲:۲۳
توضیحات این پودمان میتواند در پودمان:AlborzSports/توضیحات قرار گیرد.
--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Core Engine
-- Version 2.1
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzSports/Data")
--------------------------------------------------------------------------------
-- LOAD DEPENDENCIES
--------------------------------------------------------------------------------
local infobox = require("Module:Infobox")
--------------------------------------------------------------------------------
-- CONFIG
--------------------------------------------------------------------------------
local DEFAULT_IMAGE_WIDTH = data.meta.imageWidth or "250px"
--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
--------------------------------------------------------------------------------
local function clean(value)
if value == nil then
return nil
end
if type(value) == "string" then
value = mw.text.trim(value)
if value == "" then
return nil
end
end
return value
end
--------------------------------------------------------------------------------
-- GET ARGUMENTS
--------------------------------------------------------------------------------
local function getArgs(frame)
local args = {}
local source = frame
local parent = frame:getParent()
if parent and next(parent.args) then
source = parent
end
for k,v in pairs(source.args) do
local key = mw.text.trim(k)
local value = clean(v)
if value then
args[key] = value
end
end
return args
end
--------------------------------------------------------------------------------
-- NORMALIZE INPUT
--------------------------------------------------------------------------------
local function normalize(args)
local result = {}
for key,value in pairs(args) do
local field = data.lookup[key] or key
result[field] = value
end
return result
end
--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------
local function getEntity(args)
local entity = args.entity_type
if not entity then
entity = data.defaults.entity_type
end
if data.entities[entity] then
return entity
end
return "player"
end
--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------
local function getTitle(entity,args)
if args.name then
return args.name
end
if data.entities[entity] then
return data.entities[entity].title
end
return data.meta.title
end
--------------------------------------------------------------------------------
-- IMAGE HANDLING
--------------------------------------------------------------------------------
local function buildImage(args)
if not args.image then
return nil
end
local width =
args.image_width or DEFAULT_IMAGE_WIDTH
local caption =
args.caption or ""
local image = {
type = "image",
image = args.image,
size = width,
caption = caption
}
return image
end
--------------------------------------------------------------------------------
-- FIELD BUILDER
--------------------------------------------------------------------------------
local function buildFields(layouts,args)
local fields = {}
for _,layout in ipairs(layouts) do
local label = layout[1]
local key = layout[2]
local value = args[key]
value = clean(value)
if value then
table.insert(fields,{
label = label,
value = value
})
end
end
return fields
end
--------------------------------------------------------------------------------
-- GROUP BUILDER
--------------------------------------------------------------------------------
local function buildGroups(entity,args)
local groups = {}
local entityData =
data.entities[entity]
if not entityData then
return groups
end
for _,layoutName in ipairs(entityData.layouts) do
local layout =
data.layouts[layoutName]
if layout then
local fields =
buildFields(layout,args)
if #fields > 0 then
table.insert(groups,{
name =
data.layoutTitles[layoutName]
or layoutName,
fields = fields
})
end
end
end
return groups
end
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------
local function buildInfobox(entity,args)
local box = {}
box.title =
getTitle(entity,args)
local image =
buildImage(args)
if image then
box.image = image
end
box.groups =
buildGroups(entity,args)
return box
end
--------------------------------------------------------------------------------
-- RENDER TEXT
--------------------------------------------------------------------------------
local function renderValue(value)
if type(value) == "table" then
return table.concat(value,"، ")
end
return tostring(value)
end
--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------
local function renderInfobox(box)
local result = {}
table.insert(
result,
'<table class="infobox sport-infobox">'
)
------------------------------------------------------------
-- TITLE
------------------------------------------------------------
if box.title then
table.insert(
result,
'<tr class="infobox-title">' ..
'<th colspan="2">' ..
box.title ..
'</th></tr>'
)
end
------------------------------------------------------------
-- IMAGE
------------------------------------------------------------
if box.image then
local image = box.image
local file = image.image
local size =
image.size or DEFAULT_IMAGE_WIDTH
local caption =
image.caption or ""
table.insert(
result,
'<tr>' ..
'<td colspan="2" class="infobox-image">' ..
'[[File:' ..
file ..
'|' ..
size ..
']]'
)
if caption ~= "" then
table.insert(
result,
'<br /><small>' ..
caption ..
'</small>'
)
end
table.insert(
result,
'</td></tr>'
)
end
------------------------------------------------------------
-- GROUPS
------------------------------------------------------------
for _,group in ipairs(box.groups) do
table.insert(
result,
'<tr class="infobox-header">' ..
'<th colspan="2">' ..
group.name ..
'</th></tr>'
)
for _,field in ipairs(group.fields) do
table.insert(
result,
'<tr>' ..
'<th>' ..
field.label ..
'</th>' ..
'<td>' ..
renderValue(field.value) ..
'</td>' ..
'</tr>'
)
end
end
------------------------------------------------------------
-- CLOSE
------------------------------------------------------------
table.insert(
result,
'</table>'
)
return table.concat(result,"\n")
end
--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------
function p.main(frame)
local args =
getArgs(frame)
args =
normalize(args)
local entity =
getEntity(args)
local box =
buildInfobox(
entity,
args
)
return renderInfobox(box)
end
--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
--------------------------------------------------------------------------------
function p.infobox(frame)
return p.main(frame)
end
--------------------------------------------------------------------------------
-- DEBUG FUNCTION
--------------------------------------------------------------------------------
function p.test(frame)
local args =
getArgs(frame)
args =
normalize(args)
local entity =
getEntity(args)
local entityData =
data.entities[entity]
if not entityData then
return "Entity not found"
end
local result = {}
table.insert(
result,
"Entity: " .. entity
)
table.insert(
result,
"Title: " .. entityData.title
)
table.insert(
result,
"Layouts: " ..
table.concat(
entityData.layouts,
", "
)
)
return table.concat(
result,
"\n"
)
end
--------------------------------------------------------------------------------
-- GET ENTITY LIST
--------------------------------------------------------------------------------
function p.entities()
local result = {}
for key,value in pairs(data.entities) do
table.insert(
result,
key .. " : " .. value.title
)
end
return table.concat(
result,
"\n"
)
end
--------------------------------------------------------------------------------
-- GET VERSION
--------------------------------------------------------------------------------
function p.version()
return
data.version.name ..
" v" ..
data.version.major ..
"." ..
data.version.minor
end
--------------------------------------------------------------------------------
-- EXPORT
--------------------------------------------------------------------------------
return p