@DeletedUser1714 (talk | contribs) mNo edit summary |
BryghtShadow (talk | contribs) (frame detection.) |
||
(11 intermediate revisions by one other user not shown) | |||
Line 19: | Line 19: | ||
table.insert( arr, string.sub(str, pos) ) -- Attach chars right of last divider |
table.insert( arr, string.sub(str, pos) ) -- Attach chars right of last divider |
||
return arr |
return arr |
||
+ | end |
||
+ | |||
+ | function Utils.trim( str ) |
||
+ | return mw.text.trim( str ) |
||
end |
end |
||
Line 26: | Line 30: | ||
title is the text to work on (default is current page title if not defined) |
title is the text to work on (default is current page title if not defined) |
||
]] |
]] |
||
− | function Utils.cleanTitle( |
+ | function Utils.cleanTitle( title, limit ) |
title = title or mw.getCurrentFrame():getParent():getTitle() |
title = title or mw.getCurrentFrame():getParent():getTitle() |
||
limit = limit or 1 |
limit = limit or 1 |
||
Line 35: | Line 39: | ||
end |
end |
||
return title |
return title |
||
+ | end |
||
+ | |||
+ | function Utils.cleanLink( title, limit ) |
||
+ | return "[[" .. title .. "|" .. Utils.cleanTitle( title, limit ) .. "]]" |
||
end |
end |
||
Line 74: | Line 82: | ||
:wikitext( text ) |
:wikitext( text ) |
||
return tostring( span ) |
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 |
||
+ | |||
+ | -- Sort a table with numeric keys after some of them are removed |
||
+ | function Utils.sortTable( t ) |
||
+ | local sorted = {} |
||
+ | local j = 1 |
||
+ | for i=1, #t do |
||
+ | if t[i] ~= nil then |
||
+ | sorted[j] = t[i] |
||
+ | j = j + 1 |
||
+ | end |
||
+ | end |
||
+ | return sorted |
||
+ | end |
||
+ | |||
+ | function Utils.test(frame) |
||
+ | auto = require( 'Module:AutoAbilities' ) |
||
+ | return auto.test( frame.args[1] ) |
||
+ | end |
||
+ | |||
+ | Utils.misc = {} |
||
+ | function Utils.misc.is_frame(frame) |
||
+ | -- the type of the frame is a table containing the functions, so check whether some of these exist |
||
+ | -- should be enough to avoid collisions. |
||
+ | return not(frame == nil or type(frame) ~= 'table' or (frame.argumentPairs == nil and frame.callParserFunction == nil)) |
||
+ | end |
||
+ | |||
+ | function Utils.misc.get_frame(frame) |
||
+ | if Utils.misc.is_frame(frame) then |
||
+ | return frame |
||
+ | end |
||
+ | return mw.getCurrentFrame() |
||
end |
end |
||
Latest revision as of 02:01, 19 February 2019
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
function Utils.trim( str )
return mw.text.trim( str )
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
function Utils.cleanLink( title, limit )
return "[[" .. title .. "|" .. Utils.cleanTitle( title, limit ) .. "]]"
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
-- Sort a table with numeric keys after some of them are removed
function Utils.sortTable( t )
local sorted = {}
local j = 1
for i=1, #t do
if t[i] ~= nil then
sorted[j] = t[i]
j = j + 1
end
end
return sorted
end
function Utils.test(frame)
auto = require( 'Module:AutoAbilities' )
return auto.test( frame.args[1] )
end
Utils.misc = {}
function Utils.misc.is_frame(frame)
-- the type of the frame is a table containing the functions, so check whether some of these exist
-- should be enough to avoid collisions.
return not(frame == nil or type(frame) ~= 'table' or (frame.argumentPairs == nil and frame.callParserFunction == nil))
end
function Utils.misc.get_frame(frame)
if Utils.misc.is_frame(frame) then
return frame
end
return mw.getCurrentFrame()
end
return Utils