Module:Gallery

From Predecessor Wiki
Jump to navigation Jump to search
Template-noinfo.svg Documentation
This module has no documentation. If you know how to use this module, please add some. Module:Gallery/doc

local getArgs = require('Module:Arguments').getArgs
local builder = require("Module:SimpleHTMLBuilder")
local p = {}
local root

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	-- Get width and height from named parameters
	local width = args['width'] or '100'
	local height = args['height'] or '100'
	-- Remove width and height from args to avoid processing them as images
	args['width'] = nil
	args['height'] = nil
	
	local gallery = builder.create('div'):addClass('template-gallery')
	
	for key, value in pairs(args) do
		if key ~= nil then
			local content = args[key]
			local caption = ''
	
			-- Split content on the delimiter ";" to get the filename and alt text
			local parts = mw.text.split(content, ';')
			local filename = parts[1]
			if #parts > 1 then
				caption = parts[2]
			end
			-- Check if arg is a valid file name
			if filename:match('.[^{}<>]+%.%w+') then
				content = '[[File:' .. filename .. '|' .. width .. 'x' .. height .. 'px]]'
			end
			
			gallery
				:tag('div')
					:addClass('template-gallery__item')
					:tag('div')
						:addClass('template-gallery__image')
						:cssText('height:' .. height .. 'px')
						:cssText('width:' .. width .. 'px')
						:wikitext(content)
						:done()
					:tag('div')
						:addClass('template-gallery__caption')
						:wikitext(caption)
						:done()
					:done()
		end
	end
	return gallery:allDone()
end

return p