Modul:Log

Aus Steel Beasts Wiki
Version vom 24. Juni 2023, 18:01 Uhr von sc>FoXFTW
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

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

local Log = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

local TNT = require( 'Module:Translate' ):new()


--- Creates the actual output, either a HTML comment if options.silent is true, or a mw-message-box
--- @param severity string
--- @param message string
function methodtable.makeOutput( self, severity, message )
	local currentDate = mw.getContentLanguage():formatDate( 'c' )
	if type( message ) ~= 'string' and type( message ) ~= 'number' then
		message = TNT.formatInLanguage( self.options.lang, 'Module:Log/i18n.json', 'msg_content_error' )
	end

	local content

	-- Output as HTML Comment
	if self.options.silent ~= nil then
		content = string.format(
			'[%s] - %s: %s',
			currentDate,
			TNT.formatInLanguage( self.options.lang, 'Module:Log/i18n.json', severity ),
			message
		)
		content = mw.html.create( 'p' )
			:css( 'display', 'none' )
			:wikitext( content )
	else -- Output as HTML
		content = mw.html.create( 'p' )
			:addClass( 'log' )
			:addClass( 'mw-message-box' )
			:addClass( 'mw-message-box-' .. severity )
			:attr( 'title', currentDate )
			:wikitext( message )
	end

	return tostring( content )
end


--- Set the frame and load args
--- @param frame table
function methodtable.setFrame( self, frame )
    self.currentFrame = frame
    self.frameArgs = require( 'Module:Arguments' ).getArgs( frame )
end


--- Template entry
--- @param frame table
function Log.main( frame )
    local instance = Log:new()
    instance:setFrame( frame )

	local severity = string.lower( instance.frameArgs[ 1 ] or instance.frameArgs[ 'Schweregrad' ] or 'info' )
	local message = instance.frameArgs[ 2 ] or instance.frameArgs[ 'Nachricht' ]
	local silent = string.lower( instance.frameArgs[ 3 ] or instance.frameArgs[ 'Versteckt' ] or '' )

	if silent == '1' or silent == 'true' or silent == 'wahr' then
		silent = true
	else
		silent = nil
	end

	instance.options = {
		silent = silent,
		lang = require( 'Module:Template translation' )._getLanguageSubpage( mw.title.getCurrentTitle() )
	}

	return instance:makeOutput( severity, message )
end


--- Output an info log
--- @param message string The message Content
--- @param options table Output options
function Log.info( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'info', message )
end


--- Output a success log
--- @param message string The message Content
--- @param options table Output options
function Log.success( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'success', message )
end


--- Output a warning log
--- @param message string The message Content
--- @param options table Output options
function Log.warning( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'warning', message )
end


--- Output a error log
--- @param message string The message Content
--- @param options table Output options
function Log.error( message, options )
	local instance = Log:new( options )
	return instance:makeOutput( 'error', message )
end


--- New Instance
function Log.new( self, options )
	if options == true then
		options = {
			silent = true
		}
	else
		options = options or {}
	end
	
	if options.lang == nil or #options.lang ~= 2 then
		options.lang = 'de'
	end

    local instance = {
		options = options
	}

    setmetatable( instance, metatable )

    return instance
end


return Log