Modul:Photo montage

Izvor: Wikipedija
Dokumentacija modula[stvori]
-- implements [[predložak:kolaž]]
local p = {}

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

local function photomontage( frame )
	local args = frame:getParent().args
	local velicina = tonumber(args['velicina'] or '200') or 200
	local okvir = tonumber(args['okvir'] or '1') or 1
	local razmak = tonumber(args['razmak'] or '1') or 1
	local boja_razmaka = args['boja_razmaka'] or 'black'
	local boja_okvira = args['boja_okvira'] or 'black'
	local pozicija = (args['pozicija'] or ''):lower()
	local opis_slika = args['opis_slika'] or ''
	local boja_podloge = isnotempty(args['boja_podloge']) and args['boja_podloge'] or '#F8F8FF'
	local opis_van_okvira = args['opis_van_okvira'] or ''
	local lastnum = nil
	local rownum = nil
	local floatstyle = nil
	if( pozicija == 'left' or pozicija == 'right' or pozicija == 'none') then
		floatstyle = 'float:' .. pozicija
	elseif( pozicija == 'lijevo') then
		floatstyle = 'float: left'
	elseif( pozicija == 'desno') then
		floatstyle = 'float: right'
	elseif( pozicija == 'sredina' or pozicija == 'centar') then
		floatstyle = 'margin-left: auto; margin-right: auto;'
	elseif( pozicija == 'center' or pozicija == 'centre' ) then
		floatstyle = 'margin-left: auto; margin-right: auto;'
	end
	if isnotempty(opis_van_okvira) then
		local div = mw.html.create('div')
		div:css('font-size', 'smaller')
			:wikitext(opis_van_okvira)
		opis_van_okvira = '\n' .. tostring(div)
	end

	local lettertonumber = {
		a = '01', b = '02',	c = '03', d = '04',	e = '05', f = '06',	g = '07',
		h = '08', i = '09',	j = '10', k = '11',	l = '12', m = '13',	n = '14',
		o = '15', p = '16',	q = '17', r = '18', s = '19', t = '20', u = '21',
		v = '22', w = '23', x = '26', y = '27', z = '28' }
	local letters = {
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
		'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
		}

	-- find all the nonempty photo numbers
	local photos = {}
	local photocount = 0
	for k, v in pairs( args ) do
		local i = tonumber(tostring(k):match( '^%s*slika([%d]+)[a-z]%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			local c = lettertonumber[tostring(k):match( '^%s*slika[%d]+([a-z])%s*$' )]
			table.insert( photos, tonumber(i .. '.' .. c ) )
			photocount = photocount + 1
		end
	end
	-- sort the photo numbers
	table.sort(photos)

	-- compute the number of the photos in each row
	local count = {}
	lastnum = -1
	rownum = 0
	for k=1,photocount do
		local num = math.floor(photos[k])
		if(num == lastnum) then
			count[rownum] = count[rownum] + 1
		else
			rownum = rownum + 1
			count[rownum] = 1
		end
		lastnum = num
	end

	if(photocount > 0) then
	-- start table
	root = mw.html.create('table')
	root
		:css('background-color', boja_razmaka)
		:css('border-collapse', 'collapse')
		:css('border', okvir .. 'px solid ' .. boja_okvira)
		:css('width', velicina .. 'px')
		:cssText(floatstyle)
	local innercell = root
		:tag('tr')
			:tag('td')
				:css('border-top', 0)
				:css('padding', razmak .. 'px 0 0 ' .. razmak .. 'px')

	-- loop over the photos
	lastnum = -1
	rownum = 0
	local row
	for k=1,photocount do
		local num = math.floor(photos[k])
		local c = letters[math.floor(0.5 + 100*(photos[k] - num))]
		if(num ~= lastnum) then
			rownum = rownum + 1
			row = innercell
				:tag('table')
					:css('background-color', boja_razmaka)
					:css('border-collapse', 'collapse')
						:tag('tr')
		end
		local image = string.format( '[[Datoteka:%s|%dpx]]',
			args['slika' .. num .. c],
			(velicina - razmak*(count[rownum] - 1))/count[rownum] )
		row
			:tag('td')
				:css('border-top', 0)
				:css('padding', '0 ' .. razmak .. 'px ' .. razmak .. 'px ' .. '0')
				:wikitext(image)
		lastnum = num
	end
	if isnotempty(opis_slika) then
		root
			:tag('tr')
				:tag('td')
					:addClass('thumbcaption')
					:css('background-color', boja_podloge)
					:css('font-size', '88%')
					:wikitext(opis_slika)
	end
	-- end table
	end

    return tostring(root or '') .. opis_van_okvira
end

function p.montage( frame )
    return photomontage( frame )
end

return p