پودمان:InfoboxPerson/Core

نسخهٔ تاریخ ‏۱۲ ژوئیهٔ ۲۰۲۶، ساعت ۲۱:۳۸ توسط Modir (بحث | مشارکت‌ها) (صفحه‌ای تازه حاوی «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...» ایجاد کرد)
(تفاوت) → نسخهٔ قدیمی‌تر | نمایش نسخهٔ فعلی (تفاوت) | نسخهٔ جدیدتر ← (تفاوت)

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

local sectionDefs = data.sections or {}



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

local function normalize(value)

	if value == nil then
		return nil
	end

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

	if value == "" then
		return nil
	end

	return value

end



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

local function hasValue(value)

	return normalize(value) ~= nil

end



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

local function collectArgs(frame)

	local args = {}

	local function merge(source)

		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

			end

		end

	end


	local parent = frame:getParent()

	if parent then
		merge(parent.args)
	end


	merge(frame.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 TABLE
----------------------------------------------------

local function createBox()

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

end
----------------------------------------------------
-- RESOLVE ARGUMENTS
----------------------------------------------------

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


	local box = html.create("table")


	box
		:addClass("infobox")
		:addClass("person-infobox")


	return box

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("infobox-person-title")
		:wikitext(title)


end



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

local function addImage(box,args)


	if not hasValue(args.image) then

		return

	end



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



	if hasValue(args.caption) then


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


	end


end



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

local function addHeader(box,title)


	box:tag("tr")
		:tag("th")
		:attr("colspan","2")
		:addClass("infobox-person-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("infobox-person-label")
		:wikitext(label)



	row
		:tag("td")
		:addClass("infobox-person-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 row = layout[i]


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


		addRow(
			box,
			label,
			args[field]
		)


	end


end



----------------------------------------------------
-- RENDER SECTION
----------------------------------------------------

local function renderSection(box, sectionName, args)


	local section = sections[sectionName]


	if not section then

		return

	end



	local shouldShow = false



	if section.trigger then


		for i = 1, #section.trigger do


			local field = section.trigger[i]


			if hasValue(args[field]) then

				shouldShow = true
				break

			end


		end


	else

		shouldShow = true

	end



	if not shouldShow then

		return

	end



	addHeader(
		box,
		section.title
	)



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


end



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

local function renderSections(box,args)


	if not data.sectionOrder then

		return

	end



	for i = 1, #data.sectionOrder do


		local sectionName = data.sectionOrder[i]


		renderSection(
			box,
			sectionName,
			args
		)


	end


end



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

local function render(args)


	local box = createBox()



	addTitle(
		box,
		args
	)



	addImage(
		box,
		args
	)



	renderSection(
		box,
		"personal",
		args
	)



	renderSection(
		box,
		"career",
		args
	)



	renderSections(
		box,
		args
	)



	return loadStyles()
		.. tostring(box)


end