Class: Cheepub::HeadingParser::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/cheepub/heading_parser/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Handler

Returns a new instance of Handler.



7
8
9
10
11
# File 'lib/cheepub/heading_parser/handler.rb', line 7

def initialize(filename)
  @filename = filename
  @stack = []
  @result = []
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'lib/cheepub/heading_parser/handler.rb', line 5

def result
  @result
end

Instance Method Details

#after_element(ns, name) ⇒ Object



25
26
27
28
# File 'lib/cheepub/heading_parser/handler.rb', line 25

def after_element(ns, name)
  return if @stack.empty?
  @result.push(@stack.pop)
end

#on_element(ns, name, attrs) ⇒ Object



13
14
15
16
17
# File 'lib/cheepub/heading_parser/handler.rb', line 13

def on_element(ns, name, attrs)
  if name =~ /^h([1-6])/i
    @stack.push({filename: @filename, name: name, level: $1.to_i, content: "", id: attrs["id"]})
  end
end

#on_text(text) ⇒ Object



19
20
21
22
23
# File 'lib/cheepub/heading_parser/handler.rb', line 19

def on_text(text)
  last_elem = @stack[-1]
  return if !last_elem
  last_elem[:content] = last_elem[:content]+text
end