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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/line-tree.rb', line 13

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.strip, level)
  
end

Instance Attribute Details

#to_aObject (readonly)

Returns the value of attribute to_a.



11
12
13
# File 'lib/line-tree.rb', line 11

def to_a
  @to_a
end

Instance Method Details

#make_tree_list(a) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/line-tree.rb', line 68

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/line-tree.rb', line 35

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_html(numbered: false) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/line-tree.rb', line 56

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/line-tree.rb', line 66

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



87
88
89
# File 'lib/line-tree.rb', line 87

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