Class: FeedMe::HpricotUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/hpricot-util.rb

Instance Method Summary collapse

Instance Method Details

#clean_html(html) ⇒ Object

sanitize HTML todo: dup code to fix bugs



32
33
34
35
# File 'lib/hpricot-util.rb', line 32

def clean_html(html)
  return nil if html.nil?
  FeedMe::HtmlCleaner.clean(html)
end

#strip_html(html) ⇒ Object

strip all tags from HTML



21
22
23
# File 'lib/hpricot-util.rb', line 21

def strip_html(html)
  (Hpricot.parse(html)/:"text()").to_s
end

#strip_truncate_html(input, words = 15, truncate_string = '...') ⇒ Object

strip tags from HTML and truncate to a certain number of words



26
27
28
# File 'lib/hpricot-util.rb', line 26

def strip_truncate_html(input, words=15, truncate_string='...')
  strip_html(input).split[0..words].join(' ') + truncate_string
end

#truncate_html(html, words = 15, truncate_string = "...") ⇒ Object

Like the Rails truncate helper but doesn’t break HTML tags or entities.



13
14
15
16
17
18
# File 'lib/hpricot-util.rb', line 13

def truncate_html(html, words=15, truncate_string= "...")
  return if html.nil?
  doc = Hpricot(html.to_s)
  doc.inner_text.mb_chars.split.size >= words ? 
    doc.truncate(words, truncate_string).inner_html : html.to_s
end