پودمان:AlborzSports: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۳: | خط ۳: | ||
-- Wiki Alborz Sports Information System | -- Wiki Alborz Sports Information System | ||
-- Core Engine | -- Core Engine | ||
-- Version 2. | -- Version 2.1 | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۲: | خط ۱۲: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local data = mw.loadData("Module:AlborzSports/Data") | local data = | ||
mw.loadData("Module:AlborzSports/Data") | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۸: | خط ۱۹: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local infobox = require("Module:Infobox") | local infobox = | ||
require("Module:Infobox") | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۲۵: | خط ۲۷: | ||
local DEFAULT_IMAGE_WIDTH = | local DEFAULT_IMAGE_WIDTH = | ||
data.meta.imageWidth or " | data.meta.imageWidth or "280px" | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۳۹: | خط ۴۱: | ||
if type(value) == "string" then | if type(value) == "string" then | ||
value = mw.text.trim(value) | value = | ||
mw.text.trim(value) | |||
if value == "" then | if value == "" then | ||
| خط ۴۸: | خط ۵۱: | ||
return value | return value | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- HAS VALUE | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۶۲: | خط ۶۵: | ||
if type(value) == "string" then | if type(value) == "string" then | ||
return mw.text.trim(value) ~= "" | return mw.text.trim(value) ~= "" | ||
end | end | ||
if type(value) == "table" then | if type(value) == "table" then | ||
return next(value) ~= nil | return next(value) ~= nil | ||
end | end | ||
return true | return true | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۸۱: | خط ۸۸: | ||
local args = {} | local args = {} | ||
local source = frame | local source = | ||
frame | |||
local parent = frame:getParent() | local parent = | ||
frame:getParent() | |||
if parent and next(parent.args) then | if parent and next(parent.args) then | ||
source = parent | source = | ||
parent | |||
end | end | ||
| خط ۹۵: | خط ۱۰۵: | ||
for k, v in pairs(source.args) do | for k, v in pairs(source.args) do | ||
local key = mw.text.trim(k) | local key = | ||
mw.text.trim(k) | |||
local value = | |||
clean(v) | |||
if value then | if value then | ||
args[key] = value | args[key] = | ||
value | |||
end | end | ||
| خط ۱۰۹: | خط ۱۲۳: | ||
return args | return args | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۱۹: | خط ۱۳۳: | ||
local result = {} | local result = {} | ||
for key, value in pairs(args) do | for key, value in pairs(args) do | ||
local field = | local field = | ||
data.lookup[key] or key | data.lookup[key] | ||
or key | |||
result[field] = value | result[field] = | ||
value | |||
end | end | ||
return result | return result | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۳۹: | خط ۱۵۸: | ||
local function getEntity(args) | local function getEntity(args) | ||
local entity = args.entity_type | local entity = | ||
args.entity_type | |||
if not entity then | if not entity then | ||
| خط ۱۵۷: | خط ۱۷۸: | ||
return "player" | return "player" | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۸۱: | خط ۲۰۲: | ||
return data.meta.title | return data.meta.title | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۹۸: | خط ۲۱۹: | ||
local width = | local width = | ||
args.image_width or DEFAULT_IMAGE_WIDTH | args.image_width | ||
or DEFAULT_IMAGE_WIDTH | |||
local caption = | local caption = | ||
args.caption or "" | args.caption | ||
or "" | |||
return { | |||
type = "image", | type = "image", | ||
| خط ۲۱۷: | خط ۲۴۰: | ||
} | } | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۲۵۰: | خط ۲۷۰: | ||
if value then | if value then | ||
table.insert(fields, { | table.insert( | ||
fields, | |||
{ | |||
label = label, | |||
value = value | |||
} | |||
) | |||
end | end | ||
| خط ۲۶۴: | خط ۲۹۰: | ||
return fields | return fields | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۲۹۶: | خط ۳۲۲: | ||
local fields = | local fields = | ||
buildFields(layout, args) | buildFields( | ||
layout, | |||
args | |||
) | |||
---------------------------------------------------- | |||
-- ADD GROUP | |||
---------------------------------------------------- | |||
if #fields > 0 then | if #fields > 0 then | ||
table.insert(groups, { | table.insert( | ||
groups, | |||
{ | |||
name = | |||
data.layoutTitles[layoutName] | |||
or layoutName, | |||
layoutName = | |||
layoutName, | |||
fields = | |||
fields | |||
} | |||
) | |||
end | end | ||
| خط ۳۲۳: | خط ۳۶۲: | ||
return groups | return groups | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۳۳۶: | خط ۳۷۵: | ||
box.title = | box.title = | ||
getTitle(entity, args) | getTitle( | ||
entity, | |||
args | |||
) | |||
| خط ۳۵۲: | خط ۳۹۴: | ||
box.groups = | box.groups = | ||
buildGroups(entity, args) | buildGroups( | ||
entity, | |||
args | |||
) | |||
return box | return box | ||
end | end | ||
-------------------------------------------------------------------------------- | |||
-- RENDER VALUE | |||
-------------------------------------------------------------------------------- | |||
local function renderValue(value) | |||
if type(value) == "table" then | |||
return table.concat( | |||
value, | |||
"، " | |||
) | |||
end | |||
return tostring(value) | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- MEDALS | -- RENDER MEDALS | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- The medal templates are expanded separately because | ||
-- | -- Infobox medal templates generates its own wiki table. | ||
-- | -- | ||
-- | -- IMPORTANT: | ||
-- Do NOT place this output inside <td>. | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function | local function renderMedals(args) | ||
local medals = | local medals = | ||
| خط ۳۸۶: | خط ۴۵۰: | ||
if not hasValue(medals) then | if not hasValue(medals) then | ||
return | return nil | ||
end | end | ||
| خط ۴۲۰: | خط ۴۸۴: | ||
if not hasValue(content) then | if not hasValue(content) then | ||
return | return nil | ||
end | end | ||
return content | |||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- RENDER | -- RENDER INFOBOX | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function | local function renderInfobox(box, args) | ||
local result = {} | |||
local medals = | |||
renderMedals(args) | |||
local medalsRendered = | |||
false | |||
------------------------------------------------------------ | |||
-- | -- OPEN MAIN TABLE | ||
------------------------------------------------------------ | |||
table.insert( | |||
result, | result, | ||
| خط ۵۰۴: | خط ۵۵۴: | ||
local image = | local image = | ||
box.image | box.image | ||
local file = | local file = | ||
image.image | image.image | ||
local size = | local size = | ||
image.size or DEFAULT_IMAGE_WIDTH | image.size | ||
or DEFAULT_IMAGE_WIDTH | |||
local caption = | local caption = | ||
image.caption or "" | image.caption | ||
or "" | |||
| خط ۵۷۰: | خط ۶۲۵: | ||
for _, group in ipairs(box.groups) do | for _, group in ipairs(box.groups) do | ||
-------------------------------------------------------- | |||
-- GROUP HEADER | |||
-------------------------------------------------------- | |||
table.insert( | table.insert( | ||
| خط ۵۸۵: | خط ۶۴۴: | ||
) | ) | ||
-------------------------------------------------------- | |||
-- GROUP FIELDS | |||
-------------------------------------------------------- | |||
for _, field in ipairs(group.fields) do | for _, field in ipairs(group.fields) do | ||
table.insert( | table.insert( | ||
| خط ۶۱۷: | خط ۶۷۹: | ||
-- MEDALS | -- MEDALS | ||
-------------------------------------------------------- | -------------------------------------------------------- | ||
-- | -- The honors group is the insertion point for the | ||
-- independent medal table. | |||
-------------------------------------------------------- | -------------------------------------------------------- | ||
if group.layoutName == "honors" then | if group.layoutName == "honors" | ||
and medals | |||
and not medalsRendered then | |||
---------------------------------------------------- | |||
-- CLOSE MAIN TABLE | |||
---------------------------------------------------- | |||
table.insert( | |||
result, | |||
'</table>' | |||
) | |||
---------------------------------------------------- | |||
-- MEDAL TABLE | |||
---------------------------------------------------- | |||
table.insert( | |||
result, | |||
medals | |||
) | |||
medalsRendered = | |||
true | |||
---------------------------------------------------- | |||
-- REOPEN MAIN TABLE | |||
---------------------------------------------------- | |||
table.insert( | |||
result, | result, | ||
'<table class="sport-infobox">' | |||
) | ) | ||
end | end | ||
end | |||
------------------------------------------------------------ | |||
-- MEDALS WITHOUT HONORS GROUP | |||
------------------------------------------------------------ | |||
-- Safety fallback: | |||
-- If medals exist but the honors group was not generated | |||
-- because there are no ordinary honors, still render them. | |||
------------------------------------------------------------ | |||
if medals | |||
and not medalsRendered then | |||
-------------------------------------------------------- | |||
-- CLOSE MAIN TABLE | |||
-------------------------------------------------------- | |||
table.insert( | |||
result, | |||
'</table>' | |||
) | |||
-------------------------------------------------------- | |||
-- MEDAL TABLE | |||
-------------------------------------------------------- | |||
table.insert( | |||
result, | |||
medals | |||
) | |||
medalsRendered = | |||
true | |||
-------------------------------------------------------- | |||
-- REOPEN MAIN TABLE | |||
-------------------------------------------------------- | |||
table.insert( | |||
result, | |||
'<table class="sport-infobox">' | |||
) | |||
end | end | ||
| خط ۶۴۹: | خط ۸۰۹: | ||
"\n" | "\n" | ||
) | ) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۶۸۱: | خط ۸۴۱: | ||
args | args | ||
) | ) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۶۹۳: | خط ۸۵۳: | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۷۲۸: | خط ۸۸۷: | ||
table.insert( | table.insert( | ||
result, | result, | ||
| خط ۷۳۷: | خط ۸۹۷: | ||
table.insert( | table.insert( | ||
result, | result, | ||
| خط ۷۴۶: | خط ۹۰۷: | ||
table.insert( | table.insert( | ||
result, | result, | ||
| خط ۷۶۲: | خط ۹۲۴: | ||
return table.concat( | return table.concat( | ||
result, | result, | ||
"\n" | "\n" | ||
) | ) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۷۹۳: | خط ۹۵۸: | ||
return table.concat( | return table.concat( | ||
result, | result, | ||
"\n" | "\n" | ||
) | ) | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۸۱۶: | خط ۹۸۴: | ||
data.version.minor | data.version.minor | ||
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 "280px"
--------------------------------------------------------------------------------
-- 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
--------------------------------------------------------------------------------
-- HAS VALUE
--------------------------------------------------------------------------------
local function hasValue(value)
if value == nil then
return false
end
if type(value) == "string" then
return mw.text.trim(value) ~= ""
end
if type(value) == "table" then
return next(value) ~= nil
end
return true
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 ""
return {
type = "image",
image = args.image,
size = width,
caption = caption
}
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
)
----------------------------------------------------
-- ADD GROUP
----------------------------------------------------
if #fields > 0 then
table.insert(
groups,
{
name =
data.layoutTitles[layoutName]
or layoutName,
layoutName =
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 VALUE
--------------------------------------------------------------------------------
local function renderValue(value)
if type(value) == "table" then
return table.concat(
value,
"، "
)
end
return tostring(value)
end
--------------------------------------------------------------------------------
-- RENDER MEDALS
--------------------------------------------------------------------------------
-- The medal templates are expanded separately because
-- Infobox medal templates generates its own wiki table.
--
-- IMPORTANT:
-- Do NOT place this output inside <td>.
--------------------------------------------------------------------------------
local function renderMedals(args)
local medals =
args.medaltemplates
if not hasValue(medals) then
medals =
args.medals
end
if not hasValue(medals) then
return nil
end
local frame =
mw.getCurrentFrame()
local content =
frame:expandTemplate{
title =
"Infobox medal templates",
args = {
medals =
medals,
title =
args.medaltemplates_title
or "",
expand =
"yes"
}
}
if not hasValue(content) then
return nil
end
return content
end
--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------
local function renderInfobox(box, args)
local result = {}
local medals =
renderMedals(args)
local medalsRendered =
false
------------------------------------------------------------
-- OPEN MAIN TABLE
------------------------------------------------------------
table.insert(
result,
'<table class="sport-infobox">'
)
------------------------------------------------------------
-- TITLE
------------------------------------------------------------
if box.title then
table.insert(
result,
'<tr class="sport-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="sport-infobox-image">' ..
'[[File:' ..
file ..
'|' ..
size ..
']]'
)
if caption ~= "" then
table.insert(
result,
'<div class="sport-infobox-caption">' ..
caption ..
'</div>'
)
end
table.insert(
result,
'</td></tr>'
)
end
------------------------------------------------------------
-- GROUPS
------------------------------------------------------------
for _, group in ipairs(box.groups) do
--------------------------------------------------------
-- GROUP HEADER
--------------------------------------------------------
table.insert(
result,
'<tr class="sport-infobox-header">' ..
'<th colspan="2">' ..
group.name ..
'</th></tr>'
)
--------------------------------------------------------
-- GROUP FIELDS
--------------------------------------------------------
for _, field in ipairs(group.fields) do
table.insert(
result,
'<tr>' ..
'<th class="sport-infobox-label">' ..
field.label ..
'</th>' ..
'<td class="sport-infobox-value">' ..
renderValue(field.value) ..
'</td>' ..
'</tr>'
)
end
--------------------------------------------------------
-- MEDALS
--------------------------------------------------------
-- The honors group is the insertion point for the
-- independent medal table.
--------------------------------------------------------
if group.layoutName == "honors"
and medals
and not medalsRendered then
----------------------------------------------------
-- CLOSE MAIN TABLE
----------------------------------------------------
table.insert(
result,
'</table>'
)
----------------------------------------------------
-- MEDAL TABLE
----------------------------------------------------
table.insert(
result,
medals
)
medalsRendered =
true
----------------------------------------------------
-- REOPEN MAIN TABLE
----------------------------------------------------
table.insert(
result,
'<table class="sport-infobox">'
)
end
end
------------------------------------------------------------
-- MEDALS WITHOUT HONORS GROUP
------------------------------------------------------------
-- Safety fallback:
-- If medals exist but the honors group was not generated
-- because there are no ordinary honors, still render them.
------------------------------------------------------------
if medals
and not medalsRendered then
--------------------------------------------------------
-- CLOSE MAIN TABLE
--------------------------------------------------------
table.insert(
result,
'</table>'
)
--------------------------------------------------------
-- MEDAL TABLE
--------------------------------------------------------
table.insert(
result,
medals
)
medalsRendered =
true
--------------------------------------------------------
-- REOPEN MAIN TABLE
--------------------------------------------------------
table.insert(
result,
'<table class="sport-infobox">'
)
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,
args
)
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