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