پرش به محتوا

پودمان: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') ---------------------------------------------------------- -- Helpers ------------------------------------------------...» ایجاد کرد
 
جزبدون خلاصۀ ویرایش
 
(۵ نسخهٔ میانیِ ایجادشده توسط همین کاربر نشان داده نشد)
خط ۸: خط ۸:
local Labels = require('Module:WorkInfoBox/Data/Labels')
local Labels = require('Module:WorkInfoBox/Data/Labels')
local Aliases = require('Module:WorkInfoBox/Data/Aliases')
local Aliases = require('Module:WorkInfoBox/Data/Aliases')
local Types = require('Module:WorkInfoBox/Data/Types')


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


local function norm(value)
local function norm(value)
if not value then
if not value then
return nil
return nil
end
end


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


if value == '' then
if value == '' then
خط ۲۶: خط ۲۸:


return value
return value
end
end


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


local value = norm(args[field])
local value = norm(args[field])
خط ۳۵: خط ۴۰:
return value
return value
end
end


local alias = Aliases[field]
local alias = Aliases[field]


if alias then
if alias then
for _, name in ipairs(alias) do
for _, name in ipairs(alias) do
value = norm(args[name])
value = norm(args[name])
if value then
if value then
return value
return value
end
end
end
end
end
end


return nil
return nil
end
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


----------------------------------------------------------
----------------------------------------------------------
-- Rendering
-- Row
----------------------------------------------------------
----------------------------------------------------------


local function addRow(tbl, label, value)
local function addRow(box, label, value)


if not value then
if not value then
خط ۶۰: خط ۱۴۰:
end
end


local row = tbl:tag('tr')


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


end
end
----------------------------------------------------------
-- Render
----------------------------------------------------------


local function render(frame)
local function render(frame)
خط ۷۶: خط ۱۶۲:
local args = getArgs(frame)
local args = getArgs(frame)


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


local layout = Layouts[kind] or Layouts.default


local root = html.create('table')
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('infobox')
:addClass('work-infobox')


------------------------------------------------------
------------------------------------------------------
-- Temporary
-- Title
-- (Header, image and sections will be added later)
------------------------------------------------------
 
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
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
for _, field in ipairs(section.fields) do


local value = firstArg(args, field)
 
local value = getValue(args, field)
 


if value then
if value then


local info = Fields[field]
addRow(
 
box,
if info then
getLabel(kind, field),
addRow(root, info.label, value)
value
end
)


end
end


end
end


end
end


return tostring(root)
 
 
return tostring(box)


end
end


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


function p.main(frame)
function p.main(frame)
return render(frame)
return render(frame)
end
end


return p
return p

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

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