# $Id: html.rb 2004/11/01 09:19:23 transami Exp $ =begin rdoc = HTML Helper methods for creating HTML/XHTML documents. == Synopsis require 'trix/xhtml' # [ to do ] == Authors * T. Onoma =end require 'cgi' # Until it has its own good escape/unescape method module HTML def self.CGI_include! require 'cgi' CGI.class_eval { include XHTML } end def self.FCGI_include! require 'fcgi' FCGI.class_eval { include XHTML } end ################ module_functions ################ # Create an hidden input field through which an object can can be marshalled. # This makes it very easy to pass from data betwenn requests. def marshal_to_html(name, iobj) data = CGI.escape(Marshal.dump(iobj)) return %Q{<input type="hidden" name="__#{name}__" value="#{data}"/>\n} end # Create an hidden input field throught which an object can can be marshalled. # This makes it very easy to pass from data betwenn requests. def marshal_from_html(name) return Marshal.load(CGI.unescape(self["__#{name}__"][0])) if self.params.has_key?("__#{name}__") end # Are these good enough to replace CGI.escape? # Return an html "safe" version of the string, # where every &, < and > are replaced with appropriate entities. def esc(str) str.gsub(/&/,'&').gsub(/</,'<').gsub(/>/,'>') end # Calls #esc, and then further replaces carriage returns and quote characters with entities. def escformat(str) xmlsafe(str).gsub(/[\r\n]+/,' ').gsub(%r|"|,'"').gsub(%r|'|,''') end # Renders an object with annotations: http://tinyurl.com/6xjnj # Creates a table rendering +o+'s attributes. def render_object(o, collection, edit) r = "<table>\n" o.instance_variables.each do |attr_name| attr_value = o.instance_variable_get(attr_name) attr_anns = o.class.send(attr_name[1..-1]) r << " <tr>\n" r << " <td>" << attr_anns[:description] << "</td>\n" r << " <td>" << text_or_input(edit, :name => "#{collection}[#{o.class.name}][#{attr_name}]", :value => attr_value) << "</td>\n" r << " </tr>\n" end r << "</table>" r end end