Class: DoctorNinja::Parser

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

Defined Under Namespace

Classes: Noop

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/doctor_ninja/parser.rb', line 12

def initialize(doc)
  @docx = doc
  @xmldoc = Nokogiri::XML @docx.read "word/document.xml"
  
  # This will remove "vanished" word runs.
  # Maybe this sould be treated separatly, but as
  # we use xslt for math transformation this is the only
  # way I can think to solve it. Preprocessing it...
  @xmldoc.xpath("//w:vanish/ancestor::m:r[1]").remove
  @xmldoc.xpath("//w:vanish/ancestor::w:r[1]").remove
end

Instance Method Details

#debug(node, b) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/doctor_ninja/parser.rb', line 57

def debug(node,b)
  if(ENV["DEBUG_MODE"]=="pry")
    require "pry"
    b.pry
  else
    puts "---BEGIN---\n#{node.to_xml}\n----END----"
  end
end

#debug?(node, parsers) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'lib/doctor_ninja/parser.rb', line 50

def debug?(node,parsers)
  ENV["DEBUG"] == "all" || 
  (ENV["DEBUG"] == "missing" && parsers.length == 1) || 
  ENV["DEBUG"] == node.name ||
  (node.namespace && ENV["DEBUG"] == "#{node.namespace.prefix}:#{node.name}")
end

#parseObject



24
25
26
# File 'lib/doctor_ninja/parser.rb', line 24

def parse
  self.parse_node(@xmldoc.root, {})
end

#parse_node(node, context) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/doctor_ninja/parser.rb', line 28

def parse_node(node,context)
  parsers = parsers_for(node,context)

  if debug?(node,parsers)
    debug(node,binding)
  end

  parsers
    .first
    .parse
end

#parsersObject



46
47
48
# File 'lib/doctor_ninja/parser.rb', line 46

def parsers
  DoctorNinja::Parsers.constants.map{|c| DoctorNinja::Parsers.const_get(c)}+[Noop]
end

#parsers_for(node, context) ⇒ Object



40
41
42
43
44
# File 'lib/doctor_ninja/parser.rb', line 40

def parsers_for(node,context)
  parsers
    .select{|p| p.applicable_to? node}
    .map{|p| p.new(node, self.public_method(:parse_node), @docx, context)}
end