Модуль:Масіў выяваў

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі

Дакумэнтацыю да гэтага модуля можна стварыць у Модуль:Масіў выяваў/Дакумэнтацыя

-- Выкарыстоўваецца ў [[Шаблён:Масіў выяваў]]

local p = {}

local function isnotempty(s)
	return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end

local function renderArrayCell( img, c, a, b, l, tc, t, w, h)
	local alt = isnotempty(a) and ('|апісаньне=' .. a) or ''
	local link = isnotempty(l) and ('|спасылка=' .. l) or ''
	local text = (isnotempty(tc) and not isnotempty(t)) 
		and mw.text.unstrip(c) or mw.text.unstrip(t or '')
	local border = isnotempty(b) and '|рамка' or ''
		
	local cell = mw.html.create('')
	if( img ) then
		cell:tag('div')
			:css('display', 'table-cell')
			:css('vertical-align', 'middle')
			:css('width', w .. 'px')
			:css('height', h .. 'px')
			:css('margin-left', 'auto')
			:css('margin-right', 'auto')
			:wikitext(mw.ustring.format('[[Файл:%s|%dx%dпкс%s|%s]]', 
				img, w, h, alt .. link .. border, text))
		cell:tag('div')
			:css('padding', '1px')
			:wikitext(c)
	end
	
	return tostring(cell)
end

function p._imagearray( args )
	local width = tonumber(args['шырыня'] or '60')
	local height = tonumber(args['вышыня'] or '70')
	local perrow = tonumber(args['у_шэрагу'] or '4')
	local bw = tonumber(args['шырыня_рамкі'] or '0')
	local fs = args['памер_шрыфту'] or '88%'
	local text = args['тэкст'] or ''
	local margin = args['margin'] or 'auto'
	local border = ( bw > 0 ) and tostring(bw) .. 'px #aaa solid' or nil

	-- знайсьці ўсе непустыя нумары выяваў
	local imagenums = {}
	local imagecount = 0
	for k, v in pairs( args ) do
		local i = tonumber(tostring(k):match( '^%s*выява([%d]+)%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			table.insert( imagenums, i )
			imagecount = imagecount + 1
		end
	end
	-- адсартаваць нумары выяваў
	table.sort(imagenums)
	
	-- вылічыць колькасьць шэрагаў
	local rowcount = math.ceil(imagecount / perrow)
	
	-- пачатак табліцы
	root = mw.html.create('table')
	root
		:addClass(args['class'])
		:css('border-collapse','collapse')
		:css('text-align','center')
		:css('font-size', fs)
		:css('line-height','1.25em')
		:css('margin',margin)
		:css('width', tostring(width*perrow) .. 'px')

	-- цыкль па выявах
	for j = 1, rowcount do
		local row = root:tag('tr')
		row:css('vertical-align', 'top')
		for k = 1, perrow do
			i = imagenums[(j-1)*perrow + k] or 0
			row:tag('td')
				:css('width', width .. 'px')
				:css('text-align', 'center')
				:css(border and 'border' or '', border or '')
				:wikitext(renderArrayCell( 
					args['выява' .. i], args['подпіс' .. i], args['апісаньне' .. i], 
					args['рамка' .. i], args['спасылка' .. i] , 
					args['тэкст'], args['text' .. i] , width, height)
					)
		end
	end
    
	-- канец табліцы
    return tostring(root)
end
function p.array( frame )
	local args = frame:getParent().args
    return p._imagearray( args )
end
 
return p