Class: HTML2FB::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/Html2Feedbooks/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Parser

Returns a new instance of Parser.



11
12
13
# File 'lib/Html2Feedbooks/parser.rb', line 11

def initialize(conf)
	@conf=conf
end

Instance Method Details

#parse(txt) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Html2Feedbooks/parser.rb', line 15

def parse(txt)
	puts "Parsing HTML"
	pdoc=Nokogiri::HTML(txt)
	if @conf['conv']
		mc=pdoc/'meta[@http-equiv="Content-Type"]'
		if mc.size>0
			charset=mc.first.attributes['content'].to_s.split(';').find do |s|
				s.strip[0,7]=='charset'
			end
			unless charset.nil?
				tc=charset.split('=').last.strip
			end

			unless tc.nil? 
				puts "Trying to convert source encoding from #{tc} to utf-8"
				require 'iconv'
				pdoc=Nokogiri::HTML(Iconv.conv('utf-8',tc.downcase,txt))

			end

		end
	end
	doc=Document.new
	puts "Removing garbage elements"
	remove_objs(pdoc)
	ti=pdoc.at('title')
	doc.title= ti.text.strip unless ti.nil?
	#			pdoc.search('//h3').each do |e|
	#				doc.content.push(e.inner_text)
	#			end

	puts "Building TOC"
	parse_text(pdoc,doc)	

	#			puts green(bold(doc.pretty_inspect))

	return doc
end