Class: Doc2Text::Markdown::OdtParser

Inherits:
Object
  • Object
show all
Defined in:
lib/doc2text/markdown_odt_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ OdtParser

Returns a new instance of OdtParser.



6
7
8
9
# File 'lib/doc2text/markdown_odt_parser.rb', line 6

def initialize(output)
  @output = output
  @automatic_styles = {}
end

Instance Method Details

#closeObject



39
40
41
# File 'lib/doc2text/markdown_odt_parser.rb', line 39

def close
  @output.close
end

#close_node(prefix, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/doc2text/markdown_odt_parser.rb', line 21

def close_node(prefix, name)
  # if Odt::XmlNodes::Node.create_node(prefix, name, nil, [], self).eql? @current_node
    if @current_node.parent and @current_node.parent.office_text?
      @output << @current_node.expand
      @current_node.delete
    end
    @current_node = @current_node.parent
  # else
  #   # TODO remove this redundant(tree build algorithm) checks
  #   raise Doc2Text::XmlError, "!Close node child #{prefix} #{name} IS NOT correct, CURRENT_ELEM #{@current_node}"
  # end
end

#loggerObject



67
68
69
# File 'lib/doc2text/markdown_odt_parser.rb', line 67

def logger
  @logger ||= Logger.new(STDOUT)
end

#new_node(prefix, name, attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/doc2text/markdown_odt_parser.rb', line 11

def new_node(prefix, name, attrs)
  unless @xml_root
    @xml_root = @current_node = Odt::XmlNodes::Node.create_node prefix, name, nil, attrs, self
  else
    new_node = Odt::XmlNodes::Node.create_node prefix, name, @current_node, attrs, self
    @current_node.children << new_node
    @current_node = new_node
  end
end


43
44
45
46
47
48
# File 'lib/doc2text/markdown_odt_parser.rb', line 43

def print_tree(node)
  puts node
  node.children.each do |child|
    print_tree child
  end
end

#text(string) ⇒ Object



34
35
36
37
# File 'lib/doc2text/markdown_odt_parser.rb', line 34

def text(string)
  plain_text = Odt::XmlNodes::PlainText.new(string)
  @current_node.children << plain_text
end

#xpath(string) ⇒ Object

Select nodes xpath style

  • supports selecting from the root node



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/doc2text/markdown_odt_parser.rb', line 52

def xpath(string)
  if /^(\/[\w:\-]+)+$/ =~ string
    path = string.scan /[\w:\-]+/
    seek_nodes = [@xml_root]
    path.each_with_index do |xml_name, index|
      seek_nodes.select! { |node| node.xml_name == xml_name }
      seek_nodes = seek_nodes.map(&:children).flatten unless index == path.length - 1
      break if seek_nodes.empty?
    end
    seek_nodes
  else
    raise Doc2Text::XmlError, 'it does not support this xpath syntax'
  end
end