Class: Doc2Text::Markdown::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ Document

Returns a new instance of Document.



4
5
6
7
# File 'lib/doc2text/markdown.rb', line 4

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

Instance Method Details

#<<(string) ⇒ Object



52
53
54
# File 'lib/doc2text/markdown.rb', line 52

def <<(string)
  @output << string
end

#closeObject



56
57
58
# File 'lib/doc2text/markdown.rb', line 56

def close
  @output.close
end

#close_node(prefix, name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/doc2text/markdown.rb', line 20

def close_node(prefix, name)
  if Odt::XmlNodes::Node.create_node(prefix, name).eql? @current_node
    if @current_node.delete_on_close?
      remove_current_node!
    else
      remove_current_node! false
    end
  elsif Odt::XmlNodes::Node.create_node(prefix, name).eql? @current_node.parent
    if @current_node.parent.delete_on_close?
      remove_current_node!
      remove_current_node!
    else
      remove_current_node! false
      remove_current_node! false
    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

#new_node(prefix, name, attrs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/doc2text/markdown.rb', line 9

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
    self << @current_node.open
  end
end


60
61
62
63
64
65
# File 'lib/doc2text/markdown.rb', line 60

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

#remove_current_node!(remove = true) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/doc2text/markdown.rb', line 41

def remove_current_node!(remove = true)
  return if !@current_node
  self << @current_node.close
  node_for_deletion = @current_node
  @current_node = @current_node.parent
  return unless @current_node
  if remove
    @current_node.remove_last_child! node_for_deletion
  end
end

#xpath(string) ⇒ Object

Select nodes xpath style

  • supports selecting from the root node



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/doc2text/markdown.rb', line 69

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