Class: HTML2FB::StateMachine

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

Instance Method Summary collapse

Constructor Details

#initializeStateMachine

Returns a new instance of StateMachine.



111
112
113
114
115
116
117
118
# File 'lib/parser.rb', line 111

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

Instance Method Details

#add_level(tab) ⇒ Object



120
121
122
123
124
# File 'lib/parser.rb', line 120

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

#close_section(lvl) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/parser.rb', line 177

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



137
138
139
140
141
142
# File 'lib/parser.rb', line 137

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

#create_textNode(txt) ⇒ Object



144
145
146
# File 'lib/parser.rb', line 144

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

#feed(el) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/parser.rb', line 185

def feed(el)
	return if el.is_a?Hpricot::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.xpath, :fblevel => expr['fblevel']},i+1,el)
				break
			end
		end
	end

end

#finish(doc) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/parser.rb', line 148

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



133
134
135
# File 'lib/parser.rb', line 133

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

#open_section(obj, lvl, el) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/parser.rb', line 160

def open_section(obj,lvl,el)
	tmp=el.root.search(@content...(el.xpath))[1..-1]
	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(obj[:xpath]).extract_text,obj[:fblevel])
	@content=obj[:xpath]
	@current_level=lvl
end

#reset(doc) ⇒ Object



126
127
128
129
130
131
# File 'lib/parser.rb', line 126

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