Difference between revisions of "Module:Reporter"

From Yugipedia
Jump to: navigation, search
(A less flexible, but more standardized version of InfoWrapper. This is probably better for consistency and code re-use.)
 
(Restoring revision 4609162 by User:Becasita on 2020-12-29 01:11:35. "Method to allow changing when categories can be dumped.")
 
Line 8: Line 8:
  
 
local mwHtmlCreate = mw.html.create
 
local mwHtmlCreate = mw.html.create
 +
 +
local namespace = mw.title.getCurrentTitle().namespace
 +
 +
local canDumpCategories = (
 +
namespace ~= 10
 +
and
 +
namespace % 2 == 0
 +
)
  
 
--[[Doc
 
--[[Doc
Line 25: Line 33:
 
function Reporter.new( title )
 
function Reporter.new( title )
 
local data = { -- TODO: use local references as fields?
 
local data = { -- TODO: use local references as fields?
 +
_canDumpCategories = canDumpCategories,
 
_categories = {},
 
_categories = {},
 
_warnings  = {},
 
_warnings  = {},
Line 42: Line 51:
 
]]
 
]]
 
function Reporter:setTitle( title )
 
function Reporter:setTitle( title )
self._title = title ~= nil and tostring( title ) or '' -- TODO: default to better name? Escape title? Accept nil to avoid default category?
+
self._title = tostring( title or '' ) -- TODO: default to better name? Escape title? Accept nil to avoid default category?
  
 
return self
 
return self
Line 48: Line 57:
  
 
--[[Doc
 
--[[Doc
@method Reporter getTitle
+
@method Reporter dumpCategoriesWhen
@description Gets the instance title.
+
@description Changes the verification for when the categories can be dumped.
@return {string} The instance title.
+
@parameter {function} predicate A predicate that will receive the current
 +
value of the flag and the default value.
 +
@return {Reporter} `self`
 
]]
 
]]
function Reporter:getTitle()
+
function Reporter:dumpCategoriesWhen( predicate )
return self._title
+
self._canDumpCategories = predicate(
 +
self._canDumpCategories,
 +
canDumpCategories
 +
)
 +
 
 +
return self
 
end
 
end
  
Line 100: Line 116:
 
end
 
end
  
local function formatCategory( name, sortkey )
+
local function formatCategory( self, name, sortkey )
return ( sortkey
+
return ( sortkey and '[[Category:((%s)) %s|%s]]' or '[[Category:((%s)) %s]]' )
and table.concat{
+
:format( self._title, name, sortkey )
'[[Category:', name, '|', sortkey, ']]'
 
}
 
or table.concat{
 
'[[Category:', name, ']]'  
 
}
 
)
 
 
end
 
end
  
 
--[[Doc
 
--[[Doc
@method Reporter dumpCategories
+
@function dumpCategories
 
]]
 
]]
 
local function dumpCategories( self )
 
local function dumpCategories( self )
Line 120: Line 130:
 
or
 
or
 
self._errors.exists
 
self._errors.exists
) and table.concat{
+
) and formatCategory( self, 'transclusions to be checked', ' ' )
'[[Category:((', self._title, ')) transclusions to be checked]]'
 
},
 
 
}
 
}
  
 
for index, categoryPair in ipairs( self._categories ) do
 
for index, categoryPair in ipairs( self._categories ) do
table.insert( categories, formatCategory( categoryPair[ 1 ], categoryPair[ 2 ] ) )
+
table.insert(
 +
categories,
 +
formatCategory( self, categoryPair[ 1 ], categoryPair[ 2 ] )
 +
)
 
end
 
end
  
Line 141: Line 152:
  
 
for _, warning in ipairs( self._warnings ) do
 
for _, warning in ipairs( self._warnings ) do
container:node(
+
container:tag( 'li' ):wikitext( warning )
tostring( mwHtmlCreate( 'li' )
 
:wikitext( warning )
 
)
 
)
 
 
end
 
end
  
Line 160: Line 167:
  
 
for _, err in ipairs( self._errors ) do
 
for _, err in ipairs( self._errors ) do
container:node(
+
container:tag( 'li' ):wikitext( err )
tostring( mwHtmlCreate( 'li' )
 
:wikitext( err )
 
)
 
)
 
 
end
 
end
  
Line 170: Line 173:
 
end
 
end
  
 +
--[[Doc
 +
@method Reporter dump
 +
]]
 
function Reporter:dump()
 
function Reporter:dump()
return table.concat{
+
return tostring( mwHtmlCreate( 'div' )
'<div class="reporter">',
+
:addClass( 'reporter' )
dumpCategories( self ),
+
:node( self._canDumpCategories and dumpCategories( self ) )
self._warnings.exists and dumpWarnings( self ) or '',
+
:node( self._errors.exists and dumpErrors( self ) )
self._errors.exists and dumpErrors( self ) or '',
+
:node( self._warnings.exists and dumpWarnings( self ) )
'</div>',
+
)
}
 
 
end
 
end
  
Line 185: Line 190:
 
]=]
 
]=]
 
return setmetatable( Reporter, {
 
return setmetatable( Reporter, {
__call = function( t, ... )
+
__call = function( t, title )
 
assert(
 
assert(
 
t == Reporter,
 
t == Reporter,
Line 191: Line 196:
 
)
 
)
  
return Reporter.new( ... )
+
return Reporter.new( title )
 
end
 
end
 
} )
 
} )
 
-- </pre>
 
-- </pre>

Latest revision as of 13:33, 16 March 2023

-- <pre>
--[=[Doc
@module Reporter
@description Creates an object to store module info.
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
]=]

local mwHtmlCreate = mw.html.create

local namespace = mw.title.getCurrentTitle().namespace

local canDumpCategories = (
	namespace ~= 10
	and
	namespace % 2 == 0
)

--[[Doc
@class Reporter
@description Stores and provides an interface to manage module info.
@exportable
]]
local Reporter = {} -- TODO: check if it's efficient enough (html library vs raw strings)
Reporter.__index = Reporter
Reporter.__class = Reporter

--[[Doc
@function Reporter new
@description Initializes an instance of `Reporter`.
@return {Reporter} New instance of `Reporter`.
]]
function Reporter.new( title )
	local data = { -- TODO: use local references as fields?
		_canDumpCategories = canDumpCategories,
		_categories = {},
		_warnings   = {},
		_errors     = {},
	}

	Reporter.setTitle( data, title )

	return setmetatable( data, Reporter )
end

--[[Doc
@method Reporter setTitle
@description Changes the instance title.
@parameter {string|nil} title The new title. If `nil`, the empty string is used.
@return {Reporter} `self`
]]
function Reporter:setTitle( title )
	self._title = tostring( title or '' ) -- TODO: default to better name? Escape title? Accept nil to avoid default category?

	return self
end

--[[Doc
@method Reporter dumpCategoriesWhen
@description Changes the verification for when the categories can be dumped.
@parameter {function} predicate A predicate that will receive the current
value of the flag and the default value.
@return {Reporter} `self`
]]
function Reporter:dumpCategoriesWhen( predicate )
	self._canDumpCategories = predicate(
		self._canDumpCategories,
		canDumpCategories
	)

	return self
end

--[[Doc
@method Reporter addCategory
@description Stores a new category.
@parameter {string} category The category name.
@parameter {string|nil} sortkey The sortkey for this category.
@return {Reporter} `self`
]]
function Reporter:addCategory( category, sortkey )
	table.insert(
		self._categories,
		category and { category, sortkey }
	)

	return self
end

--[[Doc
@method Reporter addWarning
@description Stores a new warning.
@parameter {string} message The warning message.
@return {Reporter} `self`.
]]
function Reporter:addWarning( message )
	self._warnings.exists = true

	table.insert( self._warnings, message )

	return self
end

--[[Doc
@method Reporter error
@description Stores a new error.
@parameter {string} message The error message.
@return {Reporter} `self`.
]]
function Reporter:addError( message )
	self._errors.exists = true

	table.insert( self._errors, message )

	return self
end

local function formatCategory( self, name, sortkey )
	return ( sortkey and '[[Category:((%s)) %s|%s]]' or '[[Category:((%s)) %s]]' )
		:format( self._title, name, sortkey )
end

--[[Doc
@function dumpCategories
]]
local function dumpCategories( self )
	local categories = {
		(
			self._warnings.exists
			or
			self._errors.exists
		) and formatCategory( self, 'transclusions to be checked', ' ' )
	}

	for index, categoryPair in ipairs( self._categories ) do
		table.insert(
			categories,
			formatCategory( self, categoryPair[ 1 ], categoryPair[ 2 ] )
		)
	end

	return table.concat( categories )
end

--[[Doc
@function dumpWarnings
]]
local function dumpWarnings( self )
	local container = mwHtmlCreate( 'div' )
		:addClass( 'reporter__warnings' )
		:tag( 'ul' )

	for _, warning in ipairs( self._warnings ) do
		container:tag( 'li' ):wikitext( warning )
	end

	return tostring( container:allDone() )
end

--[[Doc
@function dumpErrors
]]
local function dumpErrors( self )
	local container = mwHtmlCreate( 'div' )
		:addClass( 'reporter__errors' )
		:tag( 'ul' )

	for _, err in ipairs( self._errors ) do
		container:tag( 'li' ):wikitext( err )
	end

	return tostring( container:allDone() )
end

--[[Doc
@method Reporter dump
]]
function Reporter:dump()
	return tostring( mwHtmlCreate( 'div' )
		:addClass( 'reporter' )
		:node( self._canDumpCategories and dumpCategories( self ) )
		:node( self._errors.exists and dumpErrors( self ) )
		:node( self._warnings.exists and dumpWarnings( self ) )
	)
end

--[=[Doc
@exports The `Reporter` class constructor (`new`).
@see [[#Reporter.new]]
]=]
return setmetatable( Reporter, {
	__call = function( t, title )
		assert(
			t == Reporter,
			'Cannot apply Reporter constructor except to itself'
		)

		return Reporter.new( title )
	end
} )
-- </pre>