پرش به محتوا

پودمان:AlborzEducation: تفاوت میان نسخه‌ها

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۷: خط ۷:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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("Module:AlborzEducation/Data")
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- 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
خط ۳۶: خط ۳۲:
     return value ~= "" and value or nil
     return value ~= "" and value or nil
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ARGUMENT HANDLING
-- ARGUMENT HANDLING
خط ۴۳: خط ۳۸:
     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
خط ۵۱: خط ۴۶:
     return args
     return args
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- NORMALIZE PARAMETERS
-- NORMALIZE PARAMETERS
خط ۶۲: خط ۵۶:
     return result
     return result
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD VALUE GETTER
-- FIELD VALUE GETTER
خط ۷۴: خط ۶۷:
     return definition and definition.default or nil
     return definition and definition.default or nil
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD RENDERING
-- FIELD RENDERING
خط ۸۳: خط ۷۵:
         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
خط ۹۶: خط ۸۶:
                 :done()
                 :done()
     end
     end
     -- Boolean fields
     -- Boolean fields
     if fieldType == "boolean" then
     if fieldType == "boolean" then
خط ۱۰۵: خط ۹۴:
         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("[%s %s]", value, value)
     end
     end
     -- Normal text fields
     -- Normal text fields
     local row = mw.html.create("tr")
     local row = mw.html.create("tr")
خط ۱۱۷: خط ۱۰۴:
     return row
     return row
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INFOBOX TITLE
-- INFOBOX TITLE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function renderTitle(args)
local function renderTitle(args)
     local title = getFieldValue(args, "name")
     local title = getFieldValue(args, "fullname") or getFieldValue(args, "name")
     if not title then
     if not title then
         return nil
         return nil
     end
     end
     return mw.html.create("tr")
     return mw.html.create("tr")
         :tag("th")
         :tag("th")
             :attr("colspan", "2")
             :attr("colspan", "2")
             :addClass("education-infobox-title")
             :addClass("education-infobox-title")
            :css({
                ["text-align"] = "center",
                ["font-size"] = "1.15em",
                ["font-weight"] = "bold",
                ["padding"] = "8px 6px"
            })
             :wikitext(title)
             :wikitext(title)
             :done()
             :done()
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE RENDERING
-- IMAGE RENDERING
خط ۱۴۳: خط ۱۳۳:
         return nil
         return nil
     end
     end
     return mw.html.create("tr")
     return mw.html.create("tr")
         :tag("td")
         :tag("td")
خط ۱۵۱: خط ۱۴۰:
             :done()
             :done()
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GROUP RENDERING
-- GROUP RENDERING
خط ۱۶۰: خط ۱۴۸:
         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
خط ۱۷۲: خط ۱۵۹:
         return
         return
     end
     end
 
     -- Group header (وسط‌چین)
     -- Group header
     box:tag("tr")
     box:tag("tr")
         :tag("th")
         :tag("th")
             :attr("colspan", "2")
             :attr("colspan", "2")
             :addClass("education-infobox-group")
             :addClass("education-infobox-group")
            :css({
                ["text-align"] = "center",
                ["font-weight"] = "bold",
                ["padding"] = "6px"
            })
             :wikitext((group.icon or "") .. " " .. group.label)
             :wikitext((group.icon or "") .. " " .. group.label)
     -- Fields
     -- Fields
     for _, fieldName in ipairs(fieldList) do
     for _, fieldName in ipairs(fieldList) do
خط ۱۹۱: خط ۱۸۱:
     end
     end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LAYOUT RENDERING
-- LAYOUT RENDERING
خط ۲۰۰: خط ۱۸۹:
         return nil
         return nil
     end
     end
     local box = mw.html.create("table"):addClass(INFOBOX_CLASS)
     local box = mw.html.create("table"):addClass(INFOBOX_CLASS)
     -- Header
     -- Header
     local title = renderTitle(args)
     local title = renderTitle(args)
خط ۲۰۸: خط ۱۹۵:
         box:node(title)
         box:node(title)
     end
     end
     local image = renderImage(args)
     local image = renderImage(args)
     if image then
     if image then
         box:node(image)
         box:node(image)
     end
     end
     -- Content Groups
     -- Content Groups
     for _, section in ipairs(layout) do
     for _, section in ipairs(layout) do
         renderGroup(box, section.group, section.fields, args)
         renderGroup(box, section.group, section.fields, args)
     end
     end
     return box
     return box
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- MAIN FUNCTION
-- MAIN FUNCTION
خط ۲۳۰: خط ۲۱۳:
     return result and tostring(result) or ""
     return result and tostring(result) or ""
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, "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