پودمان:AlborzSports: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۱۲: | خط ۱۲: | ||
local data = mw.loadData("Module:AlborzSports/Data") | local data = mw.loadData("Module:AlborzSports/Data") | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۱۷: | خط ۱۸: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local INFOBOX_CLASS = data.meta.class or "infobox sport-infobox" | local INFOBOX_CLASS = | ||
data.meta.class | |||
or "infobox sport-infobox" | |||
local DEFAULT_IMAGE_WIDTH = | local DEFAULT_IMAGE_WIDTH = | ||
data.defaults.image_width | data.defaults.image_width | ||
data.meta.imageWidth | or data.meta.imageWidth | ||
"250px" | or "250px" | ||
| خط ۳۵: | خط ۳۹: | ||
end | end | ||
value = mw.text.trim(value) | |||
value = | |||
mw.text.trim( | |||
tostring(value) | |||
) | |||
if value == "" then | if value == "" then | ||
return nil | return nil | ||
end | end | ||
return value | return value | ||
| خط ۴۷: | خط ۵۷: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- ARGUMENT | -- ARGUMENT SOURCE | ||
-------------------------------------------------------------------------------- | |||
local function getArgsFrame(frame) | |||
local parent = | |||
frame:getParent() | |||
if parent | |||
and next(parent.args) then | |||
return parent | |||
end | |||
return frame | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- NORMALIZE ARGUMENTS | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function normalizeArgs(frame) | local function normalizeArgs(frame) | ||
frame = | |||
getArgsFrame(frame) | |||
local args = {} | local args = {} | ||
for key, value in pairs(frame.args) do | for key, value in pairs(frame.args) do | ||
local cleanValue = clean(value) | local cleanKey = | ||
mw.text.trim( | |||
tostring(key) | |||
) | |||
local cleanValue = | |||
clean(value) | |||
if cleanValue then | if cleanValue then | ||
local field = | local field = | ||
data.lookup[cleanKey] or | data.lookup[cleanKey] | ||
or cleanKey | |||
args[field] = | |||
cleanValue | |||
end | end | ||
end | end | ||
return args | return args | ||
| خط ۸۳: | خط ۱۳۳: | ||
local function detectEntity(args) | local function detectEntity(args) | ||
if args.entity_type | |||
and data.entities[args.entity_type] then | |||
return args.entity_type | |||
end | end | ||
return | |||
data.defaults.entity_type | |||
or "player" | |||
end | end | ||
| خط ۱۲۱: | خط ۱۵۰: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- GET ENTITY | -- GET ENTITY | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function getEntity(entityType) | local function getEntity(entityType) | ||
if data.entities[entityType] then | if data.entities[entityType] then | ||
| خط ۱۳۳: | خط ۱۶۳: | ||
return data.entities.player | return | ||
data.entities.player | |||
end | end | ||
| خط ۱۳۹: | خط ۱۷۰: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- GET VALUE | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function getValue(args, field) | local function getValue(args, field) | ||
local value = args[field] | local value = | ||
args[field] | |||
| خط ۱۶۰: | خط ۱۹۲: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- LABEL | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function getLabel(field) | local function getLabel(field) | ||
return data.fieldDefinitions[field].label | if data.fieldDefinitions | ||
and data.fieldDefinitions[field] | |||
and data.fieldDefinitions[field].label then | |||
return | |||
data.fieldDefinitions[field].label | |||
end | end | ||
| خط ۱۷۵: | خط ۲۱۲: | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- TITLE | -- TITLE | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderTitle(box, entityType, args) | local function renderTitle(box, entityType, args) | ||
local entity = | |||
getEntity(entityType) | |||
local title = | local title = | ||
args.name | args.name | ||
or entity.title | |||
data.meta.title | or data.meta.title | ||
box | box | ||
:tag("tr") | |||
:tag("th") | :tag("th") | ||
:attr("colspan", "2") | :attr("colspan","2") | ||
:addClass("infobox-title") | :addClass("infobox-title") | ||
:wikitext(title) | :wikitext(title) | ||
end | end | ||
| خط ۱۹۷: | خط ۲۴۴: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- IMAGE | -- IMAGE | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderImage(box, args) | local function renderImage(box,args) | ||
if not args.image then | |||
return | return | ||
end | end | ||
local width = | local width = | ||
args.image_width | args.image_width | ||
DEFAULT_IMAGE_WIDTH | or DEFAULT_IMAGE_WIDTH | ||
local image = | |||
mw.html.create("div") | mw.html.create("div") | ||
:addClass("infobox-image") | :addClass("infobox-image") | ||
image | |||
:wikitext( | :wikitext( | ||
string.format( | string.format( | ||
"[[پرونده:%s|%s]]", | "[[پرونده:%s|%s]]", | ||
image, | args.image, | ||
width | width | ||
) | ) | ||
| خط ۲۴۱: | خط ۲۸۱: | ||
if args.caption then | if args.caption then | ||
image | |||
:tag("div") | :tag("div") | ||
:addClass("infobox-caption") | :addClass("infobox-caption") | ||
:wikitext(args.caption) | :wikitext(args.caption) | ||
end | end | ||
| خط ۲۵۲: | خط ۲۹۵: | ||
:tag("tr") | :tag("tr") | ||
:tag("td") | :tag("td") | ||
:attr("colspan", "2") | :attr("colspan","2") | ||
:node( | :node(image) | ||
end | end | ||
| خط ۲۵۹: | خط ۳۰۳: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- SECTION HEADER | -- SECTION HEADER | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderSectionHeader(box, layout) | local function renderSectionHeader(box,layout) | ||
local title = | local title = | ||
layout | |||
| خط ۲۷۶: | خط ۳۱۷: | ||
:tag("tr") | :tag("tr") | ||
:tag("th") | :tag("th") | ||
:attr("colspan", "2") | :attr("colspan","2") | ||
:addClass("infobox-header") | :addClass("infobox-header") | ||
:wikitext(title) | :wikitext(title) | ||
end | end | ||
| خط ۲۸۷: | خط ۳۲۹: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderField(box, field, value | local function renderField(box,field,value) | ||
local row = | local row = | ||
box | box | ||
:tag("tr") | |||
row | row | ||
:tag("th") | :tag("th") | ||
:wikitext( | :wikitext( | ||
getLabel(field) | |||
) | |||
| خط ۳۱۲: | خط ۳۴۹: | ||
:tag("td") | :tag("td") | ||
:wikitext(value) | :wikitext(value) | ||
end | end | ||
| خط ۳۱۷: | خط ۳۵۵: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- SPECIAL | -- SPECIAL FIELDS | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderSpecialField(box, field, value) | local function renderSpecialField(box,field,value) | ||
| خط ۳۲۸: | خط ۳۶۶: | ||
value = | value = | ||
string.format( | string.format( | ||
"[%s | "[%s وبگاه]", | ||
value | value | ||
) | ) | ||
| خط ۳۵۴: | خط ۳۹۱: | ||
end | |||
| خط ۳۷۸: | خط ۴۰۶: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderLayout(box, args, layout) | local function renderLayout(box,args,layout) | ||
| خط ۳۸۶: | خط ۴۱۴: | ||
if not fields then | if not fields then | ||
return | return | ||
end | end | ||
local rows = {} | |||
for _, item in ipairs(fields) do | |||
local label = | |||
item[1] | |||
local field = | |||
item[2] | |||
| خط ۴۰۵: | خط ۴۴۵: | ||
if value then | if value then | ||
table.insert( | |||
rows, | |||
{ | |||
label = label, | |||
field = field, | |||
value = value | |||
} | |||
) | |||
end | end | ||
end | end | ||
if | |||
if #rows == 0 then | |||
return | return | ||
end | end | ||
| خط ۴۲۷: | خط ۴۷۷: | ||
for _, row in ipairs(rows) do | |||
renderSpecialField( | |||
box, | |||
row.field, | |||
row.value | |||
) | |||
end | end | ||
| خط ۴۵۳: | خط ۴۹۲: | ||
end | end | ||
| خط ۴۵۹: | خط ۴۹۹: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function renderEntity(box, args, entityType) | local function renderEntity(box,args,entityType) | ||
local entity = | local entity = | ||
getEntity(entityType) | getEntity(entityType) | ||
| خط ۴۷۱: | خط ۵۱۲: | ||
end | end | ||
| خط ۴۸۷: | خط ۵۲۹: | ||
end | end | ||
| خط ۴۹۸: | خط ۵۴۱: | ||
local entityType = | local entityType = | ||
detectEntity(args) | detectEntity(args) | ||
local box = | local box = | ||
mw.html.create("table") | mw.html.create("table") | ||
| خط ۵۰۸: | خط ۵۵۳: | ||
INFOBOX_CLASS | INFOBOX_CLASS | ||
) | ) | ||
| خط ۵۱۵: | خط ۵۶۱: | ||
args | args | ||
) | ) | ||
| خط ۵۲۱: | خط ۵۶۸: | ||
args | args | ||
) | ) | ||
| خط ۵۳۰: | خط ۵۷۸: | ||
return tostring(box) | |||
return | |||
tostring(box) | |||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- MAIN | -- MAIN | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۵۴۳: | خط ۵۹۵: | ||
local args = | local args = | ||
normalizeArgs( | normalizeArgs(frame) | ||
return | |||
buildInfobox(args) | |||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- INVOKE | -- INVOKE | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function p. | function p.invoke(frame) | ||
return | |||
p.main(frame) | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- RENDER | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function p.render(frame) | |||
return | return | ||
p.main(frame) | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- TEST | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function p. | function p.test(frame) | ||
local args = | local args = { | ||
name = | |||
"علیرضا جهانبخش", | |||
entity_type = | |||
"player", | |||
nationality = | |||
"ایرانی", | |||
country = | |||
"ایران", | |||
sport = | |||
"فوتبال", | |||
discipline = | |||
"فوتبال", | |||
position = | |||
"هافبک", | |||
current_club = | |||
"هیرنفین", | |||
birth_date = | |||
"۱۱ مرداد ۱۳۷۲", | |||
birth_place = | |||
"جیرنده" | |||
} | |||
return | |||
buildInfobox(args) | |||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- EXPORT | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
return p | return p | ||
نسخهٔ ۴ اوت ۲۰۲۶، ساعت ۱۱:۵۵
توضیحات این پودمان میتواند در پودمان:AlborzSports/توضیحات قرار گیرد.
--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Version 3.0
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData("Module:AlborzSports/Data")
--------------------------------------------------------------------------------
-- CONFIGURATION
--------------------------------------------------------------------------------
local INFOBOX_CLASS =
data.meta.class
or "infobox sport-infobox"
local DEFAULT_IMAGE_WIDTH =
data.defaults.image_width
or data.meta.imageWidth
or "250px"
--------------------------------------------------------------------------------
-- HELPERS
--------------------------------------------------------------------------------
local function clean(value)
if not value then
return nil
end
value =
mw.text.trim(
tostring(value)
)
if value == "" then
return nil
end
return value
end
--------------------------------------------------------------------------------
-- ARGUMENT SOURCE
--------------------------------------------------------------------------------
local function getArgsFrame(frame)
local parent =
frame:getParent()
if parent
and next(parent.args) then
return parent
end
return frame
end
--------------------------------------------------------------------------------
-- NORMALIZE ARGUMENTS
--------------------------------------------------------------------------------
local function normalizeArgs(frame)
frame =
getArgsFrame(frame)
local args = {}
for key, value in pairs(frame.args) do
local cleanKey =
mw.text.trim(
tostring(key)
)
local cleanValue =
clean(value)
if cleanValue then
local field =
data.lookup[cleanKey]
or cleanKey
args[field] =
cleanValue
end
end
return args
end
--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------
local function detectEntity(args)
if args.entity_type
and data.entities[args.entity_type] then
return args.entity_type
end
return
data.defaults.entity_type
or "player"
end
--------------------------------------------------------------------------------
-- GET ENTITY
--------------------------------------------------------------------------------
local function getEntity(entityType)
if data.entities[entityType] then
return data.entities[entityType]
end
return
data.entities.player
end
--------------------------------------------------------------------------------
-- GET VALUE
--------------------------------------------------------------------------------
local function getValue(args, field)
local value =
args[field]
if value then
return value
end
return nil
end
--------------------------------------------------------------------------------
-- LABEL
--------------------------------------------------------------------------------
local function getLabel(field)
if data.fieldDefinitions
and data.fieldDefinitions[field]
and data.fieldDefinitions[field].label then
return
data.fieldDefinitions[field].label
end
return field
end
--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------
local function renderTitle(box, entityType, args)
local entity =
getEntity(entityType)
local title =
args.name
or entity.title
or data.meta.title
box
:tag("tr")
:tag("th")
:attr("colspan","2")
:addClass("infobox-title")
:wikitext(title)
end
--------------------------------------------------------------------------------
-- IMAGE
--------------------------------------------------------------------------------
local function renderImage(box,args)
if not args.image then
return
end
local width =
args.image_width
or DEFAULT_IMAGE_WIDTH
local image =
mw.html.create("div")
:addClass("infobox-image")
image
:wikitext(
string.format(
"[[پرونده:%s|%s]]",
args.image,
width
)
)
if args.caption then
image
:tag("div")
:addClass("infobox-caption")
:wikitext(args.caption)
end
box
:tag("tr")
:tag("td")
:attr("colspan","2")
:node(image)
end
--------------------------------------------------------------------------------
-- SECTION HEADER
--------------------------------------------------------------------------------
local function renderSectionHeader(box,layout)
local title =
layout
box
:tag("tr")
:tag("th")
:attr("colspan","2")
:addClass("infobox-header")
:wikitext(title)
end
--------------------------------------------------------------------------------
-- FIELD RENDERER
--------------------------------------------------------------------------------
local function renderField(box,field,value)
local row =
box
:tag("tr")
row
:tag("th")
:wikitext(
getLabel(field)
)
row
:tag("td")
:wikitext(value)
end
--------------------------------------------------------------------------------
-- SPECIAL FIELDS
--------------------------------------------------------------------------------
local function renderSpecialField(box,field,value)
if field == "website" then
value =
string.format(
"[%s وبگاه]",
value
)
elseif field == "instagram" then
value =
string.format(
"[%s اینستاگرام]",
value
)
elseif field == "telegram" then
value =
string.format(
"[%s تلگرام]",
value
)
end
renderField(
box,
field,
value
)
end
--------------------------------------------------------------------------------
-- LAYOUT PROCESSOR
--------------------------------------------------------------------------------
local function renderLayout(box,args,layout)
local fields =
data.layouts[layout]
if not fields then
return
end
local rows = {}
for _, item in ipairs(fields) do
local label =
item[1]
local field =
item[2]
local value =
getValue(
args,
field
)
if value then
table.insert(
rows,
{
label = label,
field = field,
value = value
}
)
end
end
if #rows == 0 then
return
end
renderSectionHeader(
box,
layout
)
for _, row in ipairs(rows) do
renderSpecialField(
box,
row.field,
row.value
)
end
end
--------------------------------------------------------------------------------
-- ENTITY RENDERER
--------------------------------------------------------------------------------
local function renderEntity(box,args,entityType)
local entity =
getEntity(entityType)
if not entity then
return
end
for _, layout in ipairs(entity.layouts) do
renderLayout(
box,
args,
layout
)
end
end
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------
local function buildInfobox(args)
local entityType =
detectEntity(args)
local box =
mw.html.create("table")
box
:addClass(
INFOBOX_CLASS
)
renderTitle(
box,
entityType,
args
)
renderImage(
box,
args
)
renderEntity(
box,
args,
entityType
)
return
tostring(box)
end
--------------------------------------------------------------------------------
-- MAIN
--------------------------------------------------------------------------------
function p.main(frame)
local args =
normalizeArgs(frame)
return
buildInfobox(args)
end
--------------------------------------------------------------------------------
-- INVOKE
--------------------------------------------------------------------------------
function p.invoke(frame)
return
p.main(frame)
end
--------------------------------------------------------------------------------
-- RENDER
--------------------------------------------------------------------------------
function p.render(frame)
return
p.main(frame)
end
--------------------------------------------------------------------------------
-- TEST
--------------------------------------------------------------------------------
function p.test(frame)
local args = {
name =
"علیرضا جهانبخش",
entity_type =
"player",
nationality =
"ایرانی",
country =
"ایران",
sport =
"فوتبال",
discipline =
"فوتبال",
position =
"هافبک",
current_club =
"هیرنفین",
birth_date =
"۱۱ مرداد ۱۳۷۲",
birth_place =
"جیرنده"
}
return
buildInfobox(args)
end
--------------------------------------------------------------------------------
-- EXPORT
--------------------------------------------------------------------------------
return p