[BioRuby] Bioruby HTML output

Toshiaki Katayama ktym at hgc.jp
Tue Jan 19 15:33:30 UTC 2010


Nishiyama-san,

I couldn't catch what you are trying to do...
(maybe because I didn't read throughout the thread)

On 2010/01/19, at 23:05, Tomoaki NISHIYAMA wrote:

> 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.

If the method is named as to_html, it might return a HTML object.

But, from my view point, a html string is still just a text
and escaping the html string is responsibility of a programmer
depending on where the string will be used.


> 
> 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.

I'm talking about a database class and the contents of
@entry_id is a string parsed from an flat file entry of
that database (not come from outside).


> 
> #!/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>')

Why we need to have this functionality under the Bio name space?

Toshiaki

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





More information about the BioRuby mailing list