local Utils = {}
--[[ Explode a str into a table of fragments delimited by separator.
If limit is provided the resulting table will have limit entries.
credit: http://richard.warburton.it improved to add a limit
]]
function Utils.explode( sep, str, limit )
if ( sep == '' ) then return false end
local pos, arr = 0, {}
local i = 1
local limit = limit or 0
-- for each divider found
for st, sp in function() return string.find( str, sep, pos, true ) end do
if i == limit then break end
table.insert( arr, string.sub(str, pos, st-1) ) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
i = i + 1
end
table.insert( arr, string.sub(str, pos) ) -- Attach chars right of last divider
return arr
end
--[[ Removes disambig parenthesis at the end of the title.
frame is required for the function to work both from a module or from a wikipage
limit defines how many disambig parts must be removed (default is 1)
title is the text to work on (default is current page title if not defined)
]]
function Utils.cleanTitle( title, limit )
title = title or mw.getCurrentFrame():getParent():getTitle()
limit = limit or 1
for i=1, limit do
if string.match( title, ".*%(" ) == nil then break end
title = string.match( title, ".*%(" )
title = mw.text.trim( title:sub( 1, #title-1 ) )
end
return title
end
-- Make the first lettre a maj
function Utils.startMaj( text )
text = text or ""
return string.upper( text:sub( 1, 1 ) ) .. text:sub( 2 )
end
-- Returns a color code base on element
function Utils.color( element )
local element = element
local color = "inherit"
if element == "fire" then
color = "#f44336"
elseif element == "water" then
color = "#2196f3"
elseif element == "earth" then
color = "#795548"
elseif element == "wind" then
color = "#8bc34a"
elseif element == "dark" then
color = "#673ab7"
elseif element == "light" then
color = "#ffc107"
elseif element == "life" then
color = "#f06292"
elseif element == "prismatic" then
end
return color
end
-- Color text based on element
function Utils.colorText( text, element )
local text = text or ""
local element = element
local span = mw.html.create( 'span' )
span :css( 'color', Utils.color( element ) )
:wikitext( text )
return tostring( span )
end
-- Display stars
function Utils.stars( n )
local n = n or 1
local rotate = function( elmt, angle )
return elmt:css( 'transform', "rotateZ(" .. angle .. "deg)" )
end
local lheight = function( elmt, lh )
return elmt:css( 'line-height', lh )
end
local lspacing = function( elmt, ls )
return elmt:css( 'letter-spacing', ls .. 'px' )
end
local s = mw.html.create( 'span' )
s:wikitext( "★" ):css( 'display', "inline-block" )
local stars = mw.html.create( 'div' )
stars:css( { ['text-align'] = 'center', display = 'inline-block' } )
if n == 1 then
stars:wikitext( tostring(s) )
elseif n == 2 then
rotate( stars, "-30" ); rotate( s, "30" )
lheight( stars, "0.6" ); lspacing( stars, "-3" )
stars:wikitext( tostring(s):rep(2) )
elseif n == 3 then
rotate( stars, "-20" ); rotate( s, "20" )
lheight( stars, "0.6" ); lspacing( stars, "-1" )
stars:wikitext( "<div>"..tostring(s).."</div>"
.."<div>"..tostring(s):rep(2).."</div>" )
elseif n == 4 then
rotate( stars, "-15" ); rotate( s, "15" )
lheight( stars, "0.75" ); lspacing( stars, "-2" )
stars:wikitext( "<div>"..tostring(s):rep(2).."</div>"
.."<div>"..tostring(s):rep(2).."</div>" )
elseif n == 5 then
lheight( stars, "0.4" ); lspacing( stars, "1" )
stars:wikitext( "<div>"..tostring(s):rep(2).."</div>"
.."<div>"..tostring(s).."</div>"
.."<div>"..tostring(s):rep(2).."</div>" )
end
return tostring( stars )
end
function Utils.test()
return _VERSION
end
return Utils
Advertisement
4,521
pages
Module:Utils
Advertisement