vicious/helpers.lua

55 lines
1.1 KiB
Lua
Raw Normal View History

2009-09-29 22:33:19 +02:00
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2009, Adrian C. <anrxc.sysphere.org>
-- * (c) Wicked, Lucas de Vries
---------------------------------------------------
-- {{{ Grab environment
local pairs = pairs
local string = {
sub = string.sub,
gsub = string.gsub
}
-- }}}
-- Helpers: provides helper functions for vicious widgets
module("vicious.helpers")
-- {{{ Format a string with args
function format(format, args)
for var, val in pairs(args) do
format = string.gsub(format, "$" .. var, val)
end
return format
end
-- }}}
--{{{ Escape a string
function escape(text)
local xml_entities = {
["\""] = "&quot;",
["&"] = "&amp;",
["'"] = "&apos;",
["<"] = "&lt;",
[">"] = "&gt;"
}
return text and text:gsub("[\"&'<>]", xml_entities)
end
-- }}}
--{{{ Truncate a string
function truncate(text, maxlen)
2009-08-07 23:19:50 +02:00
local txtlen = text:len()
if txtlen > maxlen then
text = text:sub(1, maxlen - 3) .. "..."
end
return text
end
-- }}}