[BioRuby] Bioruby HTML output

Tomoaki NISHIYAMA tomoakin at kenroku.kanazawa-u.ac.jp
Tue Jan 19 14:05:17 UTC 2010


Hi,

> Firstly, I don't like to use the method name 'to_html' as we already
> deprecated to use 'to_fasta' because 'to_' is reserved for conversion
> of the class in Ruby's convention (above two methods just convert
> String to String).

I think HTML and String should be actually a different class.
There are to_i and to_f for conversion between subclasses of Numeric,
yet this isn't denied because the conversion is Numeric to Numeric.

a string "<a href=example.com> aaa</a>" in HTML is
"&lt;a href=example.com&gt; aaa&lt;/a&gt;" but
HTML "<a href=example.com> aaa</a>" in HTML is "<a href=example.com>  
aaa</a>"

The return value of to_html should be a different class than String.

So, the point is
>     def output_html
>       "<h1>#{@entry_id}</h1> ... blah, blah, blah ..."
>     end

how to regulate the different behavior of @entry_id.
If the nature of entry_id is plain text, that should be escaped.
On the other hand sometimes the user may want to use html aware
object for whatever purpose (color, link, etc...).
When we want to mix them with data supplied
from outside, say user input into CGI, those data shall usually
be treated as plain text and suppress any interference with html.

#!/usr/local/bin/ruby
require 'bio'
require 'cgi'

class Bio::HTMLString < String
   def to_html
     self
   end
end
def Bio::generate_html(object)
   if object.respond_to?(:to_html)
     object.to_html
   else
     string = CGI.escapeHTML(object.to_s) #fall back to escaping
     Bio::HTMLString.new(string)
   end
end

p Bio::generate_html(12)
p Bio::generate_html(Bio::HTMLString.new('<a href=example.com> aaa</ 
a>'))
p Bio::generate_html('<a href=example.com> aaa</a>')
-- 
Tomoaki NISHIYAMA

Advanced Science Research Center,
Kanazawa University,
13-1 Takara-machi,
Kanazawa, 920-0934, Japan




More information about the BioRuby mailing list