پرش به محتوا

پودمان:WorkInfoBox

از ویکی البرز

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

local p = {}

local getArgs = require('Module:Arguments').getArgs
local html = mw.html

local Fields = require('Module:WorkInfoBox/Data/Fields')
local Layouts = require('Module:WorkInfoBox/Data/Layouts')
local Labels = require('Module:WorkInfoBox/Data/Labels')
local Aliases = require('Module:WorkInfoBox/Data/Aliases')
local Types = require('Module:WorkInfoBox/Data/Types')


----------------------------------------------------------
-- Helpers
----------------------------------------------------------

local function norm(value)

	if not value then
		return nil
	end

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

	if value == '' then
		return nil
	end

	return value

end



local function getValue(args, field)

	local value = norm(args[field])

	if value then
		return value
	end


	local alias = Aliases[field]

	if alias then

		for _, name in ipairs(alias) do

			value = norm(args[name])

			if value then
				return value
			end

		end

	end


	return nil

end



local function getLabel(kind, field)

	if Labels[kind]
		and Labels[kind][field] then

		return Labels[kind][field]

	end


	if Fields[field] then
		return Fields[field].label
	end


	return field

end



----------------------------------------------------------
-- Image
----------------------------------------------------------

local function addImage(box, args)

	local image = getValue(args, 'image')

	if not image then
		return
	end


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



	local caption = getValue(args, 'image_caption')


	if caption then

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

	end

end



----------------------------------------------------------
-- Row
----------------------------------------------------------

local function addRow(box, label, value)

	if not value then
		return
	end


	box:tag('tr')
		:tag('th')
		:addClass('infobox-label')
		:wikitext(label)
		:done()
		:tag('td')
		:addClass('infobox-data')
		:wikitext(value)

end



----------------------------------------------------------
-- Render
----------------------------------------------------------

local function render(frame)

	local args = getArgs(frame)



	local kind = getValue(args, 'type')
		or 'default'


	-- تبدیل نوع فارسی به کلید داخلی
	kind = Types[kind]
		or kind



	local layout = Layouts[kind]
		or Layouts.default



	local box = html.create('table')
		:addClass('infobox')
		:addClass('work-infobox')



	------------------------------------------------------
	-- Title
	------------------------------------------------------

	local title = getValue(args, 'title')


	if title then

		box:tag('caption')
			:addClass('infobox-title')
			:wikitext(title)

	end



	------------------------------------------------------
	-- Image
	------------------------------------------------------

	addImage(box, args)



	------------------------------------------------------
	-- Sections
	------------------------------------------------------

	for _, section in ipairs(layout) do


		if section.title then

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

		end



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


			local value = getValue(args, field)


			if value then

				addRow(
					box,
					getLabel(kind, field),
					value
				)

			end

		end


	end



	return tostring(box)

end



----------------------------------------------------------

function p.main(frame)

	return render(frame)

end


return p