پرش به محتوا

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

از ویکی البرز
جزبدون خلاصۀ ویرایش
جزبدون خلاصۀ ویرایش
خط ۲: خط ۲:
-- Module:AlborzSports
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Wiki Alborz Sports Information System
-- Core Engine
-- Version 3.0
-- Version 2.1
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


خط ۱۵: خط ۱۴:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- LOAD DEPENDENCIES
-- CONFIGURATION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local infobox = require("Module:Infobox")
local INFOBOX_CLASS = data.meta.class or "infobox sport-infobox"


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




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
-- HELPERS
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function clean(value)
local function clean(value)


     if value == nil then
     if not value then
         return nil
         return nil
     end
     end


     if type(value) == "string" then
     value = mw.text.trim(value)
 
        value = mw.text.trim(value)
 
        if value == "" then
            return nil
        end


    if value == "" then
        return nil
     end
     end


خط ۵۳: خط ۴۷:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GET ARGUMENTS
-- ARGUMENT NORMALIZATION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function getArgs(frame)
local function normalizeArgs(frame)


     local args = {}
     local args = {}


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


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


         local value = clean(v)
         local cleanValue = clean(value)


         if value then
         if cleanValue then
 
            local field =
                data.lookup[cleanKey] or
                cleanKey


             args[key] = value
             args[field] = cleanValue


         end
         end
خط ۸۰: خط ۷۸:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- NORMALIZE INPUT
-- ENTITY DETECTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function normalize(args)
local function detectEntity(args)


     local result = {}
     local entity = args.entity_type


    for key,value in pairs(args) do


        local field = data.lookup[key] or key
    if entity then


         result[field] = value
         if data.entities[entity] then
            return entity
        end


     end
     end


    return result


end
    local typeValue = args.type
 
 
    if typeValue then
 
        for key, value in pairs(data.lookup) do


            if key == typeValue then


--------------------------------------------------------------------------------
                if data.entities[value] then
-- ENTITY DETECTION
                    return value
--------------------------------------------------------------------------------
                end


local function getEntity(args)
            end


    local entity = args.entity_type
        end


    if not entity then
        entity = data.defaults.entity_type
     end
     end


    if data.entities[entity] then
        return entity
    end


     return "player"
     return data.defaults.entity_type or "player"


end
end
خط ۱۲۲: خط ۱۲۱:


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


local function getTitle(entity,args)
local function getEntity(entityType)
 
    if data.entities[entityType] then
 
        return data.entities[entityType]


    if args.name then
        return args.name
     end
     end


    if data.entities[entity] then
        return data.entities[entity].title
    end


     return data.meta.title
     return data.entities.player


end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE HANDLING
-- FIELD VALUE
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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


     if not args.image then
     local value = args[field]
        return nil
    end


    local width = args.image_width or DEFAULT_IMAGE_WIDTH


     local caption = args.caption or ""
     if value then


    local image = {
        return value


        type = "image",
    end
 
        image = args.image,
 
        size = width,


        caption = caption
    }


     return image
     return nil


end
end
خط ۱۷۰: خط ۱۶۰:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- FIELD BUILDER
-- FIELD LABEL
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function buildFields(layouts,args)
local function getLabel(field)


     local fields = {}
     if data.fieldDefinitions[field] then


    for _,layout in ipairs(layouts) do
        return data.fieldDefinitions[field].label


        local label = layout[1]
    end


        local key = layout[2]


        local value = args[key]
    return field


        value = clean(value)
end
--------------------------------------------------------------------------------
-- TITLE RENDERER
--------------------------------------------------------------------------------


        if value then
local function renderTitle(box, entityType, args)


            table.insert(fields,{
    local title =
        args.name or
        data.entities[entityType].title or
        data.meta.title


                label = label,


                value = value
    box
 
        :tag("th")
            })
        :attr("colspan", "2")
 
        :addClass("infobox-title")
         end
         :wikitext(title)
 
    end
 
    return fields


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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GROUP BUILDER
-- IMAGE RENDERER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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


     local groups = {}
     local image = args.image


    local entityData = data.entities[entity]


     if not entityData then
     if not image then
         return groups
         return
     end
     end




     for _,layoutName in ipairs(entityData.layouts) do
     local width =
        args.image_width or
        DEFAULT_IMAGE_WIDTH




        local layout = data.layouts[layoutName]
    local imageHtml =


        mw.getCurrentFrame():extensionTag(
            "imagemap",
            ""
        )


        if layout then


    local file =


            local fields = buildFields(layout,args)
        mw.html.create("div")
        :addClass("infobox-image")




            if #fields > 0 then
    file
 
        :wikitext(
 
            string.format(
          table.insert(groups,{
                "[[پرونده:%s|%s]]",
 
                image,
          name = data.layoutTitles[layoutName] or layoutName,
                width
 
            )
          fields = fields
        )
 
              })
 
            end




        end
    if args.caption then


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


     end
     end




     return groups
     box
        :tag("tr")
        :tag("td")
        :attr("colspan", "2")
        :node(file)


end
end
خط ۲۵۹: خط ۲۵۹:


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
-- SECTION HEADER RENDERER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function buildInfobox(entity,args)
local function renderSectionHeader(box, layout)


    local title =
        data.layoutTitles[layout]


    local box = {}


    if not title then
        return
    end


    box.title = getTitle(entity,args)


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


    local image = buildImage(args)
end




    if image then
--------------------------------------------------------------------------------
-- FIELD RENDERER
--------------------------------------------------------------------------------


        box.image = image
local function renderField(box, field, value)


    if not value then
        return
     end
     end




    local label =
        getLabel(field)


    box.groups = buildGroups(entity,args)


    local row =
        box
            :tag("tr")




     return box
     row
        :tag("th")
        :wikitext(label)
 
 
    row
        :tag("td")
        :wikitext(value)


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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- RENDER TEXT
-- SPECIAL FIELD RENDERER
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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




     if type(value) == "table" then
     if field == "website" then


        return table.concat(value,"، ")


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




     return tostring(value)
     elseif field == "instagram" then


end
--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------


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


    local result = {}


     table.insert(result, '<table class="infobox sport-infobox">')
     elseif field == "telegram" then




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


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


     if box.title then
     elseif field == "coordinates" then


        table.insert(result,


             '<tr class="infobox-title">' ..
        value =
            '<th colspan="2">' ..
             string.format(
            box.title ..
                "{{مختصات|%s}}",
             '</th></tr>'
                value
             )


        )


     end
     end




    renderField(
        box,
        field,
        value
    )


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


    if box.image then
local function renderLayout(box, args, layout)




        local image = box.image
    local fields =
        data.layouts[layout]




         local file = image.image
    if not fields then
         return
    end




        local size = image.size or DEFAULT_IMAGE_WIDTH
    local hasValue = false




        local caption = image.caption or ""
    for _, field in ipairs(fields) do




         table.insert(result,
         local value =
            getValue(
                args,
                field
            )
 


            '<tr>' ..
        if value then
            '<td colspan="2" class="infobox-image">' ..


             '[[File:' ..
             hasValue = true
            file ..
            '|' ..
            size ..
            ']]'


            break


         )
         end


    end


        if caption ~= "" then


    if not hasValue then


            table.insert(result,
        return


                '<br /><small>' ..
    end
                caption ..
                '</small>'


            )


    renderSectionHeader(
        box,
        layout
    )


        end


    for _, field in ipairs(fields) do


        table.insert(result,


             '</td></tr>'
        local value =
             getValue(
                args,
                field
            )


        )


        if value then


    end


            renderSpecialField(
                box,
                field,
                value
            )




    ------------------------------------------------------------
        end
    -- GROUPS
    ------------------------------------------------------------


     for _,group in ipairs(box.groups) do
     end




end


        table.insert(result,


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


            '<tr class="infobox-header">' ..
local function renderEntity(box, args, entityType)


            '<th colspan="2">' ..


            group.name ..
    local entity =
        getEntity(entityType)


            '</th></tr>'


    if not entity then


         )
         return


    end




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




             table.insert(result,
        renderLayout(
             box,
            args,
            layout
        )




                '<tr>' ..
    end


                '<th>' ..
                field.label ..
                '</th>' ..


                '<td>' ..
end
                renderValue(field.value) ..
                '</td>' ..


                '</tr>'


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


            )
local function buildInfobox(args)




         end
    local entityType =
         detectEntity(args)




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




    box
        :addClass(
            INFOBOX_CLASS
        )


    ------------------------------------------------------------
    -- CLOSE
    ------------------------------------------------------------


     table.insert(result,
     renderTitle(
        box,
        entityType,
        args
    )


        '</table>'


    renderImage(
        box,
        args
     )
     )




     return table.concat(result,"\n")
     renderEntity(
        box,
        args,
        entityType
    )


    return tostring(box)


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
-- MAIN FUNCTION
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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




     local args = getArgs(frame)
     local args =
 
        normalizeArgs(
 
            frame:getParent()
    args = normalize(args)
        )
 
 
 
    local entity = getEntity(args)




 
     return buildInfobox(
     local box = buildInfobox(entity,args)
        args
 
     )
 
 
     return renderInfobox(box)
 


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- TEMPLATE ENTRY
-- INVOKE SUPPORT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


خط ۵۰۹: خط ۵۶۵:
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- DEBUG FUNCTION
-- TEMPLESTYLE SUPPORT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function p.test(frame)
local function addStyles()


     local args = getArgs(frame)
     local frame =
        mw.getCurrentFrame()


    args = normalize(args)


    return frame:extensionTag(
        "templatestyles",
        "",
        {
            src = "Module:AlborzSports/styles.css"
        }
    )


    local entity = getEntity(args)
end




    local entityData = data.entities[entity]
--------------------------------------------------------------------------------
-- FINAL WRAPPER
--------------------------------------------------------------------------------


local function finalize(html)


     if not entityData then
     return


         return "Entity not found"
         addStyles()


    end
        .. html


end


    local result = {}


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


function p.render(frame)


    table.insert(result,


         "Entity: " .. entity
    local args =
         normalizeArgs(
            frame:getParent()
        )


    )


    local output =
        buildInfobox(
            args
        )




     table.insert(result,
     return finalize(output)


        "Title: " .. entityData.title
end


    )


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


function p.invoke(frame)


    table.insert(result,
     return p.render(frame)
 
        "Layouts: " ..
 
        table.concat(entityData.layouts,", ")
 
    )
 
 
     return table.concat(result,"\n")


end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- GET ENTITY LIST
-- TEST SUPPORT
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


function p.entities()
function p.test(frame)


     local result = {}
     local args = {


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


    for key,value in pairs(data.entities) do
        entity_type = "player",


        nationality = "ایرانی",


         table.insert(result,
         sport = "فوتبال",


            key .. " : " .. value.title
        discipline = "فوتبال",


         )
         position = "مهاجم",


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


    end
        birth_date = "۱۳۷۰",


        residence = "کرج"


     return table.concat(result,"\n")
     }


end


 
    return buildInfobox(args)
 
--------------------------------------------------------------------------------
-- GET VERSION
--------------------------------------------------------------------------------
 
function p.version()
 
    return
 
        data.version.name ..
 
        " v" ..
 
        data.version.major ..
 
        "." ..
 
        data.version.minor


end
end




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


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(value)

    if value == "" then
        return nil
    end

    return value

end


--------------------------------------------------------------------------------
-- ARGUMENT NORMALIZATION
--------------------------------------------------------------------------------

local function normalizeArgs(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)

    local entity = args.entity_type


    if entity then

        if data.entities[entity] then
            return entity
        end

    end


    local typeValue = args.type


    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


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

local function getEntity(entityType)

    if data.entities[entityType] then

        return data.entities[entityType]

    end


    return data.entities.player

end


--------------------------------------------------------------------------------
-- FIELD VALUE
--------------------------------------------------------------------------------

local function getValue(args, field)

    local value = args[field]


    if value then

        return value

    end


    return nil

end


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

local function getLabel(field)

    if data.fieldDefinitions[field] then

        return data.fieldDefinitions[field].label

    end


    return field

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

local function renderTitle(box, entityType, args)

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


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

end


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

local function renderImage(box, args)

    local image = args.image


    if not image then
        return
    end


    local width =
        args.image_width or
        DEFAULT_IMAGE_WIDTH


    local imageHtml =

        mw.getCurrentFrame():extensionTag(
            "imagemap",
            ""
        )


    local file =

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


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


    if args.caption then

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

    end


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

end


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

local function renderSectionHeader(box, layout)

    local title =
        data.layoutTitles[layout]


    if not title then
        return
    end


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

end


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

local function renderField(box, field, value)


    if not value then
        return
    end


    local label =
        getLabel(field)


    local row =
        box
            :tag("tr")


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


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

end


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

local function renderSpecialField(box, field, value)


    if field == "website" then


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


    elseif field == "instagram" then


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


    elseif field == "telegram" then


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


    elseif field == "coordinates" 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 hasValue = false


    for _, field in ipairs(fields) do


        local value =
            getValue(
                args,
                field
            )


        if value then

            hasValue = true

            break

        end

    end


    if not hasValue then

        return

    end


    renderSectionHeader(
        box,
        layout
    )


    for _, field in ipairs(fields) do


        local value =
            getValue(
                args,
                field
            )


        if value then


            renderSpecialField(
                box,
                field,
                value
            )


        end

    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
--------------------------------------------------------------------------------

function p.main(frame)


    local args =
        normalizeArgs(
            frame:getParent()
        )


    return buildInfobox(
        args
    )

end


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

function p.infobox(frame)

    return p.main(frame)

end
--------------------------------------------------------------------------------
-- TEMPLESTYLE SUPPORT
--------------------------------------------------------------------------------

local function addStyles()

    local frame =
        mw.getCurrentFrame()


    return frame:extensionTag(
        "templatestyles",
        "",
        {
            src = "Module:AlborzSports/styles.css"
        }
    )

end


--------------------------------------------------------------------------------
-- FINAL WRAPPER
--------------------------------------------------------------------------------

local function finalize(html)

    return

        addStyles()

        .. html

end


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

function p.render(frame)


    local args =
        normalizeArgs(
            frame:getParent()
        )


    local output =
        buildInfobox(
            args
        )


    return finalize(output)

end


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

function p.invoke(frame)

    return p.render(frame)

end


--------------------------------------------------------------------------------
-- TEST SUPPORT
--------------------------------------------------------------------------------

function p.test(frame)

    local args = {

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

        entity_type = "player",

        nationality = "ایرانی",

        sport = "فوتبال",

        discipline = "فوتبال",

        position = "مهاجم",

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

        birth_date = "۱۳۷۰",

        residence = "کرج"

    }


    return buildInfobox(args)

end


--------------------------------------------------------------------------------
-- RETURN MODULE
--------------------------------------------------------------------------------

return p