توضیحات این پودمان می‌تواند در پودمان:AlborzSports/توضیحات قرار گیرد.

--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Version 3.0
--------------------------------------------------------------------------------

local p = {}

--------------------------------------------------------------------------------
-- LOAD DATA
--------------------------------------------------------------------------------

local data = mw.loadData("Module:AlborzSports/Data")


--------------------------------------------------------------------------------
-- CONFIGURATION
--------------------------------------------------------------------------------

local INFOBOX_CLASS =
    data.meta.class
    or "infobox sport-infobox"


local DEFAULT_IMAGE_WIDTH =
    data.defaults.image_width
    or data.meta.imageWidth
    or "250px"


--------------------------------------------------------------------------------
-- HELPERS
--------------------------------------------------------------------------------

local function clean(value)

    if not value then
        return nil
    end


    value =
        mw.text.trim(
            tostring(value)
        )


    if value == "" then
        return nil
    end


    return value

end


--------------------------------------------------------------------------------
-- ARGUMENT SOURCE
--------------------------------------------------------------------------------

local function getArgsFrame(frame)

    local parent =
        frame:getParent()


    if parent
    and next(parent.args) then

        return parent

    end


    return frame

end


--------------------------------------------------------------------------------
-- NORMALIZE ARGUMENTS
--------------------------------------------------------------------------------

local function normalizeArgs(frame)

    frame =
        getArgsFrame(frame)


    local args = {}


    for key, value in pairs(frame.args) do


        local cleanKey =
            mw.text.trim(
                tostring(key)
            )


        local cleanValue =
            clean(value)


        if cleanValue then


            local field =
                data.lookup[cleanKey]
                or cleanKey


            args[field] =
                cleanValue


        end

    end


    return args

end


--------------------------------------------------------------------------------
-- ENTITY DETECTION
--------------------------------------------------------------------------------

local function detectEntity(args)


    if args.entity_type
    and data.entities[args.entity_type] then

        return args.entity_type

    end


    return
        data.defaults.entity_type
        or "player"

end


--------------------------------------------------------------------------------
-- GET ENTITY
--------------------------------------------------------------------------------

local function getEntity(entityType)


    if data.entities[entityType] then

        return data.entities[entityType]

    end


    return
        data.entities.player

end


--------------------------------------------------------------------------------
-- GET VALUE
--------------------------------------------------------------------------------

local function getValue(args, field)

    local value =
        args[field]


    if value then

        return value

    end


    return nil

end


--------------------------------------------------------------------------------
-- LABEL
--------------------------------------------------------------------------------

local function getLabel(field)


    if data.fieldDefinitions
    and data.fieldDefinitions[field]
    and data.fieldDefinitions[field].label then


        return
            data.fieldDefinitions[field].label

    end


    return field

end


--------------------------------------------------------------------------------
-- TITLE
--------------------------------------------------------------------------------

local function renderTitle(box, entityType, args)


    local entity =
        getEntity(entityType)


    local title =
        args.name
        or entity.title
        or data.meta.title



    box
        :tag("tr")
        :tag("th")
        :attr("colspan","2")
        :addClass("infobox-title")
        :wikitext(title)


end


--------------------------------------------------------------------------------
-- IMAGE
--------------------------------------------------------------------------------

local function renderImage(box,args)


    if not args.image then

        return

    end


    local width =
        args.image_width
        or DEFAULT_IMAGE_WIDTH



    local image =
        mw.html.create("div")
        :addClass("infobox-image")



    image
        :wikitext(
            string.format(
                "[[پرونده:%s|%s]]",
                args.image,
                width
            )
        )


    if args.caption then


        image
            :tag("div")
            :addClass("infobox-caption")
            :wikitext(args.caption)


    end



    box
        :tag("tr")
        :tag("td")
        :attr("colspan","2")
        :node(image)


end


--------------------------------------------------------------------------------
-- SECTION HEADER
--------------------------------------------------------------------------------

local function renderSectionHeader(box,layout)


    local title =
        layout



    box
        :tag("tr")
        :tag("th")
        :attr("colspan","2")
        :addClass("infobox-header")
        :wikitext(title)


end


--------------------------------------------------------------------------------
-- FIELD RENDERER
--------------------------------------------------------------------------------

local function renderField(box,field,value)


    local row =
        box
        :tag("tr")



    row
        :tag("th")
        :wikitext(
            getLabel(field)
        )



    row
        :tag("td")
        :wikitext(value)


end


--------------------------------------------------------------------------------
-- SPECIAL FIELDS
--------------------------------------------------------------------------------

local function renderSpecialField(box,field,value)


    if field == "website" then


        value =
            string.format(
                "[%s وبگاه]",
                value
            )


    elseif field == "instagram" then


        value =
            string.format(
                "[%s اینستاگرام]",
                value
            )


    elseif field == "telegram" then


        value =
            string.format(
                "[%s تلگرام]",
                value
            )


    end



    renderField(
        box,
        field,
        value
    )

end
--------------------------------------------------------------------------------
-- LAYOUT PROCESSOR
--------------------------------------------------------------------------------

local function renderLayout(box,args,layout)


    local fields =
        data.layouts[layout]


    if not fields then

        return

    end



    local rows = {}



    for _, item in ipairs(fields) do


        local label =
            item[1]


        local field =
            item[2]


        local value =
            getValue(
                args,
                field
            )


        if value then


            table.insert(
                rows,
                {
                    label = label,
                    field = field,
                    value = value
                }
            )


        end


    end



    if #rows == 0 then

        return

    end



    renderSectionHeader(
        box,
        layout
    )



    for _, row in ipairs(rows) do


        renderSpecialField(
            box,
            row.field,
            row.value
        )


    end


end



--------------------------------------------------------------------------------
-- ENTITY RENDERER
--------------------------------------------------------------------------------

local function renderEntity(box,args,entityType)


    local entity =
        getEntity(entityType)



    if not entity then

        return

    end



    for _, layout in ipairs(entity.layouts) do


        renderLayout(
            box,
            args,
            layout
        )


    end


end



--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------

local function buildInfobox(args)


    local entityType =
        detectEntity(args)



    local box =
        mw.html.create("table")



    box
        :addClass(
            INFOBOX_CLASS
        )



    renderTitle(
        box,
        entityType,
        args
    )



    renderImage(
        box,
        args
    )



    renderEntity(
        box,
        args,
        entityType
    )



    return
        tostring(box)


end



--------------------------------------------------------------------------------
-- MAIN
--------------------------------------------------------------------------------

function p.main(frame)


    local args =
        normalizeArgs(frame)



    return
        buildInfobox(args)


end



--------------------------------------------------------------------------------
-- INVOKE
--------------------------------------------------------------------------------

function p.invoke(frame)


    return
        p.main(frame)


end



--------------------------------------------------------------------------------
-- RENDER
--------------------------------------------------------------------------------

function p.render(frame)


    return
        p.main(frame)


end



--------------------------------------------------------------------------------
-- TEST
--------------------------------------------------------------------------------

function p.test(frame)


    local args = {


        name =
            "علیرضا جهانبخش",


        entity_type =
            "player",


        nationality =
            "ایرانی",


        country =
            "ایران",


        sport =
            "فوتبال",


        discipline =
            "فوتبال",


        position =
            "هافبک",


        current_club =
            "هیرنفین",


        birth_date =
            "۱۱ مرداد ۱۳۷۲",


        birth_place =
            "جیرنده"


    }



    return
        buildInfobox(args)


end



--------------------------------------------------------------------------------
-- EXPORT
--------------------------------------------------------------------------------

return p