Class: HTML2FB::StateMachine

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

Instance Method Summary collapse

Constructor Details

#initializeStateMachine

Returns a new instance of StateMachine.



130
131
132
133
134
135
136
137
# File 'lib/Html2Feedbooks/parser.rb', line 130

def initialize
	@levels=[]
	@current_level=0
	@starts=[]
	@done=[]
	@max_level=0
	@content=nil
end

Instance Method Details

#add_level(tab) ⇒ Object



139
140
141
142
143
# File 'lib/Html2Feedbooks/parser.rb', line 139

def add_level(tab)
	tab=[tab] unless tab.is_a?Array
	@levels.push tab
	@current_level+=1
end

#close_section(lvl) ⇒ Object



203
204
205
206
207
208
209
# File 'lib/Html2Feedbooks/parser.rb', line 203

def close_section(lvl)
	return if @starts[lvl].nil?
	llvl=lvl-1
	llvl=llvl-1 until !@starts[llvl].nil?
	@starts[llvl].content.push @starts[lvl]
	@starts[lvl]=nil
end

#create_fbsection(title, fblevel) ⇒ Object



156
157
158
159
160
161
# File 'lib/Html2Feedbooks/parser.rb', line 156

def create_fbsection(title,fblevel)
	s=Section.new
	s.fblevel=fblevel
	s.title = title
	s
end

#create_textNode(txt) ⇒ Object



163
164
165
# File 'lib/Html2Feedbooks/parser.rb', line 163

def create_textNode(txt)
	Text.new(txt)
end

#feed(el) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/Html2Feedbooks/parser.rb', line 211

def feed(el)
	return if el.text?
	@done=[[]*@levels.size]

	@levels.each_with_index do  |lvl,i|
		lvl.each do |expr|
			#puts i.to_s+" "+el.inspect if el.in_search?(expr['expr'])
			if el.in_search?(expr['expr'])


				open_section({:xpath => el.path, :fblevel => expr['fblevel']},i+1,el)
				break
			end
		end
	end

end

#finish(doc) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/Html2Feedbooks/parser.rb', line 167

def finish(doc)
	unless @content.nil?
		#	t=create_textNode(doc.root.search(@content...doc.children.last.xpath))
		t=create_textNode(doc.at(@content).following.to_html)
		@starts[@current_level].content.push(t)
	end
	(1..@max_level).to_a.reverse.each do |l|
		close_section(l)
	end
	@starts[0]
end

#inspectObject



152
153
154
# File 'lib/Html2Feedbooks/parser.rb', line 152

def inspect
	@levels.inspect+"\n"+@current_level.to_s+"\n\n"+@done.inspect
end

#open_section(obj, lvl, el) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/Html2Feedbooks/parser.rb', line 179

def open_section(obj,lvl,el)
	if @content=='body'
		tmp=el.preceding[0..-1]
	else
		tmp=el.root.between(@content,(el.path),true)[1..-1]
	end
	if tmp.blank? #search can'find between siblins
		tmp=el.root.deep_between(@content,(el.path))
	end
	unless tmp.blank?
		tmph=tmp.to_html
		unless tmph.blank?
			t=create_textNode(tmph)
			@starts[@current_level].content.push(t)
		end
	end
	(lvl..@max_level).to_a.reverse.each do |l|
		close_section(l)
	end
	@starts[lvl]=create_fbsection(el.root.at_xpath(obj[:xpath]).text,obj[:fblevel])
	@content=obj[:xpath]
	@current_level=lvl
end

#reset(doc) ⇒ Object



145
146
147
148
149
150
# File 'lib/Html2Feedbooks/parser.rb', line 145

def reset(doc)
	@current_level=0
	@max_level=@levels.size
	@starts[0]=doc
	@content='body'
end