Jump to content
Wikipedia The Free Encyclopedia

Module:Multiple image

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Warning This Lua module is used on approximately 66,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.

Implements Template:Multiple image.

Can be called directly with {{#invoke:Multiple image||template parameters}} to reduce article post-expand include size.

The above documentation is transcluded from Module:Multiple image/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 -- implements [[template:multiple image]]
 localp={}

 localautoscaledimages
 localnonautoscaledimages

 localfunctionisnotempty(s)
 returnsands:match('^%s*(.-)%s*$')~=''
 end

 localfunctionremovepx(s)
 returntostring(sor''):match('^(.*)[Pp][Xx]%s*$')ors
 end

 localfunctiongetdimensions(s,w,h)
 iftonumber(w)andtonumber(h)then
 nonautoscaledimages=true
 returntonumber(w),tonumber(h)
 end
 localfile=sandmw.title.new('File:'..mw.uri.decode(mw.ustring.gsub(s,'%|.*$',''),'WIKI'))
 file=fileandfile.fileor{width=0,height=0}
 w=tonumber(file.width)or0
 h=tonumber(file.height)or0
 autoscaledimages=true
 returnw,h
 end

 localfunctionrenderImageCell(image,width,height,link,alt,thumbtime,caption,class,textalign,istyle,border)
 localroot=mw.html.create('')

 localaltstr='|alt='..(altor'')
 localclassstr=classand('|class='..class)or''
 locallinkstr=linkand('|link='..link)or''
 localwidthstr='|'..tostring(width)..'px'
 localthumbtimestr=''

 ifwidthstr=='|-nanpx'then
 widthstr=''
 end
 ifisnotempty(thumbtime)then
 thumbtimestr='|thumbtime='..thumbtime
 end

 localimagediv=root:tag('div')
 imagediv:addClass((border~='infobox')and'thumbimage'ornil)
 imagediv:cssText(istyle)
 if(height)then
 imagediv:css('height',tostring(height)..'px')
 imagediv:css('overflow','hidden')
 end
 imagediv:wikitext('[[file:'..image..classstr..widthstr..linkstr..altstr..thumbtimestr..']]')
 ifisnotempty(caption)then
 localcaptiondiv=root:tag('div')
 captiondiv:addClass((border~='infobox')and'thumbcaption'ornil)
 ifisnotempty(textalign)then
 captiondiv:addClass('text-align-'..textalign)
 end
 captiondiv:wikitext(caption)
 end
 returntostring(root)
 end

 localfunctiongetWidth(w1,w2)
 localw
 ifisnotempty(w1)then
 w=tonumber(w1)
 elseifisnotempty(w2)then
 w=tonumber(w2)
 end
 returnwor200
 end

 localfunctiongetPerRow(pstr,ic)
 -- split string into array using any non-digit as a dilimiter
 localpr=mw.text.split(pstror'','[^%d][^%d]*')
 -- 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
 localr=1
 localthisrow=tonumber(pr[1]oric)oric
 localprownum={}
 while(ic>0)do
 prownum[r]=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[r]orthisrow)oric,ic)
 end
 returnprownum
 end

 localfunctionrenderMultipleImages(frame)
 localpargs=frame:getParent().args
 localargs=frame.args
 localwidth=removepx(pargs['width']or'')
 localdir=pargs['direction']or''
 localborder=pargs['border']orargs['border']or''
 localalign=pargs['align']orargs['align']or(border=='infobox'and'center'or'')
 localcapalign=pargs['caption_align']orargs['caption_align']or''
 localtotalwidth=removepx(pargs['total_width']orargs['total_width']or'')
 localimgstyle=pargs['image_style']orargs['image_style']
 localheader=pargs['header']orpargs['title']or''
 localfooter=pargs['footer']or''
 localimagegap=tonumber(pargs['image_gap']or'1')or1
 localperrow=nil
 localthumbclass={
 ["left"]='tleft',
 ["none"]='tnone',
 ["center"]='tnone',
 ["centre"]='tnone',
 ["right"]='tright'
 }

 -- find all the nonempty images
 localimagenumbers={}
 localimagecount=0
 fork,vinpairs(pargs)do
 locali=tonumber(tostring(k):match('^%s*image([%d]+)%s*$')or'0')
 if(i>0andisnotempty(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'orpargs['perrow'],imagecount)

 -- compute the number of rows
 localrowcount=#perrow

 -- store the image widths and compute row widths and maximum row width
 localheights={}
 localwidths={}
 localwidthmax=0
 localwidthsum={}
 localk=0
 forr=1,rowcountdo
 widthsum[r]=0
 forc=1,perrow[r]do
 k=k+1
 if(k<=imagecount)then
 locali=imagenumbers[k]
 if(isnotempty(totalwidth))then
 widths[k],heights[k]=getdimensions(pargs['image'..i],pargs['width'..i],pargs['height'..i])
 else
 widths[k]=getWidth(width,pargs['width'..i])
 end
 widthsum[r]=widthsum[r]+widths[k]
 end
 end
 widthmax=math.max(widthmax,widthsum[r])
 end

 -- make sure the gap is non-negative
 ifimagegap<0thenimagegap=0end

 -- if total_width has been specified, rescale the image widths
 if(isnotempty(totalwidth))then
 totalwidth=tonumber(totalwidth)
 widthmax=0
 localk=0
 forr=1,rowcountdo
 localkoffset=k
 localtw=totalwidth-(3+imagegap)*(perrow[r]-1)-12
 localar={}
 localarsum=0
 forj=1,perrow[r]do
 k=k+1
 if(k<=imagecount)then
 locali=imagenumbers[k]
 localh=heights[k]or0
 if(h>0)then
 ar[j]=widths[k]/h
 heights[k]=h
 else
 ar[j]=widths[k]/100
 end
 arsum=arsum+ar[j]
 end
 end
 localht=tw/arsum
 localws=0
 k=koffset
 forj=1,perrow[r]do
 k=k+1
 if(k<=imagecount)then
 locali=imagenumbers[k]
 widths[k]=math.floor(ar[j]*ht+0.5)
 ws=ws+widths[k]
 ifheights[k]then
 heights[k]=math.floor(ht)
 end
 end
 end
 widthsum[r]=ws
 widthmax=math.max(widthmax,widthsum[r])
 end
 end

 -- start building the array of images, if there are images
 if(imagecount>0)then
 -- compute width of outer div
 localbodywidth=0
 forr=1,rowcountdo
 if(widthmax==widthsum[r])then
 bodywidth=widthmax+(3+imagegap)*(perrow[r]-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);

 localbg=pargs['background color']or''
 -- create the array of images
 localroot=mw.html.create('div')
 root:addClass('thumb')
 root:addClass('tmulti')
 -- root:addClass('tmulti-sandbox')
 root:addClass(thumbclass[align]or'tright')

 if(align=='center'oralign=='centre')then
 root:addClass('center')
 end
 if(bg~='')then
 root:css('background-color',bg)
 end

 localdiv=root:tag('div')
 div:addClass((border~='infobox')and'thumbinner multiimageinner'or'multiimageinner')
 div:css('width',tostring(bodywidth)..'px')
 :css('max-width',tostring(bodywidth)..'px')
 if(bg~='')then
 div:css('background-color',bg)
 end
 if(border=='infobox'orborder=='none')then
 div:css('border','none')
 end
 -- add the header
 if(isnotempty(header))then
 div:tag('div')
 :addClass('trow')
 :tag('div')
 :addClass('theader')
 :css('text-align',pargs['header_align'])
 :css('background-color',
 (pargs['header_background']~='')andpargs['header_background']ornil)
 :wikitext(header)
 end
 -- loop through the images
 localk=0
 forr=1,rowcountdo
 localrowdiv=div:tag('div'):addClass('trow');
 forj=1,perrow[r]do
 k=k+1
 if(k<=imagecount)then
 localimagediv=rowdiv:tag('div')
 imagediv:addClass('tsingle')
 ifbg~=''then
 imagediv:css('background-color',bg);
 end
 ifimagegap>1andk<imagecountthen
 ifdir=='vertical'then
 imagediv:css('margin-bottom',tostring(imagegap)..'px')
 elseifj<perrow[r]then
 imagediv:css('margin-right',tostring(imagegap)..'px')
 end
 end
 locali=imagenumbers[k]
 localimg=pargs['image'..i]
 localw=widths[k]
 imagediv:css('width',tostring(2+w)..'px')
 :css('max-width',tostring(2+w)..'px')
 imagediv:wikitext(renderImageCell(img,w,heights[k],
 pargs['link'..i],pargs['alt'..i],
 pargs['thumbtime'..i],pargs['caption'..i],pargs['class'..i],capalign,imgstyle,border))
 end
 end
 end
 -- add the footer
 if(isnotempty(footer))then
 localfalign=string.lower(pargs['footer_align']orargs['footer_align']or'')
 falign=(falign=='centre')and'center'orfalign
 div:tag('div')
 :addClass('trow')
 :css('display',(falign~='')and'flow-root'or'flex')
 :tag('div')
 :addClass((border~='infobox')and'thumbcaption'ornil)
 :css('text-align',(falign~='')andfalignornil)
 :css('background-color',
 (pargs['footer_background']~='')andpargs['footer_background']ornil)
 :wikitext(footer)
 end
 returntostring(root)
 end
 return''
 end

 functionp.render(frame)
 autoscaledimages=false
 nonautoscaledimages=false

 returnframe:extensionTag{name='templatestyles',args={src='Multiple image/styles.css',wrapper=".tmulti"}}
 ..renderMultipleImages(frame)
 ..(autoscaledimagesand'[[Category:Pages using multiple image with auto scaled images]]'or'')
 ..(nonautoscaledimagesand'[[Category:Pages using multiple image with manual scaled images]]'or'')
 end

 p['']=function(frame)returnp.render(frame:newChild{title=frame:getTitle()})end

 returnp

AltStyle によって変換されたページ (->オリジナル) /