پودمان:AlborzEducation

نسخهٔ تاریخ ‏۲۶ ژوئیهٔ ۲۰۲۶، ساعت ۲۰:۴۷ توسط Modir (بحث | مشارکت‌ها)

توضیحات این پودمان می‌تواند در پودمان: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, "fullname") or getFieldValue(args, "name")
    if not title then
        return nil
    end
    return mw.html.create("tr")
        :tag("th")
            :attr("colspan", "2")
            :addClass("education-infobox-title")
            :css({
                ["text-align"] = "center",
                ["font-size"] = "1.15em",
                ["font-weight"] = "bold",
                ["padding"] = "8px 6px"
            })
            :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")
            :css({
                ["text-align"] = "center",
                ["font-weight"] = "bold",
                ["padding"] = "6px"
            })
            :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