پرش به محتوا

پودمان:InfoboxPerson/Core: تفاوت میان نسخه‌ها

از ویکی البرز
صفحه‌ای تازه حاوی «local p = {} --================================================== -- Module:InfoboxPerson/Core -- Version 2 -- Part 1 --================================================== local html = mw.html ---------------------------------------------------- -- LOAD DATA ---------------------------------------------------- local data = require("Module:InfoboxPerson/Data") local lookup = data.lookup or {} local layouts = data.layouts or {} loc...» ایجاد کرد
 
جزبدون خلاصۀ ویرایش
خط ۱: خط ۱:
local p = {}
local p = {}


--==================================================
----------------------------------------------------------
-- Module:InfoboxPerson/Core
-- Module:InfoboxPerson/Core
-- Version 2
-- WikiAlborz Infobox Engine
-- Part 1
-- Version 1.0
--==================================================
----------------------------------------------------------


local html = mw.html
local html = mw.html


----------------------------------------------------
local data = require('Module:InfoboxPerson/Data')
-- LOAD DATA
----------------------------------------------------


local data = require("Module:InfoboxPerson/Data")
local lookup  = data.lookup
local layouts  = data.layouts
local groups  = data.groups
local defaults = data.defaults


local lookup = data.lookup or {}


local layouts = data.layouts or {}


local sectionDefs = data.sections or {}
----------------------------------------------------------
-- LOAD TEMPLATESTYLES
----------------------------------------------------------


local function loadStyles()


return mw.getCurrentFrame():extensionTag{
name = 'templatestyles',
args = {
src = 'Module:InfoboxPerson/styles.css'
}
}


----------------------------------------------------
end
 
 
 
----------------------------------------------------------
-- NORMALIZE
-- NORMALIZE
----------------------------------------------------
----------------------------------------------------------


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


value = mw.text.trim(tostring(value))
value = tostring(value)
 
value = mw.text.trim(value)
 
if value == '' then
return nil
end
 
-- Remove invisible Unicode characters
value = mw.ustring.gsub(
value,
'[\194\160\226\128\139\226\128\140\226\128\141\239\187\191]',
''
)


if value == "" then
if value == '' then
return nil
return nil
end
end
خط ۴۵: خط ۷۰:




----------------------------------------------------
----------------------------------------------------------
-- CHECK VALUE
-- HAS VALUE
----------------------------------------------------
----------------------------------------------------------


local function hasValue(value)
local function hasValue(value)
خط ۵۷: خط ۸۲:




----------------------------------------------------
----------------------------------------------------------
-- COLLECT ARGUMENTS
-- READ ARGUMENT TABLE
----------------------------------------------------
----------------------------------------------------------


local function collectArgs(frame)
local function readArguments(source, target)


local args = {}
if not source then
 
return
local function merge(source)
end
 
if not source then
return
end
 
for key, value in pairs(source) do
 
key = normalize(key)
value = normalize(value)
 
if key and value then


args[key] = value
for key, value in pairs(source) do


end
key = normalize(key)
value = normalize(value)


if key and value then
target[key] = value
end
end


end
end
local parent = frame:getParent()
if parent then
merge(parent.args)
end
merge(frame.args)
return args


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




----------------------------------------------------
----------------------------------------------------------
-- RESOLVE ALIASES
-- COLLECT ARGUMENTS
----------------------------------------------------
----------------------------------------------------------


local function resolveArgs(rawArgs)
local function collectArgs(frame)


local args = {}
local args = {}


for key, value in pairs(rawArgs) do
local parent = frame:getParent()
 
local canonical = lookup[key]
 
if canonical then
 
args[canonical] = value
 
else
 
args[key] = value
 
end


if parent then
readArguments(parent.args, args)
end
end


readArguments(frame.args, args)


return args
return args
خط ۱۳۴: خط ۱۲۹:




----------------------------------------------------
----------------------------------------------------------
-- CREATE TABLE
-- RESOLVE ALIASES
----------------------------------------------------
----------------------------------------------------------
 
local function createBox()
 
return html.create("table")
:addClass("infobox")
:addClass("person-infobox")
 
end
----------------------------------------------------
-- RESOLVE ARGUMENTS
----------------------------------------------------


local function resolveArgs(rawArgs)
local function resolveArgs(rawArgs)


local args = {}
local args = {}


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


local canonical = lookup[key]
local canonical = lookup[key]


if canonical then
if canonical then
args[canonical] = value
args[canonical] = value
else
else
args[key] = value
args[key] = value
end
end


end
end


return args
return args
خط ۱۸۰: خط ۱۵۵:




----------------------------------------------------
----------------------------------------------------------
-- CREATE INFOBOX
-- CREATE INFOBOX
----------------------------------------------------
----------------------------------------------------------


local function createBox()
local function createBox()


 
return html.create('table')
local box = html.create("table")
:addClass('infobox')
 
:addClass('person-infobox')
 
box
:addClass("infobox")
:addClass("person-infobox")
 
 
return box


end
end
 
----------------------------------------------------------
 
 
----------------------------------------------------
-- ADD TITLE
-- ADD TITLE
----------------------------------------------------
----------------------------------------------------------
 
local function addTitle(box,args)


local function addTitle(box, args)


local title = args.name
local title = args.name


if not hasValue(title) then
if not hasValue(title) then
خط ۲۱۸: خط ۱۸۱:




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


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




----------------------------------------------------
----------------------------------------------------------
-- ADD IMAGE
-- ADD IMAGE
----------------------------------------------------
----------------------------------------------------------


local function addImage(box,args)
local function addImage(box, args)


local image = args.image


if not hasValue(args.image) then
if not hasValue(image) then


return
return
خط ۲۴۴: خط ۲۰۶:




 
box:tag('tr')
box:tag("tr")
:tag('td')
:tag("td")
:attr('colspan', '2')
:attr("colspan","2")
:addClass('person-infobox-image')
:addClass("infobox-person-image")
:wikitext(
:wikitext(
"[[File:" ..
'[[File:' ..
args.image ..
image ..
"|250px]]"
'|' ..
(defaults.image_size or '250px') ..
']]'
)
)




if hasValue(args.caption) then
if hasValue(args.caption) then


 
box:tag('tr')
box:tag("tr")
:tag('td')
:tag("td")
:attr('colspan', '2')
:attr("colspan","2")
:addClass('person-infobox-caption')
:addClass("infobox-person-caption")
:wikitext(args.caption)
:wikitext(args.caption)


end
end


end
end
خط ۲۷۴: خط ۲۳۳:




----------------------------------------------------
----------------------------------------------------------
-- ADD HEADER
-- ADD HEADER
----------------------------------------------------
----------------------------------------------------------
 
local function addHeader(box, title)
 
if not hasValue(title) then


local function addHeader(box,title)
return
 
end




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


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




----------------------------------------------------
----------------------------------------------------------
-- ADD ROW
-- ADD ROW
----------------------------------------------------
----------------------------------------------------------
 
local function addRow(box,label,value)


local function addRow(box, label, value)


if not hasValue(value) then
if not hasValue(value) then
خط ۳۰۶: خط ۲۶۹:




local row = box:tag('tr')


local row = box:tag("tr")


 
row:tag('th')
row
:addClass('person-infobox-label')
:tag("th")
:addClass("infobox-person-label")
:wikitext(label)
:wikitext(label)






row
row:tag('td')
:tag("td")
:addClass('person-infobox-value')
:addClass("infobox-person-value")
:wikitext(value)
:wikitext(value)


خط ۳۲۷: خط ۲۸۷:


end
end
----------------------------------------------------
 
 
 
----------------------------------------------------------
-- RENDER LAYOUT
-- RENDER LAYOUT
----------------------------------------------------
----------------------------------------------------------


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


if not layout then
if not layout then
خط ۳۳۹: خط ۳۰۱:


end
end




for i = 1, #layout do
for i = 1, #layout do


local item = layout[i]


local row = layout[i]
local label = item[1]
 
local field = item[2]
 
local label = row[1]
local field = row[2]




خط ۳۵۷: خط ۳۱۶:
args[field]
args[field]
)
)


end
end


end
end
----------------------------------------------------------
-- CHECK GROUP TRIGGER
----------------------------------------------------------


local function checkTrigger(group, args)


if not group.trigger then


----------------------------------------------------
return true
-- RENDER SECTION
----------------------------------------------------
 
local function renderSection(box, sectionName, args)
 
 
local section = sections[sectionName]
 
 
if not section then
 
return


end
end




for i = 1, #group.trigger do


local shouldShow = false
local field = group.trigger[i]




if hasValue(args[field]) then


if section.trigger then
return true


end


for i = 1, #section.trigger do
end




local field = section.trigger[i]
return false


end


if hasValue(args[field]) then


shouldShow = true
break


end
----------------------------------------------------------
-- RENDER GROUP
----------------------------------------------------------


local function renderGroup(box, group, args)


end
if not checkTrigger(group, args) then


 
return
else
 
shouldShow = true


end
end
خط ۴۱۶: خط ۳۶۷:




if not shouldShow then
if hasValue(group.title) then


return
addHeader(
box,
group.title
)


end
end
addHeader(
box,
section.title
)




خط ۴۳۳: خط ۳۸۰:
renderLayout(
renderLayout(
box,
box,
layouts[section.layout],
layouts[group.layout],
args
args
)
)


end
end
خط ۴۴۲: خط ۳۸۸:




----------------------------------------------------
----------------------------------------------------------
-- RENDER ALL SECTIONS
-- RENDER ALL GROUPS
----------------------------------------------------
----------------------------------------------------------


local function renderSections(box,args)
local function renderGroups(box, args)


for i = 1, #groups do


if not data.sectionOrder then
renderGroup(
box,
groups[i],
args
)


return
end


end
end






for i = 1, #data.sectionOrder do
----------------------------------------------------------
-- PREPARE ARGUMENTS
----------------------------------------------------------


local function prepareArgs(args)


local sectionName = data.sectionOrder[i]
for key, value in pairs(defaults) do


if args[key] == nil then


renderSection(
args[key] = value
box,
sectionName,
args
)


end


end
end


return args


end
end
خط ۴۷۷: خط ۴۳۱:




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


local function render(args)
local function render(args)


local box = createBox()
local box = createBox()




خط ۴۹۲: خط ۴۴۴:
args
args
)
)




خط ۵۰۱: خط ۴۵۲:




 
renderGroups(
renderSection(
box,
box,
"personal",
args
args
)
)




return loadStyles()
.. tostring(box)
end
----------------------------------------------------------
-- MAIN ENTRY POINT
----------------------------------------------------------


renderSection(
function p.main(frame)
box,
"career",
args
)


local rawArgs = collectArgs(frame)




renderSections(
local args = resolveArgs(rawArgs)
box,
args
)




args = prepareArgs(args)


return loadStyles()
.. tostring(box)


return render(args)


end
end
----------------------------------------------------------
-- EXPORT
----------------------------------------------------------
return p

نسخهٔ ‏۱۳ ژوئیهٔ ۲۰۲۶، ساعت ۱۱:۱۰

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

local p = {}

----------------------------------------------------------
-- Module:InfoboxPerson/Core
-- WikiAlborz Infobox Engine
-- Version 1.0
----------------------------------------------------------

local html = mw.html

local data = require('Module:InfoboxPerson/Data')

local lookup   = data.lookup
local layouts  = data.layouts
local groups   = data.groups
local defaults = data.defaults



----------------------------------------------------------
-- LOAD TEMPLATESTYLES
----------------------------------------------------------

local function loadStyles()

	return mw.getCurrentFrame():extensionTag{
		name = 'templatestyles',
		args = {
			src = 'Module:InfoboxPerson/styles.css'
		}
	}

end



----------------------------------------------------------
-- NORMALIZE
----------------------------------------------------------

local function normalize(value)

	if value == nil then
		return nil
	end

	value = tostring(value)

	value = mw.text.trim(value)

	if value == '' then
		return nil
	end

	-- Remove invisible Unicode characters
	value = mw.ustring.gsub(
		value,
		'[\194\160\226\128\139\226\128\140\226\128\141\239\187\191]',
		''
	)

	if value == '' then
		return nil
	end

	return value

end



----------------------------------------------------------
-- HAS VALUE
----------------------------------------------------------

local function hasValue(value)

	return normalize(value) ~= nil

end



----------------------------------------------------------
-- READ ARGUMENT TABLE
----------------------------------------------------------

local function readArguments(source, target)

	if not source then
		return
	end

	for key, value in pairs(source) do

		key = normalize(key)
		value = normalize(value)

		if key and value then
			target[key] = value
		end

	end

end



----------------------------------------------------------
-- COLLECT ARGUMENTS
----------------------------------------------------------

local function collectArgs(frame)

	local args = {}

	local parent = frame:getParent()

	if parent then
		readArguments(parent.args, args)
	end

	readArguments(frame.args, args)

	return args

end



----------------------------------------------------------
-- RESOLVE ALIASES
----------------------------------------------------------

local function resolveArgs(rawArgs)

	local args = {}

	for key, value in pairs(rawArgs) do

		local canonical = lookup[key]

		if canonical then
			args[canonical] = value
		else
			args[key] = value
		end

	end

	return args

end



----------------------------------------------------------
-- CREATE INFOBOX
----------------------------------------------------------

local function createBox()

	return html.create('table')
		:addClass('infobox')
		:addClass('person-infobox')

end
----------------------------------------------------------
-- ADD TITLE
----------------------------------------------------------

local function addTitle(box, args)

	local title = args.name

	if not hasValue(title) then

		title = mw.title.getCurrentTitle().text

	end


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

end



----------------------------------------------------------
-- ADD IMAGE
----------------------------------------------------------

local function addImage(box, args)

	local image = args.image

	if not hasValue(image) then

		return

	end


	box:tag('tr')
		:tag('td')
		:attr('colspan', '2')
		:addClass('person-infobox-image')
		:wikitext(
			'[[File:' ..
			image ..
			'|' ..
			(defaults.image_size or '250px') ..
			']]'
		)


	if hasValue(args.caption) then

		box:tag('tr')
			:tag('td')
			:attr('colspan', '2')
			:addClass('person-infobox-caption')
			:wikitext(args.caption)

	end

end



----------------------------------------------------------
-- ADD HEADER
----------------------------------------------------------

local function addHeader(box, title)

	if not hasValue(title) then

		return

	end


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

end



----------------------------------------------------------
-- ADD ROW
----------------------------------------------------------

local function addRow(box, label, value)

	if not hasValue(value) then

		return false

	end


	local row = box:tag('tr')


	row:tag('th')
		:addClass('person-infobox-label')
		:wikitext(label)



	row:tag('td')
		:addClass('person-infobox-value')
		:wikitext(value)



	return true

end



----------------------------------------------------------
-- RENDER LAYOUT
----------------------------------------------------------

local function renderLayout(box, layout, args)

	if not layout then

		return

	end


	for i = 1, #layout do

		local item = layout[i]

		local label = item[1]
		local field = item[2]


		addRow(
			box,
			label,
			args[field]
		)

	end

end
----------------------------------------------------------
-- CHECK GROUP TRIGGER
----------------------------------------------------------

local function checkTrigger(group, args)

	if not group.trigger then

		return true

	end


	for i = 1, #group.trigger do

		local field = group.trigger[i]


		if hasValue(args[field]) then

			return true

		end

	end


	return false

end



----------------------------------------------------------
-- RENDER GROUP
----------------------------------------------------------

local function renderGroup(box, group, args)

	if not checkTrigger(group, args) then

		return

	end



	if hasValue(group.title) then

		addHeader(
			box,
			group.title
		)

	end



	renderLayout(
		box,
		layouts[group.layout],
		args
	)

end



----------------------------------------------------------
-- RENDER ALL GROUPS
----------------------------------------------------------

local function renderGroups(box, args)

	for i = 1, #groups do

		renderGroup(
			box,
			groups[i],
			args
		)

	end

end



----------------------------------------------------------
-- PREPARE ARGUMENTS
----------------------------------------------------------

local function prepareArgs(args)

	for key, value in pairs(defaults) do

		if args[key] == nil then

			args[key] = value

		end

	end


	return args

end



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

local function render(args)

	local box = createBox()


	addTitle(
		box,
		args
	)


	addImage(
		box,
		args
	)


	renderGroups(
		box,
		args
	)


	return loadStyles()
		.. tostring(box)

end
----------------------------------------------------------
-- MAIN ENTRY POINT
----------------------------------------------------------

function p.main(frame)

	local rawArgs = collectArgs(frame)


	local args = resolveArgs(rawArgs)


	args = prepareArgs(args)


	return render(args)

end



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

return p