# = HTML utilities collection # # code:: gmosx, tkout, ekarak # # (c) 2004 Navel, all rights reserved. # $Id: html.rb 71 2004-10-18 10:50:22Z gmosx $ require "uri" require "cgi" module N; # = HtmlUtils # # === Design: # # Implement as a module to avoid class polution. You can still Ruby's # advanced features to include the module in your class. # Passing the object to act upon allows to check for nil, which isn't # possible if you use self. # # The older text_sum, text_block methods are not needed in the latest # code # # === TODO: # - add xxx! versions # module HtmlUtils # escape html tags. # usefull to make text entered by end users html safe. # # Input: # the string to be escaped # # Output: # the escaped string # def self.escape(string) # gmosx: no need to return "" on nil, will be interpolated to "" return nil unless string return CGI::escapeHTML(string) end # TODO: move to markup! # # Expands the urls found in the given string. Use the target parameter # to apply presentation semantics (ie open in new window) # # Example: # text = "visit this site: www.navel.gr" # text = Web::Utils::Html::expand_urls(text) # p text # => # "visit this site: http://www.navel.gr" # def self.expand_urls(string, target = nil) return nil unless string xstring = string.gsub(/\s(www\.[^\s]*)/, " http://\\1") xstring.gsub!(/\s(ftp\.[^\s]*)/, " ftp://\\1") xstring.gsub!(URI::REGEXP::ABS_URI_REF) { |uriref| if /(http|ftp):/.match(uriref) "#{uriref}" else uriref end } return xstring end # Strips potentially dangerous html tags, leaving only safe # tags. Usefull for simple Html formatting. # # === Design: # # Escapes ALL quotes for security, use html without quotes: # # kok #