A modult a Modul:multiple images/doc lapon tudod dokumentálni
-- implements ]
local p = {}
local function isnotempty(s)
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function renderImageCell(image, 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 imagediv = root:tag('div')
imagediv:addClass('thumbimage')
imagediv:cssText(istyle)
if( height ) then
imagediv:css('height', tostring(height) .. 'px')
imagediv:css('overflow', 'hidden')
end
imagediv:wikitext(']')
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 '', '*')
-- 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 or ic) or ic
local prownum = {}
while( ic > 0 ) do
prownum = 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 images
thisrow = math.min(tonumber(pr or thisrow) or ic, ic)
end
return prownum
end
local function renderMultipleImages(frame)
local pargs = frame:getParent().args
local args = frame.args
local width = pargs or ''
local dir = pargs or ''
local align = pargs or args or ''
local capalign = pargs or args or ''
local totalwidth = pargs or args or ''
local imgstyle = pargs or args
local header = pargs or pargs or ''
local footer = pargs or ''
local perrow = nil
local thumbclass = {
= 'tleft',
= 'tnone',
= 'tnone',
= 'tnone',
= 'tright'
}
-- find all the nonempty images
local imagenumbers = {}
local imagecount = 0
for k, v in pairs( pargs ) do
local i = tonumber(tostring(k):match( '^%s*image(+)%s*$' ) or '0')
if( i > 0 and isnotempty(v) ) then
table.insert( imagenumbers, i)
imagecount = imagecount + 1
end
end
-- sort the imagenumbers
table.sort(imagenumbers)
-- create an array with the number of images per row
perrow = getPerRow(dir == 'vertical' and '1' or pargs, imagecount)
-- compute the number of rows
local rowcount = #perrow
-- store the image 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 = 0
for c=1,perrow do
k = k + 1
if( k <= imagecount ) then
local i = imagenumbers
widths = getWidth(width, pargs)
widthsum = widthsum + widths
end
end
widthmax = math.max(widthmax, widthsum)
end
-- if total_width has been specified, rescale the image 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 - 1) - 12
local ar = {}
local arsum = 0
for j=1,perrow do
k = k + 1
if( k<= imagecount ) then
local i = imagenumbers
local h = tonumber( pargs or '' ) or 0
if (h > 0) then
ar = widths/h
heights = h
else
ar = widths/100
end
arsum = arsum + ar
end
end
local ht = tw/arsum
local ws = 0
k = koffset
for j=1,perrow do
k = k + 1
if( k<= imagecount ) then
local i = imagenumbers
widths = math.floor(ar*ht + 0.5)
ws = ws + widths
if heights then
heights = math.floor(ht)
end
end
end
widthsum = ws
widthmax = math.max(widthmax, widthsum)
end
end
-- start building the array of images, if there are images
if( imagecount > 0 ) then
-- compute width of outer div
local bodywidth = 0
for r=1,rowcount do
if( widthmax == widthsum ) then
bodywidth = widthmax + 4 * (perrow - 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 or ''
-- create the array of images
local root = mw.html.create('div')
root:addClass('thumb')
root:addClass('tmulti')
root:addClass(thumbclass or 'tright')
if( align == 'center' or align == 'centre' ) then
root:addClass('center')
end
if( pargs or args) then
root:css('margin-top', pargs or args)
end
if( pargs or args) then
root:css('margin-bottom', pargs or args)
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 or 'center')
:css('background-color', pargs or 'transparent')
:wikitext(header)
end
-- loop through the images
local k = 0
for r=1,rowcount do
for j=1,perrow do
k = k + 1
if( k <= imagecount ) then
local imagediv = div:tag('div')
imagediv:addClass('tsingle')
if dir ~= 'vertical' then
imagediv:css('float', 'left')
end
if bg ~= '' then
imagediv:css('background-color', bg);
end
imagediv:css('margin', '1px')
local i = imagenumbers
local img = pargs
local w = widths
imagediv:css('width', tostring(2 + w) .. 'px')
:css('max-width', tostring(2 + w) .. 'px')
imagediv:wikitext(renderImageCell(img, w, heights,
pargs, pargs,
pargs, capalign, imgstyle))
end
end
-- only float content gives a parent height:0, so add a clearing div
if dir ~= 'vertical' 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 or args or 'left')
:css('background-color', pargs or 'transparent')
:wikitext(footer)
end
return tostring(root)
end
return ''
end
function p.render( frame )
return renderMultipleImages( frame )
end
return p