Class: LineTree

Inherits:
Object
  • Object
show all
Defined in:
lib/line-tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_s, level: nil, ignore_non_element: true, ignore_blank_lines: true, ignore_label: false, ignore_newline: true, root: nil, debug: false) ⇒ LineTree

Returns a new instance of LineTree.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/line-tree.rb', line 26

def initialize(raw_s, level: nil, ignore_non_element: true, 
               ignore_blank_lines: true, ignore_label: false, 
               ignore_newline: true, root: nil, debug: false)
  
  
  puts 'inside linetree'.info if @debug
  
  @root = root
  s =  raw_s
  
  @ignore_non_element = ignore_non_element
  @ignore_label = ignore_label
  @ignore_blank_lines = ignore_blank_lines
  @ignore_newline = ignore_newline
  @debug = debug
  
  lines = ignore_blank_lines ? s.gsub(/^ *$\n/,'') : s
  puts ('lines : ' + lines.inspect).debug if @debug
  @to_a = scan_shift(lines.lstrip, level)
  
end

Instance Attribute Details

#to_aObject (readonly)

Returns the value of attribute to_a


24
25
26
# File 'lib/line-tree.rb', line 24

def to_a
  @to_a
end

Instance Method Details

#make_tree_list(a) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/line-tree.rb', line 85

def make_tree_list(a)
  
  items = a.inject([]) do |r,x|    
    
    if x.is_a? Array then
      r << make_tree_list(x)
    else
      r + ['item', {title: x}]
    end
  end

end

#to_doc(encapsulate: false) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/line-tree.rb', line 48

def to_doc(encapsulate: false)    
  
  a = @to_a
  
  a2 = if a[0].is_a? Array then      
    encapsulate ? encap(a) : scan_a(a)
  else
    puts 'before scan_a' if @debug
    scan_a(*a)
  end

  
  
  a3 = @root ? a2.unshift(@root, {}) : a2.flatten(1)
    
  puts 'a3: ' + a3.inspect if @debug  

  Rexle.new(a3)
  
end

#to_hObject


69
70
71
# File 'lib/line-tree.rb', line 69

def to_h()
  build_h(@to_a)
end

#to_html(numbered: false) ⇒ Object


73
74
75
76
77
78
79
80
81
# File 'lib/line-tree.rb', line 73

def to_html(numbered: false)
  
  @numbered = numbered
  s = "<ul>%s</ul>" % make_html_list(@to_a)
  puts ('s: ' + s.inspect).debug if @debug
  puts s if @debug
  Rexle.new(s).xml declaration: false
  
end

#to_treeObject


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/line-tree.rb', line 83

def to_tree()
  
  def make_tree_list(a)
    
    items = a.inject([]) do |r,x|    
      
      if x.is_a? Array then
        r << make_tree_list(x)
      else
        r + ['item', {title: x}]
      end
    end

  end

  a = make_tree_list(@to_a)
  doc = Rexle.new(['tree', {}, *a])
  doc.xml    
  
end

#to_xml(options = {}) ⇒ Object


104
105
106
# File 'lib/line-tree.rb', line 104

def to_xml(options={})
  to_doc.xml(options)
end