Модуль:Userbox

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Документация

Это модуль для шаблона Userbox.

--
-- Реализует шаблон {{Userbox}}
-- Основа позаимствована из Module:Navbox.
--

local p = {}

local getArgs -- lazily initialized

local templateStyles = 'Шаблон:Userbox/styles.css'

local args

--
--   Render caption box
--

local function renderCaptionDiv(mainBox)
	if not args.cap then return end

	caption = mainBox:tag('div')
		:addClass('ts-Userbox-caption')
		:css('text-align', args['cap-a'])
		:css('background', args['cap-c'])
		:css('color', args['cap-fc'])
		
		if args['cap-s'] then
			caption:css('font-size', args['cap-s'] .. 'pt')
		end
		
	caption
		:cssText(args['cap-style'])
		:wikitext(args.cap)
end

--
--   Render main box with IDs and info cells
--

local function renderMainCell(mainBox)
	local baseBox = mainBox:tag('div')
		:addClass('ts-Userbox-main')
		
	if args.id or args.id1 or args[3] then

		local firstID = baseBox:tag('div')
			:addClass('ts-Userbox-idCell')
			:css('text-align', args['id1-a'] or args['id-a'])
			:css('background', args['id1-c'] or args['id-c'] or args[1])
			:css('color', args['id1-fc'] or args['id-fc'])
		
			if args['id1-s'] or args['id-s'] then
				firstID:css('font-size', (args['id1-s'] or args['id-s']) .. 'pt')
			end
		
		firstID
			:cssText(args['id1-style'] or args['id-style'])
			:wikitext(args.id or args.id1 or args[3])
	
	end
	
	if not (args.info or args[4]) then 
		
		local info = baseBox:tag('div')
			:addClass('ts-Userbox-info')
				:tag('p')
					:wikitext('info')
		
		else

		local info = baseBox:tag('div')
			:addClass('ts-Userbox-info')
			:css('text-align', args['info-a'])
			:css('background', args['info-c'] or args[2])
			:css('color', args['info-fc'])
		
			if args['info-s'] then
				info:css('font-size', args['info-s'] .. 'pt')
			end
		
		info
			:cssText(args['info-style'])
				:tag('p')
					:wikitext(args.info or args[4])
	end
	
	if args.id2 or args[5] then 

		local secondID = baseBox:tag('div')
			:addClass('ts-Userbox-idCell')
			:css('text-align', args['id2-a'] or args['id-a'])
			:css('background', args['id2-c'] or args['id-c'] or args[1])
			:css('color', args['id2-fc'] or args['id-fc'])
		
			if args['id2-s'] or args['id-s'] then
				secondID:css('font-size', (args['id2-s'] or args['id-s']) .. 'pt')
			end
		
		secondID
			:cssText(args['id2-style'] or args['id-style'])
			:wikitext(args.id2 or args[5])
	
	end
	
end

--
--   Main userbox container
--
local function renderMainTable()

	local mainBox = mw.html.create('div')
		:addClass('ts-Userbox')
		:addClass('nocolbreak')
		
		if args.float then
			mainBox:addClass('t' .. args.float)
		else
			mainBox:addClass('tright')
		end
		
		mainBox	
			:css('border-color', args[1] or args['border-c'] or args['id1-c'] or args['id-c'] or args['id2-c'] or args[2] or args['info-c'])
			if args['border-s'] then
				mainBox:css('border-width', args['border-s']..'px')
			end
		
	renderCaptionDiv(mainBox)
	renderMainCell(mainBox)

	return mainBox
end

function p._userbox(userboxArgs)
	args = userboxArgs

	-- render the main body of the userbox
	local mainBox = renderMainTable()

	return tostring(mainBox)
end

function p.userbox(frame)
	
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	args = getArgs(frame, {wrappers = {'Шаблон:Userbox/песочница', 'Шаблон:Userbox'}})
	
	-- Read the arguments in the order they'll be output in, to make references number in the right order.
	local _
	_ = args.cap
	_ = args.id
	_ = args.info
	_ = args.id2

	return frame:extensionTag{ name = 'templatestyles', args = { src = templateStyles } } .. p._userbox(args)
end

return p