پرش به محتوا

پودمان: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 = 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
--------------------------------------------------------------------------------

local function renderGroup(groupName, fieldList, args)


    local group = groups[groupName]


    if not group then
        return nil
    end



    local sectionTable = mw.html.create("table")
        :addClass("infobox-section")



 

    local hasValue = false



    ----------------------------------------------------------------------------
    -- Fields
    ----------------------------------------------------------------------------

    for _, fieldName in ipairs(fieldList) do


        local value = getFieldValue(
            args,
            fieldName
        )



        if value then


            local row = renderField(
                fieldName,
                value
            )



            if row then

          sectionTable
              :node(row)

                hasValue = true

            end

        end


    end




    if not hasValue then
        return nil
    end




    ----------------------------------------------------------------------------
    -- Group Header
    ----------------------------------------------------------------------------

    local header = sectionTable
        :tag("tr")



    header
        :tag("th")
        :attr("colspan", "2")
        :wikitext(
            (group.icon or "")
            .. " "
            .. group.label
        )
        :done()






    return sectionTable

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


        local groupHtml = renderGroup(
            section.group,
            section.fields,
            args
        )



        if groupHtml then

            box
                :node(groupHtml)

        end


    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