Modul:ImageCollection

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


--[[
 invoke images from keys controlled by config page
]]

local ImageCollection = {
	config = {},
	groups = {},
	data   = {},
	serial = "2024-10-16",
	item   = 0
}

-- failsafe option 
local Failsafe = ImageCollection

-- export table
local p = {}

ImageCollection.formatError = function ( message, errtype )
	-- format error message
	-- parameters:
	--   errtype: (string) error type
	-- returns:
	--   (string) formatted error message

	local html       -- html error object
	local err        -- error type info
	local text = ""  -- message text

	-- default error type
	errtype = errtype or "default"

	-- get error message
	err = ImageCollection.config.error[ errtype ] or ImageCollection.config.error.default
	
	-- get error text
	if type( err.text ) == "string" then
		text = text .. string.format( err.text, "[[" .. ImageCollection.config.source .. "]]", message )
	end
	if type( err.category ) == "string" then
		text = text .. "[[Category:" .. err.category .. "]]"
	end
	
	-- return nothing if no text set
	if text == "" then
		return ""
	end

	-- create and format error message wrapper
	html = mw.html.create( "span" )
		:wikitext( text )
		:addClass( "error" )
	if type( err.class ) == "string" then
		html:addClass( err.class )
	end
	if type( err.style ) == "string" then
		html:cssText( err.style )
	end
	
	-- return
	return tostring( html )
end

ImageCollection.loadConfigJson = function ( page )
	-- load config from json page
	-- parameters:
	--   page: (string) path to config json page
	-- returns:
	--   (bool) true or false by success/fault
	
	local success, c = pcall( mw.loadJsonData, page )
	if type( c ) == "table" then
		if type( c.config ) == "table" then
			ImageCollection.config = c.config
		end
		if type( c.groups ) == "table" then
			ImageCollection.groups = c.groups
		end
		if type( c.data ) == "table" then
			ImageCollection.data = c.data
		end
	end
	
	return success
end

ImageCollection.loadConfigFromTemplate = function ( frame )
	-- load configuration
	-- parameters:
	--   frame: (table) wiki environment frame
	-- returns:
	--   (bool, string) loading successful, error message if needed

	local args    -- arguments of template call
	local cnf     -- configuration page name
	local html    -- html error object
	local r1, r2  -- return values
	
	-- get module parameters
	args = frame.args
	cnf  = args[1]
	if cnf then
		cnf = mw.text.trim( cnf )
		if cnf == "" then
			cnf = false
		end
	end
	if cnf then
		-- load config
		r1 = ImageCollection.loadConfigJson( cnf )
		if r1 then
			r2 = ""
		else
			html = mw.html.create( "span" )
				:addClass( "error" )
				:wikitext( "data call failed" )
			r2 = tostring( html )
		end
	else
		html = mw.html.create( "span" )
			:addClass( "error" )
			:wikitext( "config page missing" )
		r1 = false
		r2 = tostring( html )
	end
	
	return r1, r2
end

ImageCollection.getImageInfo = function ( key )
	-- get the data element for the given key
	-- parameters:
	--   key:  (string) image key
	-- returns:
	--   (bool, table, string) element found, element, key
	
	local img -- image table
	
	img = ImageCollection.data[ key ]
	
	-- invalid key
	if type( img ) ~= "table" then
		return false, {}, key
	end
	
	-- follow alias
	if type( img.alias ) == "string" then
		key = img.alias
		return ImageCollection.getImageInfo( key )
	end
	
	-- valid key
	return true, img, key
end

ImageCollection.invokeImage = function ( key, img, size )
	-- get invocation string for image
	-- parameters:
	--   key:  (string) image key
	--   img:  (string) image info
	--   size: (string) image size
	-- returns:
	--   (string) image invocation wikisytax string
	
	local r      -- return string
	local opt    -- image options
	local alt    -- image alt text
	local link   -- image link
	local invert -- invert image for dark mode
	local class  -- image class
	local desc   -- image description
	
	-- file
	r = "[[File:" .. img.file
	
	-- options
	opt = img.options or ImageCollection.config.defaults.options or nil
	if type( opt ) == "string" and opt ~= "" then
		r = r .. "|" .. opt
	end

	-- size
	if size == "" then
		if type( img.size ) == "string" then
			size = img.size
		else
			size = ImageCollection.config.defaults.size
		end
	end
	r = r .. "|" .. size

	-- alt text
	alt = img.alt or ImageCollection.config.defaults.alt or nil
	if type( alt ) == "string" then
		r = r .. "|alt=" .. alt
	end
	
	-- link
	link = img.link or ImageCollection.config.defaults.link or img.name or key
	if link ~= "@DEFAULT" then
		r = r .. "|link=" .. link
	end
	
	-- invert
	if type( img.invert ) == "boolean" then
		invert = img.invert
	else
		invert = ImageCollection.config.defaults.invert or false
	end
	if invert == true then
		class = "skin-invert-image "
	else
		class = ""
	end
	
	-- class
	class = mw.text.trim( class .. ( img.class or ImageCollection.config.defaults.class or "" ) )
	if class ~= "" then
		r = r .. "|class=" .. class
	end

	-- description
	desc = img.name or ImageCollection.config.defaults.name or key
	r = r .. "|" .. desc
	
	-- finish and return invocation
	r = r .. "]]"
	return r
end

ImageCollection.getImage = function ( key, size )
	-- get image for key in given size
	-- parameters:
	--   key:  (string) image key
	--   size: (string) image size
	-- returns:
	--   (string) image invocation wikisytax string
	
	local found -- key found
	local img   -- image table

	-- get image data and resolve alias
	found, img, key = ImageCollection.getImageInfo( key )
	if found == false then
		return ImageCollection.formatError( string.format( ImageCollection.config.i18n.errorKeyNotFound or "", key ), "keynotfound" )
	end
	
	-- error
	if type( img.error ) == "string" then
		return ImageCollection.formatError( img.error, "manual" )
	end
	
	-- create image invocation
	return ImageCollection.invokeImage( key, img, size )

end

p.f = function ( frame )
	-- return image, access from templates
	-- parameters:
	--   frame: (table) wiki environment frame
	-- returns:
	--   (string) image invocation wikisytax string
	
	local success -- success in config load
	local error   -- error message from config load
	local args    -- arguments of template call
	local key     -- image key
	local size    -- image size
	
	-- load config
	success, error = ImageCollection.loadConfigFromTemplate( frame )
	if success == false then
		return error
	end
	
	-- get template parameters
	args = frame:getParent().args
	key  = mw.text.trim( args[1] or "" )
	size = mw.text.trim( args[2] or "" )
	
	-- call main and return
	return ImageCollection.getImage( key, size )
end

setmetatable( p,  { __call = function ( func, ... )
                                 setmetatable( p, nil );
                                 return Failsafe;
                             end } );

return p