Jump to content

Module:Multiple image

Permanently protected module
From Wikipedia, the free encyclopedia

-- implements [[template:multiple image]]localp={}localautoscaledimageslocalnonautoscaledimageslocalfunctionisnotempty(s)returnsands:match('^%s*(.-)%s*$')~=''endlocalfunctionremovepx(s)returntostring(sor''):match('^(.*)[Pp][Xx]%s*$')orsendlocalfunctiongetdimensions(s,w,h)iftonumber(w)andtonumber(h)thennonautoscaledimages=truereturntonumber(w),tonumber(h)endlocalfile=sandmw.title.new('File:'..mw.uri.decode(mw.ustring.gsub(s,'%|.*$',''),'WIKI'))file=fileandfile.fileor{width=0,height=0}w=tonumber(file.width)or0h=tonumber(file.height)or0autoscaledimages=truereturnw,hendlocalfunctionrenderImageCell(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'thenwidthstr=''endifisnotempty(thumbtime)thenthumbtimestr='|thumbtime='..thumbtimeendlocalimagediv=root:tag('div')imagediv:addClass((border~='infobox')and'thumbimage'ornil)imagediv:cssText(istyle)if(height)thenimagediv:css('height',tostring(height)..'px')imagediv:css('overflow','hidden')endimagediv:wikitext('[[file:'..image..classstr..widthstr..linkstr..altstr..thumbtimestr..']]')ifisnotempty(caption)thenlocalcaptiondiv=root:tag('div')captiondiv:addClass((border~='infobox')and'thumbcaption'ornil)ifisnotempty(textalign)thencaptiondiv:addClass('text-align-'..textalign)endcaptiondiv:wikitext(caption)endreturntostring(root)endlocalfunctiongetWidth(w1,w2)localwifisnotempty(w1)thenw=tonumber(w1)elseifisnotempty(w2)thenw=tonumber(w2)endreturnwor200endlocalfunctiongetPerRow(pstr,ic)-- split string into array using any non-digit as a dilimiterlocalpr=mw.text.split(pstror'','[^%d][^%d]*')-- if split failed, assume a single rowif(#pr<1)thenpr={tostring(ic)}end-- convert the array of strings to an array of numbers,-- adding any implied/missing numbers at the end of the arraylocalr=1localthisrow=tonumber(pr[1]oric)oriclocalprownum={}while(ic>0)doprownum[r]=thisrowic=ic-thisrowr=r+1-- use the previous if the next is missing and-- make sure we don't overstep the number of imagesthisrow=math.min(tonumber(pr[r]orthisrow)oric,ic)endreturnprownumendlocalfunctionrenderMultipleImages(frame)localpargs=frame:getParent().argslocalargs=frame.argslocalwidth=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')or1localperrow=nillocalthumbclass={["left"]='tleft',["none"]='tnone',["center"]='tnone',["centre"]='tnone',["right"]='tright'}-- find all the nonempty imageslocalimagenumbers={}localimagecount=0fork,vinpairs(pargs)dolocali=tonumber(tostring(k):match('^%s*image([%d]+)%s*$')or'0')if(i>0andisnotempty(v))thentable.insert(imagenumbers,i)imagecount=imagecount+1endend-- sort the imagenumberstable.sort(imagenumbers)-- create an array with the number of images per rowperrow=getPerRow(dir=='vertical'and'1'orpargs['perrow'],imagecount)-- compute the number of rowslocalrowcount=#perrow-- store the image widths and compute row widths and maximum row widthlocalheights={}localwidths={}localwidthmax=0localwidthsum={}localk=0forr=1,rowcountdowidthsum[r]=0forc=1,perrow[r]dok=k+1if(k<=imagecount)thenlocali=imagenumbers[k]if(isnotempty(totalwidth))thenwidths[k],heights[k]=getdimensions(pargs['image'..i],pargs['width'..i],pargs['height'..i])elsewidths[k]=getWidth(width,pargs['width'..i])endwidthsum[r]=widthsum[r]+widths[k]endendwidthmax=math.max(widthmax,widthsum[r])end-- make sure the gap is non-negativeifimagegap<0thenimagegap=0end-- if total_width has been specified, rescale the image widthsif(isnotempty(totalwidth))thentotalwidth=tonumber(totalwidth)widthmax=0localk=0forr=1,rowcountdolocalkoffset=klocaltw=totalwidth-(3+imagegap)*(perrow[r]-1)-12localar={}localarsum=0forj=1,perrow[r]dok=k+1if(k<=imagecount)thenlocali=imagenumbers[k]localh=heights[k]or0if(h>0)thenar[j]=widths[k]/hheights[k]=helsear[j]=widths[k]/100endarsum=arsum+ar[j]endendlocalht=tw/arsumlocalws=0k=koffsetforj=1,perrow[r]dok=k+1if(k<=imagecount)thenlocali=imagenumbers[k]widths[k]=math.floor(ar[j]*ht+0.5)ws=ws+widths[k]ifheights[k]thenheights[k]=math.floor(ht)endendendwidthsum[r]=wswidthmax=math.max(widthmax,widthsum[r])endend-- start building the array of images, if there are imagesif(imagecount>0)then-- compute width of outer divlocalbodywidth=0forr=1,rowcountdoif(widthmax==widthsum[r])thenbodywidth=widthmax+(3+imagegap)*(perrow[r]-1)+12endend-- The body has a min-width of 100, which needs to be taken into account on specific widthsbodywidth=math.max(100,bodywidth-8);localbg=pargs['background color']or''-- create the array of imageslocalroot=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')thenroot:addClass('center')endif(bg~='')thenroot:css('background-color',bg)endlocaldiv=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~='')thendiv:css('background-color',bg)endif(border=='infobox'orborder=='none')thendiv:css('border','none')end-- add the headerif(isnotempty(header))thendiv: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 imageslocalk=0forr=1,rowcountdolocalrowdiv=div:tag('div'):addClass('trow');forj=1,perrow[r]dok=k+1if(k<=imagecount)thenlocalimagediv=rowdiv:tag('div')imagediv:addClass('tsingle')ifbg~=''thenimagediv:css('background-color',bg);endifimagegap>1andk<imagecountthenifdir=='vertical'thenimagediv:css('margin-bottom',tostring(imagegap)..'px')elseifj<perrow[r]thenimagediv:css('margin-right',tostring(imagegap)..'px')endendlocali=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))endendend-- add the footerif(isnotempty(footer))thenlocalfalign=string.lower(pargs['footer_align']orargs['footer_align']or'')falign=(falign=='centre')and'center'orfaligndiv: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)endreturntostring(root)endreturn''endfunctionp.render(frame)autoscaledimages=falsenonautoscaledimages=falsereturnframe: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'')endp['']=function(frame)returnp.render(frame:newChild{title=frame:getTitle()})endreturnp
close