Difference between revisions of "Module:Image array/sandbox"
Jump to navigation
Jump to search
blackwiki>Frietjes |
blackwiki>Frietjes |
||
| Line 7: | Line 7: | ||
end | end | ||
| − | local function renderArrayCell( img, c, a, l, tc, t, w, h | + | local function renderArrayCell( img, c, a, l, tc, t, w, h) |
| − | local alt, link, text, endstr, border | + | local alt, link, text, endstr, border |
if( isnotempty(a) ) then alt = 'alt=' .. a end | if( isnotempty(a) ) then alt = 'alt=' .. a end | ||
if( isnotempty(l) ) then link = 'link=' .. l end | if( isnotempty(l) ) then link = 'link=' .. l end | ||
if( isnotempty(tc) and not isnotempty(t)) then text = c end | if( isnotempty(tc) and not isnotempty(t)) then text = c end | ||
| − | |||
| − | |||
return mw.ustring.format('<div style="vertical-align:middle; width:%dpx; height:%dpx; margin-left:auto; margin-right:auto;">' | return mw.ustring.format('<div style="vertical-align:middle; width:%dpx; height:%dpx; margin-left:auto; margin-right:auto;">' | ||
..'[[File:%s|%dx%dpx|%s|%s|%s]]</div>' | ..'[[File:%s|%dx%dpx|%s|%s|%s]]</div>' | ||
| − | ..'<div style="vertical-align:middle; padding:1px; | + | ..'<div style="vertical-align:middle; padding:1px;">%s</div>', |
| − | w, h, img, w, h, alt, link, text | + | w, h, img, w, h, alt, link, text, c) |
end | end | ||
| Line 70: | Line 68: | ||
args['image' .. i], args['caption' .. i] or '', args['alt' .. i] or '', | args['image' .. i], args['caption' .. i] or '', args['alt' .. i] or '', | ||
args['link' .. i] or '', args['text'] or '', args['text' .. i] or '', | args['link' .. i] or '', args['text'] or '', args['text' .. i] or '', | ||
| − | width, height | + | width, height)) |
else | else | ||
row:tag('td') | row:tag('td') | ||
Revision as of 16:30, 22 April 2014
Documentation for this module may be created at Module:Image array/sandbox/doc
-- implements [[template:image array]]
local p = {}
local function isnotempty(s)
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function renderArrayCell( img, c, a, l, tc, t, w, h)
local alt, link, text, endstr, border
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
return mw.ustring.format('<div style="vertical-align:middle; width:%dpx; height:%dpx; margin-left:auto; margin-right:auto;">'
..'[[File:%s|%dx%dpx|%s|%s|%s]]</div>'
..'<div style="vertical-align:middle; padding:1px;">%s</div>',
w, h, img, w, h, alt, link, text, c)
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 fs = args['font-size'] or '88%'
local text = args['text'] or ''
local border = ( bw > 0 ) and tostring(bw) .. 'px #aaa solid' or nil
-- 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)
-- compute the number of rows
local rowcount = math.ceil(imagecount / perrow)
-- start table
root = mw.html.create('table')
root
:css('border-collapse','collapse')
:css('text-align','center')
:css('font-size', fs)
:css('line-height','1.25em')
:css('margin','auto')
:css('width', tostring(width*perrow) .. 'px')
-- loop over the images
for j = 1, rowcount do
local row = mw.html.create('')
for k = 1, perrow do
i = imagenums[(j-1)*perrow + k] or 0
if( i > 0 ) then
row:tag('td')
:css('width', width .. 'px')
:css(border and 'border' or '', border or '')
:wikitext(renderArrayCell(
args['image' .. i], args['caption' .. i] or '', args['alt' .. i] or '',
args['link' .. i] or '', args['text'] or '', args['text' .. i] or '',
width, height))
else
row:tag('td')
:css('width', width .. 'px')
:css(border and 'border' or '', border or '')
end
end
root:tag('tr')
:css('vertical-align', 'top')
:wikitext(tostring(row))
end
-- end table
return tostring(root)
end
function p.array( frame )
return imagearray( frame )
end
return p