Difference between revisions of "Module:Image array/sandbox"
Jump to navigation
Jump to search
blackwiki>Jackmcbarn (sync) |
blackwiki>Jackmcbarn (begin simplifying) |
||
| Line 33: | Line 33: | ||
end | end | ||
| − | + | function p.array( frame ) | |
local args = frame:getParent().args | local args = frame:getParent().args | ||
| − | local width = tonumber(args | + | local width = tonumber(args.width or '60') |
| − | local height = tonumber(args | + | local height = tonumber(args.height or '70') |
| − | local perrow = tonumber(args | + | local perrow = tonumber(args.perrow or '4') |
| − | local bw = tonumber(args | + | local bw = tonumber(args.border-width or '0') |
local fs = args['font-size'] or '88%' | local fs = args['font-size'] or '88%' | ||
| − | local text = args | + | local text = args.text or '' |
| − | local margin = args | + | local margin = args.margin or 'auto' |
| − | local border = ( bw > 0 ) and tostring(bw) .. 'px #aaa solid' | + | local border = ( bw > 0 ) and tostring(bw) .. 'px #aaa solid' |
-- find all the nonempty image numbers | -- find all the nonempty image numbers | ||
local imagenums = {} | local imagenums = {} | ||
| − | |||
for k, v in pairs( args ) do | for k, v in pairs( args ) do | ||
| − | local i = tonumber( | + | local i = tonumber(string.match( k, '^%s*image([%d]+)%s*$' )) |
| − | if( i | + | if( i and isnotempty(v) ) then |
table.insert( imagenums, i ) | table.insert( imagenums, i ) | ||
| − | |||
end | end | ||
end | end | ||
| Line 58: | Line 56: | ||
-- compute the number of rows | -- compute the number of rows | ||
| − | local rowcount = math.ceil( | + | local rowcount = math.ceil(#imagenums / perrow) |
-- start table | -- start table | ||
root = mw.html.create('table') | root = mw.html.create('table') | ||
| − | + | :addClass(args.class) | |
| − | :addClass(args | ||
:css('border-collapse','collapse') | :css('border-collapse','collapse') | ||
:css('text-align','center') | :css('text-align','center') | ||
| Line 74: | Line 71: | ||
for j = 1, rowcount do | for j = 1, rowcount do | ||
local row = root:tag('tr') | local row = root:tag('tr') | ||
| − | + | css('vertical-align', 'top') | |
for k = 1, perrow do | for k = 1, perrow do | ||
i = imagenums[(j-1)*perrow + k] or 0 | i = imagenums[(j-1)*perrow + k] or 0 | ||
| Line 91: | Line 88: | ||
-- end table | -- end table | ||
return tostring(root) | return tostring(root) | ||
| − | |||
| − | |||
| − | |||
end | end | ||
return p | return p | ||
Revision as of 15:43, 18 March 2016
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, b, l, tc, t, w, h)
local alt = isnotempty(a) and ('|alt=' .. a) or ''
local link = isnotempty(l) and ('|link=' .. l) or ''
local text = (isnotempty(tc) and not isnotempty(t))
and mw.text.unstrip(c) or mw.text.unstrip(t or '')
local border = isnotempty(b) and '|border' or ''
local cell = mw.html.create('')
if( img ) then
cell:tag('div')
:css('display', 'table-cell')
:css('vertical-align', 'middle')
:css('width', w .. 'px')
:css('height', h .. 'px')
:css('margin-left', 'auto')
:css('margin-right', 'auto')
:wikitext(mw.ustring.format('[[File:%s|%dx%dpx%s|%s]]',
img, w, h, alt .. link .. border, text))
cell:tag('div')
:css('padding', '1px')
:wikitext(c)
end
return tostring(cell)
end
function p.array( 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 margin = args.margin or 'auto'
local border = ( bw > 0 ) and tostring(bw) .. 'px #aaa solid'
-- find all the nonempty image numbers
local imagenums = {}
for k, v in pairs( args ) do
local i = tonumber(string.match( k, '^%s*image([%d]+)%s*$' ))
if( i and isnotempty(v) ) then
table.insert( imagenums, i )
end
end
-- sort the image numbers
table.sort(imagenums)
-- compute the number of rows
local rowcount = math.ceil(#imagenums / perrow)
-- start table
root = mw.html.create('table')
:addClass(args.class)
:css('border-collapse','collapse')
:css('text-align','center')
:css('font-size', fs)
:css('line-height','1.25em')
:css('margin',margin)
:css('width', tostring(width*perrow) .. 'px')
-- loop over the images
for j = 1, rowcount do
local row = root:tag('tr')
css('vertical-align', 'top')
for k = 1, perrow do
i = imagenums[(j-1)*perrow + k] or 0
row:tag('td')
:css('width', width .. 'px')
:css('text-align', 'center')
:css(border and 'border' or '', border or '')
:wikitext(renderArrayCell(
args['image' .. i], args['caption' .. i], args['alt' .. i],
args['border' .. i], args['link' .. i] ,
args['text'], args['text' .. i] , width, height)
)
end
end
-- end table
return tostring(root)
end
return p