Difference between pages "Module:Util" and "Module:Util/sandbox"

(Difference between pages)
Jump to: navigation, search
Page 1
Page 2
(Tooltip)
 
(Testing string format with named arguments.)
 
Line 671: Line 671:
 
util.cargo.store(frame, cargo_values)
 
util.cargo.store(frame, cargo_values)
 
end
 
end
end
 
 
function util.args.template_to_lua(str)
 
--[[
 
Convert templates to lua format. Simplifes debugging and creating
 
examples.
 
 
Parameters
 
----------
 
str : string
 
The entire template wrapped into string.
 
 
Returns
 
-------
 
out : table
 
out.template - Template name.
 
out.args - arguments in table format.
 
out.args_to_str - arguments in readable string format.
 
]]
 
local out = {}
 
 
-- Get the template name:
 
out.template = string.match(str, '{{(.-)%s*|')
 
 
-- Remove everything but the arguments:
 
str = string.gsub(str, '%s*{{.-|', '')
 
str = string.gsub(str, '%s*}}%s*', '')
 
 
-- Split up the arguments:
 
out.args = {}
 
for i, v in ipairs(util.string.split(str, '%s*|%s*')) do
 
local arg = util.string.split(v, '%s*=%s*')
 
out.args[arg[1]] = arg[2]
 
out.args[#out.args+1] = arg[1]
 
end
 
 
-- Concate for easy copy/pasting:
 
local tbl = {}
 
for i, v in ipairs(out.args) do
 
tbl[#tbl+1]= string.format("%s='%s'", v, out.args[v])
 
end
 
out.args_to_str = table.concat(tbl, ',\n')
 
 
return out
 
 
end
 
end
   
Line 756: Line 712:
   
 
function util.html.tooltip(abbr, text, class)
 
function util.html.tooltip(abbr, text, class)
return string.format('<span class="hoverbox c-tooltip %s"><span class="hoverbox__activator c-tooltip__activator">%s</span><span class="hoverbox__display c-tooltip__display">%s</span></span>', class or '', abbr or '', text or '')
+
return string.format('<span class="tooltip-activator %s">%s<span class="tooltip-content">%s</span></span>', class or '', abbr or '', text or '')
 
end
 
end
   
Line 1,397: Line 1,353:
 
--[[
 
--[[
 
Allow string replacement using named arguments.
 
Allow string replacement using named arguments.
 
TODO:
 
* Support %d ?
 
* Support 0.2f ?
 
   
 
Parameters
 
Parameters
Line 1,410: Line 1,362:
 
--------
 
--------
 
= util.string.format('{foo} is {bar}.', {foo='Dinner', bar='nice'})
 
= util.string.format('{foo} is {bar}.', {foo='Dinner', bar='nice'})
 
References
 
----------
 
http://lua-users.org/wiki/StringInterpolation
 
 
]]
 
]]