Class: Eiwa::Jmdict::Doc

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

Instance Method Summary collapse

Constructor Details

#initialize(each_entry_block) ⇒ Doc

Returns a new instance of Doc.



23
24
25
26
# File 'lib/eiwa/jmdict/doc.rb', line 23

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

Instance Method Details

#characters(s) ⇒ Object



52
53
54
# File 'lib/eiwa/jmdict/doc.rb', line 52

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

#end_documentObject



31
32
# File 'lib/eiwa/jmdict/doc.rb', line 31

def end_document
end

#end_element(name) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eiwa/jmdict/doc.rb', line 40

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::Entry)
    @each_entry_block&.call(ending)
  end

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

#error(msg) ⇒ Object

def warning string

puts "warning #{string}"

end



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/eiwa/jmdict/doc.rb', line 64

def error(msg)
  if (matches = msg.match(/Entity '(\S+)' not defined/))
    # See: http://github.com/sparklemotion/nokogiri/issues/1926
    code = matches[1]
    @current.set_entity(code, ENTITIES[code])
  elsif msg == "Detected an entity reference loop\n"
    # Do nothing and hope this does not matter.
  else
    raise Eiwa::Error.new("Parsing error: #{msg}")
  end
end

#start_documentObject



28
29
# File 'lib/eiwa/jmdict/doc.rb', line 28

def start_document
end

#start_element(name, attrs) ⇒ Object



34
35
36
37
38
# File 'lib/eiwa/jmdict/doc.rb', line 34

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