Class: MusicStory::Utils::HTMLToText

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/music_story/utils/html_to_text.rb

Overview

Converts HTML to plain text, converting
‘s into newlines but stripping all other tags. May want to add support for other things like <p> into nn if they crop up; MusicStory only seems to use
though

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTMLToText

Returns a new instance of HTMLToText.



13
14
15
# File 'lib/music_story/utils/html_to_text.rb', line 13

def initialize
  @result = ''
end

Class Method Details

.convert(html) ⇒ Object



7
8
9
10
11
# File 'lib/music_story/utils/html_to_text.rb', line 7

def self.convert(html)
  doc = new
  Nokogiri::HTML::SAX::Parser.new(doc).parse(html)
  doc.to_s
end

Instance Method Details

#characters(string) ⇒ Object Also known as: cdata_block



17
18
19
# File 'lib/music_story/utils/html_to_text.rb', line 17

def characters(string)
  @result << string
end

#start_element(name, attributes = nil) ⇒ Object



22
23
24
# File 'lib/music_story/utils/html_to_text.rb', line 22

def start_element(name, attributes=nil)
  @result << "\n" if name.downcase == 'br'
end

#to_sObject



26
27
28
# File 'lib/music_story/utils/html_to_text.rb', line 26

def to_s
  @result.strip
end