Difference between revisions of "Module:Util"
m (Fix copy paste...) |
(Add functions to make bold, italicize and link.) |
||
Line 13: | Line 13: | ||
-- Functions: | -- Functions: | ||
------------- | ------------- | ||
+ | -- @name doBold | ||
+ | -- @description Renders bold wikitext markup. | ||
+ | function U.doBold( s ) | ||
+ | return ("'''%s'''"):format( s ) | ||
+ | end | ||
+ | |||
+ | -- @name doItalics | ||
+ | -- @description Renders italics wikitext markup. | ||
+ | function U.doItalics( s ) | ||
+ | return ("''%s''"):format( s ) | ||
+ | end | ||
+ | |||
-- @name trim | -- @name trim | ||
-- @description Trims white space from front and tail of string. Returns nil if only whitespace. | -- @description Trims white space from front and tail of string. Returns nil if only whitespace. | ||
Line 33: | Line 45: | ||
-- @name link | -- @name link | ||
− | -- | + | -- @description Creates a wikitext link. |
− | return ('[[%s|%s]]'):format( page, label or mw.text.split( | + | function U.link( page, label ) |
− | end | + | return ('[[%s|%s]]'):format( |
+ | page:gsub( '#', '' ), | ||
+ | label or mw.text.split( page, '%s*%(' )[1] | ||
+ | ); | ||
+ | end | ||
-- @name isSomething | -- @name isSomething |
Revision as of 00:50, 6 May 2018
-- <pre>
-- @name Util
-- @description Holds commonly used simple functions.
-- @author [[User:Becasita]]
-- @contact [[User talk:Becasita]]
-------------------
-- Export variable:
-------------------
local U = {};
-------------
-- Functions:
-------------
-- @name doBold
-- @description Renders bold wikitext markup.
function U.doBold( s )
return ("'''%s'''"):format( s )
end
-- @name doItalics
-- @description Renders italics wikitext markup.
function U.doItalics( s )
return ("''%s''"):format( s )
end
-- @name trim
-- @description Trims white space from front and tail of string. Returns nil if only whitespace.
-- @see [[mw:Extension:Scribunto/Lua reference manual#mw.text.trim]]
function U.trim( s )
if s and not s:match( '^%s*$' ) then
return mw.text.trim( s );
end
end
-- @name count
-- @description Counts the number of elements in a table.
function U.count( t )
local counter = 0;
for key, value in pairs( t ) do
counter = counter + 1;
end
return counter;
end
-- @name link
-- @description Creates a wikitext link.
function U.link( page, label )
return ('[[%s|%s]]'):format(
page:gsub( '#', '' ),
label or mw.text.split( page, '%s*%(' )[1]
);
end
-- @name isSomething
-- @description Meta-function for type checkers.
local function isSomething( toCompare, compareTo )
return type( toCompare ) == type( compareTo );
end
-- @name isNumber
function U.isNumber( v )
return isSomething( v, 1 );
end
-- @name isString
function U.isString( v )
return isSomething( v, '' );
end
-- @name isTable
function U.isTable( v )
return isSomething( v, {} );
end
----------
-- Return:
----------
return U;
-- </pre>