Module:Image array

From Sarkarverse
Jump to navigation Jump to search

-- implements template:image array

local p = {}

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

local function arraycell( img, c, a, l, tc, t, w, h, rowend, bw, fs) local alt, link, text, endstr, border, font = , , t, , ,

if( isnotempty(a) ) then alt = 'alt=' .. a end if( isnotempty(l) ) then link = 'link=' .. l end if( isnotempty(tc) and not isnotempty(t)) then text = c end

if( rowend ) then endstr = '' end if( bw > 0 ) then border = 'border:' .. tostring(bw) .. 'px #aaa solid' end if( isnotempty(fs) ) then font = 'font-size:' .. fs end return mw.ustring.format( '' ..'

' ..'%s

' ..'

%s

%s',

w, border, w, h, img, w, h, alt, link, text, font, c, endstr) end

local function imagearray( frame ) local args = frame:getParent().args local width = tonumber(args['width'] or '60') local height = tonumber(args['height'] or '70') local perrow = tonumber(args['perrow'] or '4') local bw = tonumber(args['border-width'] or '0') local text = args['text'] or local fs = args['font-size'] or

local t = {}

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

-- start array

table.insert(t, '

\n\n') -- loop over the images for k, i in ipairs( imagenums ) do local rowend = (math.fmod(k, perrow) == 0) and (k ~= imagecount) table.insert(t, arraycell( args['image' .. i], args['caption' .. i] or , args['alt' .. i] or , args['link' .. i] or , args['text'] or , args['text' .. i] or , width, height, rowend, bw, fs) ) end -- end array table.insert(t, '\n

')

   return table.concat( t, '\n' )

end function p.array( frame )

   return imagearray( frame )

end

return p