پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۱۲: خط ۱۲:


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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۱۷: خط ۱۸:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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


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




خط ۳۵: خط ۳۹:
     end
     end


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


     if value == "" then
     if value == "" then
         return nil
         return nil
     end
     end


     return value
     return value
خط ۴۷: خط ۵۷:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- ARGUMENT NORMALIZATION
-- 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)
local function normalizeArgs(frame)
    frame =
        getArgsFrame(frame)


     local args = {}
     local args = {}


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


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


         local cleanValue = clean(value)
        local cleanKey =
            mw.text.trim(
                tostring(key)
            )
 
 
         local cleanValue =
            clean(value)
 


         if cleanValue then
         if cleanValue then


             local field =
             local field =
                 data.lookup[cleanKey] or
                 data.lookup[cleanKey]
                 cleanKey
                or cleanKey
 
 
            args[field] =
                 cleanValue


            args[field] = cleanValue


         end
         end


     end
     end


     return args
     return args
خط ۸۳: خط ۱۳۳:
local function detectEntity(args)
local function detectEntity(args)


    local entity = args.entity_type


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


    if entity then
         return args.entity_type
 
         if data.entities[entity] then
            return entity
        end


     end
     end




     local typeValue = args.type
     return
 
         data.defaults.entity_type
 
        or "player"
    if typeValue then
 
        for key, value in pairs(data.lookup) do
 
            if key == typeValue then
 
                if data.entities[value] then
                    return value
                end
 
            end
 
         end
 
    end
 
 
    return data.defaults.entity_type or "player"


end
end
خط ۱۲۱: خط ۱۵۰:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GET ENTITY DATA
-- GET ENTITY
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function getEntity(entityType)
local function getEntity(entityType)


     if data.entities[entityType] then
     if data.entities[entityType] then
خط ۱۳۳: خط ۱۶۳:




     return data.entities.player
     return
        data.entities.player


end
end
خط ۱۳۹: خط ۱۷۰:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD VALUE
-- GET VALUE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function getValue(args, field)
local function getValue(args, field)


     local value = args[field]
     local value =
        args[field]




خط ۱۶۰: خط ۱۹۲:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD LABEL
-- LABEL
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function getLabel(field)
local function getLabel(field)


    if data.fieldDefinitions[field] then


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


     end
     end
خط ۱۷۵: خط ۲۱۲:


end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TITLE RENDERER
-- TITLE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderTitle(box, entityType, args)
local function renderTitle(box, entityType, args)
    local entity =
        getEntity(entityType)


     local title =
     local title =
         args.name or
         args.name
         data.entities[entityType].title or
         or entity.title
         data.meta.title
         or data.meta.title
 




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


end
end
خط ۱۹۷: خط ۲۴۴:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE RENDERER
-- IMAGE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderImage(box, args)
local function renderImage(box,args)


    local image = args.image


    if not args.image then


    if not image then
         return
         return
     end
     end




     local width =
     local width =
         args.image_width or
         args.image_width
         DEFAULT_IMAGE_WIDTH
         or DEFAULT_IMAGE_WIDTH




    local imageHtml =
        mw.getCurrentFrame():extensionTag(
            "imagemap",
            ""
        )
    local file =


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




     file
 
     image
         :wikitext(
         :wikitext(
             string.format(
             string.format(
                 "[[پرونده:%s|%s]]",
                 "[[پرونده:%s|%s]]",
                 image,
                 args.image,
                 width
                 width
             )
             )
خط ۲۴۱: خط ۲۸۱:
     if args.caption then
     if args.caption then


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


     end
     end




خط ۲۵۲: خط ۲۹۵:
         :tag("tr")
         :tag("tr")
         :tag("td")
         :tag("td")
         :attr("colspan", "2")
         :attr("colspan","2")
         :node(file)
         :node(image)
 


end
end
خط ۲۵۹: خط ۳۰۳:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- SECTION HEADER RENDERER
-- SECTION HEADER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderSectionHeader(box, layout)
local function renderSectionHeader(box,layout)
 


     local title =
     local title =
         data.layoutTitles[layout]
         layout


    if not title then
        return
    end




خط ۲۷۶: خط ۳۱۷:
         :tag("tr")
         :tag("tr")
         :tag("th")
         :tag("th")
         :attr("colspan", "2")
         :attr("colspan","2")
         :addClass("infobox-header")
         :addClass("infobox-header")
         :wikitext(title)
         :wikitext(title)


end
end
خط ۲۸۷: خط ۳۲۹:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderField(box, field, value)
local function renderField(box,field,value)
 
 
    if not value then
        return
    end
 
 
    local label =
        getLabel(field)




     local row =
     local row =
         box
         box
            :tag("tr")
        :tag("tr")
 




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




خط ۳۱۲: خط ۳۴۹:
         :tag("td")
         :tag("td")
         :wikitext(value)
         :wikitext(value)


end
end
خط ۳۱۷: خط ۳۵۵:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- SPECIAL FIELD RENDERER
-- SPECIAL FIELDS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderSpecialField(box, field, value)
local function renderSpecialField(box,field,value)




خط ۳۲۸: خط ۳۶۶:
         value =
         value =
             string.format(
             string.format(
                 "[%s %s]",
                 "[%s وبگاه]",
                 value,
                 value
                "وبگاه"
             )
             )


خط ۳۵۴: خط ۳۹۱:




     elseif field == "coordinates" then
     end
 
 
        value =
            string.format(
                "{{مختصات|%s}}",
                value
            )
 


    end




خط ۳۷۸: خط ۴۰۶:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderLayout(box, args, layout)
local function renderLayout(box,args,layout)




خط ۳۸۶: خط ۴۱۴:


     if not fields then
     if not fields then
         return
         return
     end
     end




    local hasValue = false


    local rows = {}
    for _, item in ipairs(fields) do


    for _, field in ipairs(fields) do
 
        local label =
            item[1]
 
 
        local field =
            item[2]




خط ۴۰۵: خط ۴۴۵:
         if value then
         if value then


            hasValue = true


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


         end
         end


     end
     end




     if not hasValue then
 
     if #rows == 0 then


         return
         return


     end
     end




خط ۴۲۷: خط ۴۷۷:




    for _, field in ipairs(fields) do


    for _, row in ipairs(rows) do


        local value =
            getValue(
                args,
                field
            )
        if value then
            renderSpecialField(
                box,
                field,
                value
            )


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


        end


     end
     end
خط ۴۵۳: خط ۴۹۲:


end
end




خط ۴۵۹: خط ۴۹۹:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function renderEntity(box, args, entityType)
local function renderEntity(box,args,entityType)




     local entity =
     local entity =
         getEntity(entityType)
         getEntity(entityType)




خط ۴۷۱: خط ۵۱۲:


     end
     end




خط ۴۸۷: خط ۵۲۹:


end
end




خط ۴۹۸: خط ۵۴۱:
     local entityType =
     local entityType =
         detectEntity(args)
         detectEntity(args)




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




خط ۵۰۸: خط ۵۵۳:
             INFOBOX_CLASS
             INFOBOX_CLASS
         )
         )




خط ۵۱۵: خط ۵۶۱:
         args
         args
     )
     )




خط ۵۲۱: خط ۵۶۸:
         args
         args
     )
     )




خط ۵۳۰: خط ۵۷۸:




     return tostring(box)
 
     return
        tostring(box)
 


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- MAIN FUNCTION
-- MAIN
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


خط ۵۴۳: خط ۵۹۵:


     local args =
     local args =
         normalizeArgs(
         normalizeArgs(frame)
            frame:getParent()
 
        )
 


    return
        buildInfobox(args)


    return buildInfobox(
        args
    )


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INVOKE SUPPORT
-- INVOKE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function p.infobox(frame)
function p.invoke(frame)


    return p.main(frame)


end
    return
--------------------------------------------------------------------------------
        p.main(frame)
-- TEMPLESTYLE SUPPORT
--------------------------------------------------------------------------------


local function addStyles()


    local frame =
end
        mw.getCurrentFrame()


    return frame:extensionTag(
        "templatestyles",
        "",
        {
            src = "الگو:جعبه اطلاعات ورزش/styles.css"
        }
    )


end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FINAL WRAPPER
-- RENDER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function finalize(html)
function p.render(frame)
 


     return
     return
        p.main(frame)


        addStyles()


        .. html
end


end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- PUBLIC RENDER FUNCTION
-- TEST
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function p.render(frame)
function p.test(frame)




     local args =
     local args = {
        normalizeArgs(
            frame:getParent()
        )




    local output =
        name =
        buildInfobox(
             "علیرضا جهانبخش",
             args
        )




    return finalize(output)
        entity_type =
            "player",


end


        nationality =
            "ایرانی",


--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
--------------------------------------------------------------------------------


function p.invoke(frame)
        country =
            "ایران",


    return p.render(frame)


end
        sport =
            "فوتبال",




--------------------------------------------------------------------------------
        discipline =
-- TEST SUPPORT
            "فوتبال",
--------------------------------------------------------------------------------


function p.test(frame)


    local args = {
        position =
            "هافبک",


        name = "نمونه ورزشکار",


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


        nationality = "ایرانی",


         sport = "فوتبال",
         birth_date =
            "۱۱ مرداد ۱۳۷۲",


        discipline = "فوتبال",


         position = "مهاجم",
         birth_place =
            "جیرنده"


        current_club = "باشگاه نمونه",


        birth_date = "۱۳۷۰",
    }


        residence = "کرج"


    }


    return
        buildInfobox(args)


    return buildInfobox(args)


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- RETURN MODULE
-- EXPORT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


return p
return p

نسخهٔ ‏۴ اوت ۲۰۲۶، ساعت ۱۱:۵۵

توضیحات این پودمان می‌تواند در پودمان: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