Documentation for this module may be created at Module:Item files gallery/doc

local p = {}

p.main = function ( frame )
	local args = frame.args
	if mw.getCurrentFrame():getParent() ~= nil then
		-- Weird hack to get the parent args.
		for i,a in pairs( mw.getCurrentFrame():getParent().args ) do
			args[i] = a
		end
	end
	local parentItem = mw.text.trim( args[ 1 ] )
	table.remove( args, 1 )
	local out = ''
	local p = 1
	while args[ p ] ~= nil do
		local filename = args[ p ]
		local itemPartNum = mw.text.trim( args[ p + 1 ] )
		local itemPartTitle = mw.title.new( parentItem .. '-' .. itemPartNum )
		if not itemPartTitle.exists then
			local caption = ''
			if itemPartNum ~= '' then
				local formEdit = mw.title.new( 'Special:FormEdit/item/' .. itemPartTitle.text )
				local params = {}
				params[ 'item[parent_item]' ] = parentItem
				params[ 'item[image]' ] = filename
				params[ 'item[original_order]' ] = itemPartNum
				local url = formEdit:canonicalUrl( params )
				caption = itemPartTitle.text .. ' ([' .. url .. ' create])'
			end
			out = out .. filename .. '|' .. caption .. '\n'
		else
			out = out .. filename .. '|link=' .. itemPartTitle.text .. '|[[' .. itemPartTitle.text .. ']]\n'
		end
		p = p + 2
	end
	-- =p.main({args={'PREF\n', 'File:Foo.jpg','3', 'File:Lorem ipsum','4\n'}})
	-- return out
	return mw.getCurrentFrame():extensionTag( 'gallery', out, { mode = 'traditional' } )
end

return p