Documentation for this module may be created at Module:FSPS street gallery/doc

local p = {}

p.main = function ( frame )
	local args = frame.args
	if #args == 0 then
		-- Weird hack to get the parent args.
		for i,a in pairs( mw.getCurrentFrame():getParent().args ) do
			args[i] = a
		end
	end
	if #args == 0 then
		return "Error: no files listed"
	end
	local lines = mw.text.split( table.concat( args, "|" ), "\n" )
	local out = ""
	for _,line in pairs( lines ) do
		local parts = mw.text.split( line, "|" )

		-- Filename (strip 'File:' prefix if it's there.)
		local filename = parts[1]
		if string.ulower( string.sub( filename, 1, 5 ) ) == 'file:' then
			filename = string.sub( filename, 6 )
		end

		-- Label/link. Default to filename without prefix.
		local label = ""
		if parts[2] ~= nil and parts[2] ~= "" then
			label = parts[2]
		elseif string.sub( filename, 1, 5 ) == 'FSPS ' then
			label = string.sub( filename, 6 )
		end

		-- Description. Merge parts 3 and beyond.
		local desc = ""
		if parts[3] ~= nil and parts[3] ~= "" then
			desc = parts[3]
			if parts[4] ~= nil and parts[4] ~= "" then
				desc = desc .. "|" .. parts[4]
			end
			if parts[5] ~= nil and parts[5] ~= "" then
				desc = desc .. "|" .. parts[5]
			end
		end

		if filename ~= "" then
			out = out .. "<span class='mdl-fsps-street-gallery-item'>"
				.. "[[File:" .. filename .. "|250x250px|link=]]"
			if label ~= "" then
				local buildingTitle = mw.title.new( label )
				if buildingTitle and #lines < 20 and not buildingTitle.exists then
					out = out .. "<br>[" .. buildingTitle:fullUrl( "action=edit&preload=Template:Building/preload" ) .. " " .. label .. "]"
				else
					out = out .. "<br>[[" .. label .. "]]"
				end
			end
			if desc ~= "" then
				out = out .. "<br>" .. desc
			end
			-- Add link to file
			if filename ~= 'Empty.png' then
				out = out .. ' <small>([[:File:' .. filename .. '|File]])</small>'
			end
			out = out .. "</span>"
		end
	end

	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:FSPS street gallery/styles.css' } )
	return styles .. "<div class='mdl-fsps-street-gallery'>" .. out .. "</div>"
end

-- =p.main({args={"File:Empty.jpg\nFSPS bar.png\nFile:baz.png", "baz label\nFSPS new file.png"}})
return p