Modul:ImageCollection/maintenance

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:ImageCollection/maintenance/Doku erstellt werden

-- main module
local ImageCollection = {}

-- export table
local p = {}

function loadModule()
	-- loads the main ImageCollection module
	-- returns:
	--   (bool) loading successful
	
	local m       -- module handle
	local success -- laoding success
	
	-- load module
	success, m = pcall( require, "Module:ImageCollection" )
	if type( m ) == "table" then
	    ImageCollection = m()
	end
	
	-- return success
	return success
end

function colTableRowCount( size )
	-- calculates the required row count to fit size in three cols
	-- parameters:
	--   size: (integer) row count
	-- returns:
	--   (table) row count by col
	
	local rc   -- row count
	local base -- base row count
	local add  -- additional row count
	
	-- only use one row if less then 5 items
	if size < 5 then
		return { size, 0, 0 }
	end
	
	-- calc row count
	base = math.floor( size / 3 )
	add  = size % 3

	-- assign counts
	rc = {base, base, base}
	if add >= 1 then
		rc[1] = rc[1] + 1
	end
	if add == 2 then
		rc[2] = rc[2] + 1
	end
	
	-- return
	return rc
end

function colTable()
	-- return all defined images as table in multiple cols
	-- returns:
	--   (string) overview table wikisyntax
	
	local imgs      -- images assigned to group
	local size      -- image group size
	local colsizes  -- size of cols
	local i         -- increment
	local j         -- increment
	local currgroup -- current group
	local currimg   -- current image info
	local key       -- image key
	local text      -- cell text
	local hmain     -- html main element
	local htable    -- html table element
	local hhead     -- html table header
	local hrow      -- html table row
	
	-- assign images to groups
	imgs = {}
	for key, img in pairs( ImageCollection.data ) do
		currgroup =	img.group or ImageCollection.config.defaults.group or "-"

		-- add group if necessary
		if not imgs[ currgroup ] then
			imgs[ currgroup ] = {}
		end

		-- add image to group, skip errors
		if not img.error then
			table.insert( imgs[ currgroup ], key )
		end
	end

	-- create main output
	hmain = mw.html.create( "div" )
	
	-- create table header with cells
	hhead = mw.html.create( "tr" )
	for i = 1, 2 do
		hhead:node(
			mw.html.create( "th" ):wikitext( ImageCollection.config.i18n.colTableHeaderText[ i ] )
		)
	end
	
	-- output groups
	for _, group in ipairs( ImageCollection.groups.order ) do
		currgroup = imgs[ group ]
		if currgroup then
			table.sort( currgroup )
			size = #currgroup
		    i    = 1
			if size > 0 then
				-- create group title
				hmain:node(
					mw.html.create( "h3" ):wikitext( ImageCollection.groups.title[ group ] )
				)
				-- create cols
				colsizes = colTableRowCount( size )
				for _, rowcount in ipairs( colsizes ) do
					if rowcount > 0 then
						-- create table
						htable = mw.html.create( "table" )
							:addClass( "wikitable" )
							:cssText( "float:left; margin-right:1em;" )
							:node( hhead )
						
						-- loop through group
						j = 1
						repeat
							-- get current image info
							key     = imgs[ group ][ i ]
							currimg = ImageCollection.data[ key ]
							
							-- create row
							hrow = mw.html.create( "tr" )							
							
							-- image
							if currimg.alias then
								text = ""
							else
								text = ImageCollection.invokeImage( key, currimg, "20px" )
							end
							hrow:node( mw.html.create( "td" ):wikitext( text ) )
							
							-- description
							if currimg.alias then
								text = "<small>" .. key .. "</small> → " .. currimg.alias
							else
								text = key
							end
							hrow:node( mw.html.create( "td" ):wikitext( text ) )
							
							-- add row to table
							htable:node( hrow )
	
							i = i + 1
							j = j + 1
						until j > rowcount or i > size
						
						-- add table to main
						hmain:node( htable )
					end
				end
				
				-- add clearer to main
				hmain:node( mw.html.create( "div" ):cssText( "clear: both;" ) )
			end
		end
	end

	-- return
	return tostring( hmain )
end

p.inventory = function( frame )
	-- return set of defined images
	-- parameters:
	--   frame: (table) wiki environment frame
	-- returns:
	--   (string) overview wikisyntax
	
	local success -- loading success
	local error   -- loading error
	local format  -- output format
	
	-- load module and exit if error
	success = loadModule()
	if not success then
		return "<span class=\"error\">module call failed</span>"
	end
	
	-- load config and exit if error
	success, error = ImageCollection.loadConfigFromTemplate( frame )
	if success == false then
		return error
	end
	
	-- get output format
	format = frame.args.format or "colTable"
	
	-- switch output format
	if format == "colTable" then
		return colTable()
	end
	
end

return p