lib/knj/strings.rb in knjrbfw-0.0.7 vs lib/knj/strings.rb in knjrbfw-0.0.8
- old
+ new
@@ -60,6 +60,78 @@
def self.shorten(str, maxlength)
str = str.to_s
str = str.slice(0..(maxlength - 1)).strip + "..." if str.length > maxlength
return str
end
+
+ def self.html_links(str, args = {})
+ str.to_s.html.scan(/(http:\/\/([A-z]+)\S*\.([A-z]{2,4})(\S+))/) do |match|
+ if block_given?
+ str = yield(:str => str, :args => args, :match => match)
+ else
+ if args["target"]
+ html = "<a target=\"#{args["target"]}\""
+ else
+ html = "<a"
+ end
+
+ html += " href=\"#{match[0]}\">#{match[0]}</a>"
+ str = str.gsub(match[0], html)
+ end
+ end
+
+ return str
+ end
+
+ def self.strip(origstr, args)
+ newstr = "#{origstr}<br>"
+
+ if !args.key?(:right) or args[:right]
+ loop do
+ changed = false
+ args[:strips].each do |str|
+ len = str.length
+ endstr = newstr.slice(-len, len)
+ next if !endstr
+
+ if endstr == str
+ changed = true
+ newstr = newstr.slice(0..newstr.length-len-1)
+ end
+ end
+
+ break if !changed
+ end
+ end
+
+ if !args.key?(:left) or args[:left]
+ loop do
+ changed = false
+ args[:strips].each do |str|
+ len = str.length
+ endstr = newstr.slice(0, len)
+ next if !endstr
+
+ if endstr == str
+ changed = true
+ newstr = newstr.slice(len..-1)
+ end
+ end
+
+ break if !changed
+ end
+ end
+
+ return newstr
+ end
+
+ #Returns the module from the given string - even if formed as SomeClass::SomeNewClass.
+ def self.const_get_full(str)
+ module_use = Kernel
+
+ str.scan(/(.+?)(::|$)/) do |match|
+ module_use = module_use.const_get(match[0])
+ end
+
+ return module_use
+ end
end
\ No newline at end of file