Modul:Multiple image

Fra Wikipedia, den frie encyklopedi
Hopp til navigering Hopp til søk
-- implements [[template:multiple bilde]]
local p = {}

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

local function renderbildeCell(bilde, width, height, link, alt, caption, textalign, istyle) 
	local root = mw.html.create('')
	
	local altstr = '|alt=' .. (alt or '')
	local linkstr = link and ('|link=' .. link) or ''
	local widthstr = '|' .. tostring(width) .. 'px'
	
	local bildediv = root:tag('div')
	bildediv:addClass('thumbbilde')
	bildediv:cssText(istyle)
	if( height ) then
		bildediv:css('height', tostring(height) .. 'px')
		bildediv:css('overflow', 'hidden')
	end
	bildediv:wikitext('[[fil:' .. bilde .. widthstr .. linkstr .. altstr .. ']]')
	if isnotempty(caption) then
		local captiondiv = root:tag('div')
		captiondiv:addClass('thumbcaption')
		captiondiv:css('clear', 'left')
		if isnotempty(textalign) then
			captiondiv:css('text-align', textalign)
		end
		captiondiv:wikitext(caption)
	end
	return tostring(root)
end

local function getWidth(w1, w2)
	local w
	if isnotempty(w1) then
		w = tonumber(w1)
	elseif isnotempty(w2) then
		w = tonumber(w2)
	end
	return w or 200
end

local function getPerRow(pstr, ic)
	-- split string into array using any non-digit as a dilimiter
	local pr = mw.text.split(pstr or '', '[^%d][^%d]*')
	-- if split failed, assume a single row
	if (#pr < 1) then
		pr = {tostring(ic)}
	end
	-- convert the array of strings to an array of numbers,
	-- adding any implied/missing numbers at the end of the array
	local r = 1
	local thisrow = tonumber(pr[1] or ic) or ic
	local prownum = {}
	while( ic > 0 ) do
		prownum[r] = thisrow
		ic = ic - thisrow
		r = r + 1
		-- use the previous if the next is missing and 
		-- make sure we don't overstep the number of bildes
		thisrow = math.min(tonumber(pr[r] or thisrow) or ic, ic)
	end
	return prownum
end

local function renderMultiplebildes(frame)
	local pargs = frame:getParent().args
	local args = frame.args
	local width = pargs['width'] or pargs['bredde'] or ''
	local dir = pargs['direction'] or pargs['retning'] or ''
	local align = pargs['align'] or args['align'] or pargs['justering'] or args['justering'] or ''
	local capalign = pargs['caption_align'] or args['caption_align'] or pargs['justering_undertekst'] or args['justering_undertekst'] or ''
	local totalwidth = pargs['total_width'] or args['total_width'] or pargs['totalbredde'] or args['totalbredde'] or ''
	local imgstyle = pargs['bilde_style'] or args['bilde_style'] or pargs['bildestil'] or args['bildestil']
	local header = pargs['header'] or pargs['title'] or pargs['overskrift'] or pargs['tittel'] or ''
	local footer = pargs['footer'] or pargs['undertekst'] or ''
	local perrow = nil
	local thumbclass = {
		["left"] = 'tleft',
		["venstre"] = 'tleft',
		["none"] = 'tnone',
		["ingen"] = 'tnone',
		["center"] = 'tnone',
		["centre"] = 'tnone',
		["senter"] = 'tnone',
		["right"] = 'tright',
		["høyre"] = 'tright'
		}

	-- find all the nonempty bildes
	local bildenumbers = {}
	local bildecount = 0
	for k, v in pairs( pargs ) do
		local i = tonumber(tostring(k):match( '^%s*bilde([%d]+)%s*$' ) or '0')
		if( i > 0 and isnotempty(v) ) then
			table.insert( bildenumbers, i)
			bildecount = bildecount + 1
		end
	end

	-- sort the bildenumbers
	table.sort(bildenumbers)
	
	-- create an array with the number of bildes per row
	perrow = getPerRow(dir == 'vertikal' and '1' or pargs['perrad'], bildecount)
	
	-- compute the number of rows
	local rowcount = #perrow

	-- store the bilde widths and compute row widths and maximum row width
	local widths = {}
	local widthmax = 0
	local widthsum = {}
	local k = 0
	for r=1,rowcount do
		widthsum[r] = 0
		for c=1,perrow[r] do
			k = k + 1
			if( k <= bildecount ) then
				local i = bildenumbers[k]
				widths[k] = getWidth(width, pargs['bredde' .. i])
				widthsum[r] = widthsum[r] + widths[k]
			end
		end
		widthmax = math.max(widthmax, widthsum[r])
	end

	-- if total_width has been specified, rescale the bilde widths
	local heights = {}
	if( isnotempty(totalwidth) ) then
		totalwidth = tonumber(totalwidth)
		widthmax = 0
		local k = 0
		for r=1,rowcount do
			local koffset = k
			local tw = totalwidth - 4 * (perrow[r] - 1) - 12
			local ar = {}
			local arsum = 0
			for j=1,perrow[r] do
				k = k + 1
				if( k<= bildecount ) then
					local i = bildenumbers[k]
					local h = tonumber( pargs['høyde' .. i] or '' ) or 0
					if (h > 0) then
						ar[j] = widths[k]/h
						heights[k] = h
					else
						ar[j] = widths[k]/100
					end
					arsum = arsum + ar[j]
				end
			end
			local ht = tw/arsum
			local ws = 0
			k = koffset
			for j=1,perrow[r] do
				k = k + 1
				if( k<= bildecount ) then
					local i = bildenumbers[k]
					widths[k] = math.floor(ar[j]*ht + 0.5)
					ws = ws + widths[k]
					if heights[k] then
						heights[k] = math.floor(ht)
					end
				end
			end
			widthsum[r] = ws
			widthmax = math.max(widthmax, widthsum[r])
		end
	end	

	-- start building the array of bildes, if there are bildes
	if( bildecount > 0 ) then
		-- compute width of outer div
		local bodywidth = 0
		for r=1,rowcount do
			if( widthmax == widthsum[r] ) then
				bodywidth = widthmax + 4 * (perrow[r] - 1) + 12
			end
		end
		-- The body has a min-width of 100, which needs to be taken into account on specific widths
		bodywidth = math.max( 100, bodywidth - 8);

		local bg = pargs['bakgrunnsfarge'] or ''
		-- create the array of bildes
		local root = mw.html.create('div')
		root:addClass('thumb')
		root:addClass('tmulti')
		root:addClass(thumbclass[align] or 'tright')
		
		if( align == 'center' or align == 'centre' ) then
			root:addClass('center')
		end
		if( pargs['margin_top'] or args['margin_top']) then
			root:css('margin-top', pargs['margin_top'] or args['margin_top'])
		end
		if( pargs['margin_bottom'] or args['margin_bottom']) then
			root:css('margin-bottom', pargs['margin_bottom'] or args['margin_bottom'])
		end
		if( bg ~= '' ) then
			root:css('background-color', bg)
		end

		local div = root:tag('div')
		div:addClass('thumbinner')
		div:css('width', tostring(bodywidth) .. 'px')
			:css('max-width', tostring(bodywidth) .. 'px')
		if( bg ~= '' ) then
			div:css('background-color', bg)
		end
		-- add the header
		if( isnotempty(header) ) then
			div:tag('div')
				:css('clear', 'both')
				:css('font-weight', 'bold')
				:css('text-align', pargs['header_align'] or 'center')
				:css('background-color', pargs['header_background'] or 'transparent')
				:wikitext(header)
		end
		-- loop through the bildes
		local k = 0
		for r=1,rowcount do
			for j=1,perrow[r] do
				k = k + 1
				if( k <= bildecount ) then
					local bildediv = div:tag('div')
					bildediv:addClass('tsingle')
					if dir ~= 'vertikal' then
						bildediv:css('float', 'left')
					end
					if bg ~= '' then
						bildediv:css('background-color', bg);
					end
					bildediv:css('margin', '1px')
					local i = bildenumbers[k]
					local img = pargs['bilde' .. i]
					local w = widths[k]
					bildediv:css('width', tostring(2 + w) .. 'px')
						:css('max-width', tostring(2 + w) .. 'px')
					bildediv:wikitext(renderbildeCell(img, w, heights[k], 
						pargs['lenke' .. i], pargs['alt' .. i],
						pargs['bildetekst' .. i], capalign, imgstyle))
				end
			end
			-- only float content gives a parent height:0, so add a clearing div
			if dir ~= 'vertikal' then
				div:tag('div')
					:css('clear', 'left')
			end
		end
		-- add the footer
		if( isnotempty(footer) ) then
			div:tag('div')
				:addClass('thumbcaption')
				:css('clear', 'left')
				:css('text-align', pargs['footer_align'] or args['footer_align'] or 'left')
				:css('background-color', pargs['footer_background'] or 'transparent')
				:wikitext(footer)
		end
		return tostring(root)
	end
	return ''
end

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