Re: Sample code for displaying a MIME email message on a web

Olivier Dirrenberger
Fri, 17 Aug 2007 09:20:27 -0000

Joshua Baer wrote:
> Sorry one last question on TextHelper... I can call sanitize from
> within a view (.rhtml file) no problem, but when I try to use it
> within a controller or model it complains that the method is unknown.
> What do I need to require/include to reference sanitize from within a
> controller or model?
>
> ~Josh

Just copy this on your application.rb

  def strip_tags(html)
    return html if html.blank?
    if html.index("<")
      text = ""
      tokenizer = HTML::Tokenizer.new(html)

      while token = tokenizer.next
        node = HTML::Node.parse(nil, 0, 0, token, false)
        # result is only the content of any Text nodes
        text << node.to_s if node.class == HTML::Text
      end
      # strip any comments, and if they have a newline at the end (ie.
line with
      # only a comment) strip that too
text.gsub(/ [\n]?/m, "")
    else
      html # already plain text
    end
--
Posted via http://www.ruby-forum.com/.