Module:Multiple image: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
end | end | ||
local function renderImageCell(image, width, link, alt, caption) | local function renderImageCell(image, width, link, alt, caption, textalign) | ||
local root = mw.html.create('') | local root = mw.html.create('') | ||
| Line 19: | Line 19: | ||
captiondiv:addClass('thumbcaption') | captiondiv:addClass('thumbcaption') | ||
captiondiv:css('clear', 'left') | captiondiv:css('clear', 'left') | ||
if isnotempty(textalign) then | |||
captiondiv:css('text-align', textalign) | |||
end | |||
captiondiv:wikitext(caption) | captiondiv:wikitext(caption) | ||
end | end | ||
| Line 42: | Line 45: | ||
local dir = args['direction'] or '' | local dir = args['direction'] or '' | ||
local align = args['align'] or '' | local align = args['align'] or '' | ||
local textalign = args['text_align'] or args['text-align'] or '' | |||
local thumbclass = { | local thumbclass = { | ||
["left"] = 'tleft', | ["left"] = 'tleft', | ||
| Line 114: | Line 118: | ||
local w = getWidth(width, args['width' .. i], defaultwidth) | local w = getWidth(width, args['width' .. i], defaultwidth) | ||
imagediv:css('width', tostring(2 + w) .. 'px') | imagediv:css('width', tostring(2 + w) .. 'px') | ||
imagediv:wikitext(renderImageCell(img, w, args['link' .. i], args['alt' .. i], args['caption' .. i])) | imagediv:wikitext(renderImageCell(img, w, args['link' .. i], args['alt' .. i], args['caption' .. i], textalign)) | ||
end | end | ||
-- add the footer | -- add the footer | ||
Revision as of 19:24, 23 April 2014
Documentation for this module may be created at Module:Multiple image/doc
-- implements [[template:multiple image]]
local p = {}
local function isnotempty(s)
return s and s:match( '^%s*(.-)%s*$' ) ~= ''
end
local function renderImageCell(image, width, link, alt, caption, textalign)
local root = mw.html.create('')
local altstr = '|alt=' .. (alt or '')
local linkstr = link and ('|link=' .. link) or ''
local imagediv = root:tag('div')
imagediv:addClass('thumbimage')
imagediv:wikitext('[[file:' .. image .. '|' .. tostring(width) .. 'px' .. 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(w0, w1, w2)
local w = 0
if isnotempty(w0) then
w = tonumber(w0)
elseif isnotempty(w1) then
w = tonumber(w1)
else
w = tonumber(w2)
end
return w
end
local function renderMultipleImages(frame)
local args = frame:getParent().args
local defaultwidth = '200'
local width = args['width'] or ''
local dir = args['direction'] or ''
local align = args['align'] or ''
local textalign = args['text_align'] or args['text-align'] or ''
local thumbclass = {
["left"] = 'tleft',
["none"] = 'tnone',
["center"] = 'tnone',
["centre"] = 'tnone',
["right"] = 'tright'
}
-- find all the nonempty images and corresponding widths
-- also compute the sum of widths and maximum width
local imagenumbers = {}
local imagecount = 0
local widthmax = 0
local widthsum = 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
local w = getWidth(width, args['width' .. i], defaultwidth)
widthmax = math.max(widthmax, w)
widthsum = widthsum + w
table.insert( imagenumbers, i)
imagecount = imagecount + 1
end
end
-- sort the imagenumbers
table.sort(imagenumbers)
if( imagecount > 0 ) then
local bodywidth = 0
local bg = args['background color'] or ''
-- create the array of images
local root = mw.html.create('div')
root:addClass('thumb')
root:addClass(thumbclass[align] or 'tright')
if( dir == 'vertical') then
bodywidth = widthmax + 12
else
bodywidth = widthsum + 4 * (imagecount - 1) + 12
end
-- root:css('width', tostring(bodywidth) .. 'px')
if( align == 'center' or align == 'centre' ) then
root:css('margin', '0 auto')
end
if( bg ~= '' ) then
root:css('background-color', bg)
end
local div = root:tag('div')
div:addClass('thumbinner')
div:css('width', tostring(bodywidth - 8) .. 'px')
if( bg ~= '' ) then
div:css('background-color', bg)
end
-- add the header
if( isnotempty(args['header']) ) then
div:tag('div')
:css('clear', 'both')
:css('font-weight', 'bold')
:css('text-align', args['header_align'] or 'center')
:css('background-color', args['header_background'] or 'transparent')
:wikitext(args['header'])
end
-- loop through the images
for k=1,imagecount do
imagediv = div:tag('div')
if dir ~= 'vertical' then
imagediv:css('float', 'left')
end
imagediv:css('margin', '1px')
local i = imagenumbers[k]
local img = args['image' .. i]
local w = getWidth(width, args['width' .. i], defaultwidth)
imagediv:css('width', tostring(2 + w) .. 'px')
imagediv:wikitext(renderImageCell(img, w, args['link' .. i], args['alt' .. i], args['caption' .. i], textalign))
end
-- add the footer
if( isnotempty(args['footer']) ) then
class="thumbcaption" style="clear: left; text-align: {{{footer_align|left}}}; background: {{{footer_background|transparent}}}"
div:tag('div')
:addClass('thumbcaption')
:css('clear', 'left')
:css('text-align', args['footer_align'] or 'left')
:css('background-color', args['footer_background'] or 'transparent')
:wikitext(args['footer'])
end
return tostring(root)
end
return ''
end
function p.render( frame )
return renderMultipleImages( frame )
end
return p