Class: Eiwa::JmdictDoc

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) ⇒ JmdictDoc

Returns a new instance of JmdictDoc.



33
34
35
# File 'lib/eiwa/jmdict_doc.rb', line 33

def initialize(each_entry_block)
  @each_entry_block = each_entry_block
end

Instance Method Details

#characters(s) ⇒ Object



61
62
63
# File 'lib/eiwa/jmdict_doc.rb', line 61

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

#end_documentObject



40
41
# File 'lib/eiwa/jmdict_doc.rb', line 40

def end_document
end

#end_element(name) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eiwa/jmdict_doc.rb', line 49

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



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/eiwa/jmdict_doc.rb', line 73

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, JMDICT_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



37
38
# File 'lib/eiwa/jmdict_doc.rb', line 37

def start_document
end

#start_element(name, attrs) ⇒ Object



43
44
45
46
47
# File 'lib/eiwa/jmdict_doc.rb', line 43

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