Class: Eiwa::Kanjidic::Doc

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/eiwa/kanjidic/doc.rb

Instance Method Summary collapse

Constructor Details

#initialize(each_entry_block) ⇒ Doc

Returns a new instance of Doc.



11
12
13
14
# File 'lib/eiwa/kanjidic/doc.rb', line 11

def initialize(each_entry_block)
  @each_entry_block = each_entry_block
  @current = nil
end

Instance Method Details

#characters(s) ⇒ Object



34
35
36
# File 'lib/eiwa/kanjidic/doc.rb', line 34

def characters(s)
  @current.add_characters(s)
end

#end_element(name) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eiwa/kanjidic/doc.rb', line 22

def end_element(name)
  raise Eiwa::Error.new("Parsing error. Expected <#{@current.tag_name}> to close before <#{name}>") if @current.tag_name != name
  ending = @current
  ending.end_self
  if ending.is_a?(Tag::Character)
    @each_entry_block&.call(ending)
  end

  @current = ending.parent
  @current&.end_child(ending)
end

#error(msg) ⇒ Object

Raises:



38
39
40
# File 'lib/eiwa/kanjidic/doc.rb', line 38

def error(msg)
  raise Eiwa::Error.new("Parsing error: #{msg}")
end

#start_element(name, attrs) ⇒ Object



16
17
18
19
20
# File 'lib/eiwa/kanjidic/doc.rb', line 16

def start_element(name, attrs)
  parent = @current
  @current = (TAGS[name] || Tag::Other).new
  @current.start(name, attrs, parent)
end