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



47
48
49
# File 'lib/doc2text/markdown_odt_parser.rb', line 47

def close
  @output.close
end

#close_node(prefix, name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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
    return if !@current_node
    if @current_node.delete_on_close?
      # if @current_node.parent
      #   @output << @current_node.parent.expand
      #   @current_node.parent.un_delete
      # else
        @output << @current_node.expand
      # end
    end
    @current_node = @current_node.parent
    if @current_node && @current_node.delete_on_close?
      @current_node.delete
    end
  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



75
76
77
# File 'lib/doc2text/markdown_odt_parser.rb', line 75

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


51
52
53
54
55
56
# File 'lib/doc2text/markdown_odt_parser.rb', line 51

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

#text(string) ⇒ Object



42
43
44
45
# File 'lib/doc2text/markdown_odt_parser.rb', line 42

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/doc2text/markdown_odt_parser.rb', line 60

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