پرش به محتوا

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

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


خط ۲۴: خط ۲۴:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
خط ۳۳: خط ۳۳:
local function clean(value)
local function clean(value)


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


    if type(value) == "string" then
--------------------------------------------------------------------------------
-- CHECK VALUE
--------------------------------------------------------------------------------


        value = mw.text.trim(value)
local function hasValue(value)


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


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


    return value
if type(value) == "table" then
return next(value) ~= nil
end


return true
end
end


خط ۵۸: خط ۷۹:
local function getArgs(frame)
local function getArgs(frame)


    local args = {}
local args = {}


    local source = frame
local source = frame


    local parent = frame:getParent()
local parent = frame:getParent()




    if parent and next(parent.args) then
if parent and next(parent.args) then


        source = parent
source = parent


    end
end




    for k,v in pairs(source.args) do
for k, v in pairs(source.args) do


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


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


        if value then
if value then


            args[key] = value
args[key] = value


        end
end


    end
end




    return args
return args
 
end
end


خط ۹۸: خط ۱۱۸:
local function normalize(args)
local function normalize(args)


    local result = {}
local result = {}


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


        local field = data.lookup[key] or key
local field =
data.lookup[key] or key


        result[field] = value
result[field] = value


    end
end
 
    return result


return result
end
end


خط ۱۱۹: خط ۱۳۹:
local function getEntity(args)
local function getEntity(args)


    local entity = args.entity_type
local entity = args.entity_type


    if not entity then
if not entity then


        entity = data.defaults.entity_type
entity =
data.defaults.entity_type


    end
end




    if data.entities[entity] then
if data.entities[entity] then


        return entity
return entity


    end
end




    return "player"
return "player"
 
end
end


خط ۱۴۴: خط ۱۶۴:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function getTitle(entity,args)
local function getTitle(entity, args)


    if args.name then
if args.name then


        return args.name
return args.name


    end
end




    if data.entities[entity] then
if data.entities[entity] then


        return data.entities[entity].title
return data.entities[entity].title


    end
end




    return data.meta.title
return data.meta.title
end
 


end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- IMAGE HANDLING
-- IMAGE HANDLING
خط ۱۶۹: خط ۱۹۰:
local function buildImage(args)
local function buildImage(args)


    if not args.image then
if not args.image then


        return nil
return nil


    end
end




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




    local caption =
local caption =
        args.caption or ""
args.caption or ""




    local image = {
local image = {


        type = "image",
type = "image",


        image = args.image,
image = args.image,


        size = width,
size = width,


        caption = caption
caption = caption


    }
}




    return image
return image
 
end
end


خط ۲۰۶: خط ۲۲۶:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


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


    local fields = {}
local fields = {}




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


local label =
layout[1]


        local label = layout[1]
local key =
layout[2]


        local key = layout[2]


local value =
args[key]


        local value = args[key]


value =
clean(value)


        value = clean(value)


if value then


        if value then
table.insert(fields, {


label = label,


            table.insert(fields,{
value = value


                label = label,
})


                value = value
end


            })
end




        end
return fields
end
 
 
--------------------------------------------------------------------------------
-- GROUP BUILDER
--------------------------------------------------------------------------------
 
local function buildGroups(entity, args)


local groups = {}


    end


local entityData =
data.entities[entity]


    return fields


end
if not entityData then


return groups


--------------------------------------------------------------------------------
end
-- GROUP BUILDER
--------------------------------------------------------------------------------


local function buildGroups(entity,args)


    local groups = {}
for _, layoutName in ipairs(entityData.layouts) do


local layout =
data.layouts[layoutName]


    local entityData =
        data.entities[entity]


if layout then


    if not entityData then
local fields =
buildFields(layout, args)


        return groups


    end
if #fields > 0 then


table.insert(groups, {


name =
data.layoutTitles[layoutName]
or layoutName,


    for _,layoutName in ipairs(entityData.layouts) do
layoutName =
layoutName,


fields =
fields


        local layout =
})
            data.layouts[layoutName]


end


        if layout then
end


end


            local fields =
                buildFields(layout,args)


return groups
end




            if #fields > 0 then
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
--------------------------------------------------------------------------------


local function buildInfobox(entity, args)


                table.insert(groups,{
local box = {}


                    name =
                        data.layoutTitles[layoutName]
                        or layoutName,


                    fields = fields
box.title =
getTitle(entity, args)


                })


local image =
buildImage(args)


            end


if image then


        end
box.image =
image


end


    end


box.groups =
buildGroups(entity, args)


    return groups


return box
end
end




--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- INFOBOX BUILDER
-- MEDALS
--------------------------------------------------------------------------------
-- Supports both:
--
-- |medaltemplates=
-- |medals=
--
-- medaltemplates has priority when both are supplied.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


local function buildInfobox(entity,args)
local function addMedals(result, args)


local medals =
args.medaltemplates


    local box = {}


if not hasValue(medals) then


    box.title =
medals =
        getTitle(entity,args)
args.medals


end




    local image =
if not hasValue(medals) then
        buildImage(args)


return


end


    if image then


        box.image = image
local frame =
mw.getCurrentFrame()


    end


local content =
frame:expandTemplate{


title =
"Infobox medal templates",


    box.groups =
args = {
        buildGroups(entity,args)


medals =
medals,


title =
args.medaltemplates_title
or "",


    return box
expand =
"yes"
 
}
 
}
 
 
if not hasValue(content) then
 
return
 
end
 
 
table.insert(
result,
 
'<tr>' ..
'<td colspan="2" class="sport-infobox-medals">' ..
content ..
'</td>' ..
'</tr>'
 
)


end
end
خط ۳۵۶: خط ۴۴۵:
local function renderValue(value)
local function renderValue(value)


if type(value) == "table" then


    if type(value) == "table" then
return table.concat(value, "، ")


        return table.concat(value,"، ")
end


    end


return tostring(value)
end


    return tostring(value)


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


local function renderInfobox(box)
local function renderInfobox(box, args)


    local result = {}
local result = {}


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


table.insert(
result,


    ------------------------------------------------------------
'<table class="sport-infobox">'
    -- TITLE
    ------------------------------------------------------------


    if box.title then
)


        table.insert(result,


            '<tr class="sport-infobox-title">' ..
------------------------------------------------------------
            '<th colspan="2">' ..
-- TITLE
            box.title ..
------------------------------------------------------------
            '</th></tr>'


        )
if box.title then


    end
table.insert(


result,


    ------------------------------------------------------------
'<tr class="sport-infobox-title">' ..
    -- IMAGE
    ------------------------------------------------------------


    if box.image then
'<th colspan="2">' ..


        local image = box.image
box.title ..


        local file = image.image
'</th></tr>'


        local size = image.size or DEFAULT_IMAGE_WIDTH
)


        local caption = image.caption or ""
end




        table.insert(result,
------------------------------------------------------------
-- IMAGE
------------------------------------------------------------


            '<tr>' ..
if box.image then
            '<td colspan="2" class="sport-infobox-image">' ..
            '[[File:' ..
            file ..
            '|' ..
            size ..
            ']]'


        )
local image =
box.image


local file =
image.image


        if caption ~= "" then
local size =
image.size or DEFAULT_IMAGE_WIDTH


            table.insert(result,
local caption =
image.caption or ""


                '<div class="sport-infobox-caption">' ..
                caption ..
                '</div>'


            )
table.insert(


        end
result,


'<tr>' ..


        table.insert(result,
'<td colspan="2" class="sport-infobox-image">' ..


            '</td></tr>'
'[[File:' ..


        )
file ..


    end
'|' ..


size ..


']]'


    ------------------------------------------------------------
)
    -- GROUPS
    ------------------------------------------------------------


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


if caption ~= "" then


        table.insert(result,
table.insert(


            '<tr class="sport-infobox-header">' ..
result,
            '<th colspan="2">' ..
            group.name ..
            '</th></tr>'


        )
'<div class="sport-infobox-caption">' ..


caption ..


'</div>'


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


end


            table.insert(result,


                '<tr>' ..
table.insert(


                '<th class="sport-infobox-label">' ..
result,
                field.label ..
                '</th>' ..


                '<td class="sport-infobox-value">' ..
'</td></tr>'
                renderValue(field.value) ..
                '</td>' ..


                '</tr>'
)


            )
end




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


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


    end


table.insert(


result,


    ------------------------------------------------------------
'<tr class="sport-infobox-header">' ..
    -- CLOSE
    ------------------------------------------------------------


    table.insert(result,
'<th colspan="2">' ..


        '</table>'
group.name ..


    )
'</th></tr>'


)


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


end
for _, field in ipairs(group.fields) do
 
 
table.insert(
 
result,
 
'<tr>' ..
 
'<th class="sport-infobox-label">' ..
 
field.label ..
 
'</th>' ..
 
'<td class="sport-infobox-value">' ..
 
renderValue(field.value) ..


--------------------------------------------------------------------------------
'</td>' ..
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------


function p.main(frame)
'</tr>'


)


    local args =
end
        getArgs(frame)




--------------------------------------------------------
-- MEDALS
--------------------------------------------------------
-- Insert medals immediately after the Honors group.
--------------------------------------------------------


    args =
if group.layoutName == "honors" then
        normalize(args)


addMedals(
result,
args
)


end


    local entity =
end
        getEntity(args)




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


    local box =
table.insert(
        buildInfobox(
            entity,
            args
        )


result,


'</table>'


    return renderInfobox(box)
)




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


--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------
function p.main(frame)
local args =
getArgs(frame)
args =
normalize(args)
local entity =
getEntity(args)
local box =
buildInfobox(
entity,
args
)
return renderInfobox(
box,
args
)
end




خط ۵۵۲: خط ۶۹۰:
function p.infobox(frame)
function p.infobox(frame)


    return p.main(frame)
return p.main(frame)


end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- DEBUG FUNCTION
-- DEBUG FUNCTION
خط ۵۶۱: خط ۷۰۱:
function p.test(frame)
function p.test(frame)


    local args =
local args =
        getArgs(frame)
getArgs(frame)
 
 
    args =
        normalize(args)
 




    local entity =
args =
        getEntity(args)
normalize(args)




local entity =
getEntity(args)


    local entityData =
        data.entities[entity]


local entityData =
data.entities[entity]




    if not entityData then
if not entityData then


        return "Entity not found"
return "Entity not found"


    end
end




local result = {}


    local result = {}


table.insert(
result,


"Entity: " ..
entity


    table.insert(
)
        result,


        "Entity: " .. entity


    )
table.insert(
result,


"Title: " ..
entityData.title


)


    table.insert(
        result,


        "Title: " .. entityData.title
table.insert(
result,


    )
"Layouts: " ..


table.concat(


entityData.layouts,


    table.insert(
", "
        result,


        "Layouts: " ..
)
        table.concat(
            entityData.layouts,
            ", "
        )


    )
)




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




خط ۶۳۷: خط ۷۷۴:
function p.entities()
function p.entities()


    local result = {}
local result = {}
 




    for key,value in pairs(data.entities) do
for key, value in pairs(data.entities) do


table.insert(


        table.insert(
result,


            result,
key ..
" : " ..
value.title


            key .. " : " .. value.title
)


        )
end




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




خط ۶۷۲: خط ۸۰۵:
function p.version()
function p.version()


    return
return


        data.version.name ..
data.version.name ..


        " v" ..
" v" ..


        data.version.major ..
data.version.major ..


        "." ..
"." ..
 
        data.version.minor


data.version.minor
end
end





نسخهٔ ‏۸ اوت ۲۰۲۶، ساعت ۲۱:۳۷

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

--------------------------------------------------------------------------------
-- Module:AlborzSports
-- Wiki Alborz Sports Information System
-- Core Engine
-- Version 2.2
--------------------------------------------------------------------------------

local p = {}

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

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

--------------------------------------------------------------------------------
-- LOAD DEPENDENCIES
--------------------------------------------------------------------------------

local infobox = require("Module:Infobox")

--------------------------------------------------------------------------------
-- CONFIG
--------------------------------------------------------------------------------

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

--------------------------------------------------------------------------------
-- UTILITY FUNCTIONS
--------------------------------------------------------------------------------

local function clean(value)

	if value == nil then
		return nil
	end

	if type(value) == "string" then

		value = mw.text.trim(value)

		if value == "" then
			return nil
		end

	end

	return value
end


--------------------------------------------------------------------------------
-- CHECK VALUE
--------------------------------------------------------------------------------

local function hasValue(value)

	if value == nil then
		return false
	end

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

	if type(value) == "table" then
		return next(value) ~= nil
	end

	return true
end


--------------------------------------------------------------------------------
-- GET ARGUMENTS
--------------------------------------------------------------------------------

local function getArgs(frame)

	local args = {}

	local source = frame

	local parent = frame:getParent()


	if parent and next(parent.args) then

		source = parent

	end


	for k, v in pairs(source.args) do

		local key = mw.text.trim(k)

		local value = clean(v)

		if value then

			args[key] = value

		end

	end


	return args
end


--------------------------------------------------------------------------------
-- NORMALIZE INPUT
--------------------------------------------------------------------------------

local function normalize(args)

	local result = {}

	for key, value in pairs(args) do

		local field =
			data.lookup[key] or key

		result[field] = value

	end

	return result
end


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

local function getEntity(args)

	local entity = args.entity_type

	if not entity then

		entity =
			data.defaults.entity_type

	end


	if data.entities[entity] then

		return entity

	end


	return "player"
end


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

local function getTitle(entity, args)

	if args.name then

		return args.name

	end


	if data.entities[entity] then

		return data.entities[entity].title

	end


	return data.meta.title
end


--------------------------------------------------------------------------------
-- IMAGE HANDLING
--------------------------------------------------------------------------------

local function buildImage(args)

	if not args.image then

		return nil

	end


	local width =
		args.image_width or DEFAULT_IMAGE_WIDTH


	local caption =
		args.caption or ""


	local image = {

		type = "image",

		image = args.image,

		size = width,

		caption = caption

	}


	return image
end


--------------------------------------------------------------------------------
-- FIELD BUILDER
--------------------------------------------------------------------------------

local function buildFields(layouts, args)

	local fields = {}


	for _, layout in ipairs(layouts) do

		local label =
			layout[1]

		local key =
			layout[2]


		local value =
			args[key]


		value =
			clean(value)


		if value then

			table.insert(fields, {

				label = label,

				value = value

			})

		end

	end


	return fields
end


--------------------------------------------------------------------------------
-- GROUP BUILDER
--------------------------------------------------------------------------------

local function buildGroups(entity, args)

	local groups = {}


	local entityData =
		data.entities[entity]


	if not entityData then

		return groups

	end


	for _, layoutName in ipairs(entityData.layouts) do

		local layout =
			data.layouts[layoutName]


		if layout then

			local fields =
				buildFields(layout, args)


			if #fields > 0 then

				table.insert(groups, {

					name =
						data.layoutTitles[layoutName]
						or layoutName,

					layoutName =
						layoutName,

					fields =
						fields

				})

			end

		end

	end


	return groups
end


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

local function buildInfobox(entity, args)

	local box = {}


	box.title =
		getTitle(entity, args)


	local image =
		buildImage(args)


	if image then

		box.image =
			image

	end


	box.groups =
		buildGroups(entity, args)


	return box
end


--------------------------------------------------------------------------------
-- MEDALS
--------------------------------------------------------------------------------
-- Supports both:
--
-- |medaltemplates=
-- |medals=
--
-- medaltemplates has priority when both are supplied.
--------------------------------------------------------------------------------

local function addMedals(result, args)

	local medals =
		args.medaltemplates


	if not hasValue(medals) then

		medals =
			args.medals

	end


	if not hasValue(medals) then

		return

	end


	local frame =
		mw.getCurrentFrame()


	local content =
		frame:expandTemplate{

			title =
				"Infobox medal templates",

			args = {

				medals =
					medals,

				title =
					args.medaltemplates_title
					or "",

				expand =
					"yes"

			}

		}


	if not hasValue(content) then

		return

	end


	table.insert(
		result,

		'<tr>' ..
		'<td colspan="2" class="sport-infobox-medals">' ..
		content ..
		'</td>' ..
		'</tr>'

	)

end


--------------------------------------------------------------------------------
-- RENDER TEXT
--------------------------------------------------------------------------------

local function renderValue(value)

	if type(value) == "table" then

		return table.concat(value, "، ")

	end


	return tostring(value)
end


--------------------------------------------------------------------------------
-- RENDER INFOBOX
--------------------------------------------------------------------------------

local function renderInfobox(box, args)

	local result = {}


	table.insert(
		result,

		'<table class="sport-infobox">'

	)


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

	if box.title then

		table.insert(

			result,

			'<tr class="sport-infobox-title">' ..

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

			box.title ..

			'</th></tr>'

		)

	end


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

	if box.image then

		local image =
			box.image

		local file =
			image.image

		local size =
			image.size or DEFAULT_IMAGE_WIDTH

		local caption =
			image.caption or ""


		table.insert(

			result,

			'<tr>' ..

			'<td colspan="2" class="sport-infobox-image">' ..

			'[[File:' ..

			file ..

			'|' ..

			size ..

			']]'

		)


		if caption ~= "" then

			table.insert(

				result,

				'<div class="sport-infobox-caption">' ..

				caption ..

				'</div>'

			)

		end


		table.insert(

			result,

			'</td></tr>'

		)

	end


	------------------------------------------------------------
	-- GROUPS
	------------------------------------------------------------

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


		table.insert(

			result,

			'<tr class="sport-infobox-header">' ..

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

			group.name ..

			'</th></tr>'

		)


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


			table.insert(

				result,

				'<tr>' ..

				'<th class="sport-infobox-label">' ..

				field.label ..

				'</th>' ..

				'<td class="sport-infobox-value">' ..

				renderValue(field.value) ..

				'</td>' ..

				'</tr>'

			)

		end


		--------------------------------------------------------
		-- MEDALS
		--------------------------------------------------------
		-- Insert medals immediately after the Honors group.
		--------------------------------------------------------

		if group.layoutName == "honors" then

			addMedals(
				result,
				args
			)

		end

	end


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

	table.insert(

		result,

		'</table>'

	)


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


--------------------------------------------------------------------------------
-- PUBLIC FUNCTION
--------------------------------------------------------------------------------

function p.main(frame)

	local args =
		getArgs(frame)


	args =
		normalize(args)


	local entity =
		getEntity(args)


	local box =
		buildInfobox(
			entity,
			args
		)


	return renderInfobox(
		box,
		args
	)
end


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

function p.infobox(frame)

	return p.main(frame)

end


--------------------------------------------------------------------------------
-- DEBUG FUNCTION
--------------------------------------------------------------------------------

function p.test(frame)

	local args =
		getArgs(frame)


	args =
		normalize(args)


	local entity =
		getEntity(args)


	local entityData =
		data.entities[entity]


	if not entityData then

		return "Entity not found"

	end


	local result = {}


	table.insert(
		result,

		"Entity: " ..
		entity

	)


	table.insert(
		result,

		"Title: " ..
		entityData.title

	)


	table.insert(
		result,

		"Layouts: " ..

		table.concat(

			entityData.layouts,

			", "

		)

	)


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


--------------------------------------------------------------------------------
-- GET ENTITY LIST
--------------------------------------------------------------------------------

function p.entities()

	local result = {}


	for key, value in pairs(data.entities) do

		table.insert(

			result,

			key ..
			" : " ..
			value.title

		)

	end


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


--------------------------------------------------------------------------------
-- GET VERSION
--------------------------------------------------------------------------------

function p.version()

	return

		data.version.name ..

		" v" ..

		data.version.major ..

		"." ..

		data.version.minor
end


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

return p