پودمان:AlborzEducation: تفاوت میان نسخهها
ظاهر
جزبدون خلاصۀ ویرایش |
جزبدون خلاصۀ ویرایش |
||
| خط ۳۵۹: | خط ۳۵۹: | ||
-------------------------------------------------------------------------------- | |||
-- GROUP RENDERING | |||
-------------------------------------------------------------------------------- | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| خط ۳۷۳: | خط ۳۷۷: | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- Check whether the group has at least one visible field | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
| خط ۳۹۰: | خط ۳۹۴: | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- Group header | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
box | local header = box:tag("tr") | ||
header | |||
:tag("th") | |||
:attr("colspan", "2") | |||
:addClass("education-infobox-group") | |||
:wikitext((group.icon or "") .. " " .. group.label) | |||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
-- | -- Fields | ||
-------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ||
| خط ۴۲۱: | خط ۴۲۴: | ||
end | end | ||
end | end | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- LAYOUT RENDERING | -- LAYOUT RENDERING | ||
نسخهٔ ۲۶ ژوئیهٔ ۲۰۲۶، ساعت ۰۲:۲۱
توضیحات این پودمان میتواند در پودمان:AlborzEducation/توضیحات قرار گیرد.
--------------------------------------------------------------------------------
-- Module:AlborzEducation
-- Wiki Alborz Education Infobox Engine
--
-- Rendering engine for education information boxes.
-- Configuration is stored in Module:AlborzEducation/Data
--------------------------------------------------------------------------------
local p = {}
--------------------------------------------------------------------------------
-- CSS CLASSES
--------------------------------------------------------------------------------
local INFOBOX_CLASS = "infobox education-infobox"
--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------
local data = mw.loadData(
"Module:AlborzEducation/Data"
)
--------------------------------------------------------------------------------
-- LOCAL REFERENCES
--------------------------------------------------------------------------------
local lookup = data.lookup
local fields = data.fieldDefinitions
local layouts = data.layouts
local groups = data.groups
--------------------------------------------------------------------------------
-- BASIC UTILITIES
--------------------------------------------------------------------------------
local function trim(value)
if not value then
return nil
end
value = tostring(value)
value = mw.text.trim(value)
if value == "" then
return nil
end
return value
end
--------------------------------------------------------------------------------
-- ARGUMENT HANDLING
--------------------------------------------------------------------------------
local function getArgs(frame)
local args = {}
for key, value in pairs(frame.args) do
key = trim(key)
value = trim(value)
if key and value then
args[key] = value
end
end
return args
end
--------------------------------------------------------------------------------
-- NORMALIZE PARAMETERS
--------------------------------------------------------------------------------
local function normalizeArgs(args)
local result = {}
for key, value in pairs(args) do
local internalName = lookup[key]
if internalName then
result[internalName] = value
else
-- allow direct internal field names
result[key] = value
end
end
return result
end
--------------------------------------------------------------------------------
-- FIELD VALUE GETTER
--------------------------------------------------------------------------------
local function getFieldValue(args, fieldName)
local value = args[fieldName]
if value then
return value
end
local definition = fields[fieldName]
if definition and definition.default then
return definition.default
end
return nil
end
--------------------------------------------------------------------------------
-- FIELD RENDERING
--------------------------------------------------------------------------------
local function renderField(fieldName, value)
local field = fields[fieldName]
if not field then
return nil
end
if not value then
return nil
end
local label = field.label
local fieldType = field.type
----------------------------------------------------------------------------
-- Image fields
----------------------------------------------------------------------------
if fieldType == "image" then
local row = mw.html.create("tr")
row
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:wikitext(
string.format(
"[[پرونده:%s|250px|center]]",
value
)
)
:done()
return row
end
----------------------------------------------------------------------------
-- Boolean fields
----------------------------------------------------------------------------
if fieldType == "boolean" then
if value == "yes"
or value == "بله"
or value == "true"
or value == "1"
then
value = "دارد"
else
value = "ندارد"
end
end
----------------------------------------------------------------------------
-- URL fields
----------------------------------------------------------------------------
if fieldType == "url" then
value = string.format(
"[%s %s]",
value,
value
)
end
----------------------------------------------------------------------------
-- Normal text fields
----------------------------------------------------------------------------
local row = mw.html.create("tr")
row
:tag("th")
:wikitext(label)
:done()
row
:tag("td")
:wikitext(value)
:done()
return row
end
--------------------------------------------------------------------------------
-- INFOBOX TITLE
--------------------------------------------------------------------------------
local function renderTitle(args)
local title = getFieldValue(
args,
"name"
)
if not title then
return nil
end
local row = mw.html.create("tr")
row
:tag("th")
:attr("colspan", "2")
:addClass("education-infobox-title")
:wikitext(title)
:done()
return row
end
--------------------------------------------------------------------------------
-- IMAGE RENDERING
--------------------------------------------------------------------------------
local function renderImage(args)
local image = getFieldValue(
args,
"image"
)
if not image then
return nil
end
local row = mw.html.create("tr")
row
:tag("td")
:attr("colspan", "2")
:addClass("education-infobox-image")
:wikitext(
string.format(
"[[پرونده:%s|250px|center]]",
image
)
)
:done()
return row
end
--------------------------------------------------------------------------------
-- GROUP RENDERING
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GROUP RENDERING
--------------------------------------------------------------------------------
local function renderGroup(box, groupName, fieldList, args)
local group = groups[groupName]
if not group then
return
end
--------------------------------------------------------------------------
-- Check whether the group has at least one visible field
--------------------------------------------------------------------------
local hasValue = false
for _, fieldName in ipairs(fieldList) do
if getFieldValue(args, fieldName) then
hasValue = true
break
end
end
if not hasValue then
return
end
--------------------------------------------------------------------------
-- Group header
--------------------------------------------------------------------------
local header = box:tag("tr")
header
:tag("th")
:attr("colspan", "2")
:addClass("education-infobox-group")
:wikitext((group.icon or "") .. " " .. group.label)
--------------------------------------------------------------------------
-- Fields
--------------------------------------------------------------------------
for _, fieldName in ipairs(fieldList) do
local value = getFieldValue(args, fieldName)
if value then
local row = renderField(fieldName, value)
if row then
box:node(row)
end
end
end
end
--------------------------------------------------------------------------------
-- LAYOUT RENDERING
--------------------------------------------------------------------------------
local function renderLayout(layoutName, args)
local layout = layouts[layoutName]
if not layout then
return nil
end
local box = mw.html.create("table")
:addClass(INFOBOX_CLASS)
----------------------------------------------------------------------------
-- Header
----------------------------------------------------------------------------
local title = renderTitle(args)
if title then
box
:node(title)
end
local image = renderImage(args)
if image then
box
:node(image)
end
----------------------------------------------------------------------------
-- Content Groups
----------------------------------------------------------------------------
for _, section in ipairs(layout) do
renderGroup(
box,
section.group,
section.fields,
args
)
end
return box
end
--------------------------------------------------------------------------------
-- MAIN FUNCTION
--------------------------------------------------------------------------------
function p.main(frame)
local rawArgs = getArgs(frame)
local args = normalizeArgs(
rawArgs
)
local layoutName = "school"
local result = renderLayout(
layoutName,
args
)
if result then
return tostring(result)
end
return ""
end
--------------------------------------------------------------------------------
-- RETURN MODULE
--------------------------------------------------------------------------------
return p