Class: JsTreeBuilder::TreeBuilder
- Inherits:
-
Object
- Object
- JsTreeBuilder::TreeBuilder
- Defined in:
- lib/jstreebuilder.rb
Instance Attribute Summary collapse
-
#to_tree ⇒ Object
readonly
Returns the value of attribute to_tree.
Instance Method Summary collapse
-
#initialize(s, debug: false) ⇒ TreeBuilder
constructor
A new instance of TreeBuilder.
- #make_tree(a, indent = 0, hn = 1) ⇒ Object
- #scan_headings(s, n = 1) ⇒ Object
Constructor Details
#initialize(s, debug: false) ⇒ TreeBuilder
Returns a new instance of TreeBuilder.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/jstreebuilder.rb', line 136 def initialize(s, debug: false) @debug = debug html = Kramdown::Document.new(s).to_html puts ('html: ' + html.inspect) if @debug a = scan_headings(html) puts ('a: ' + a.inspect) if @debug s2 = make_tree(a) puts ('s2: ' + s2.inspect) if @debug tree = LineTree.new(s2).to_tree puts ('tree: ' + tree.inspect).debug if @debug doc = Rexle.new(tree) doc.root.each_recursive do |node| h = node.attributes puts ('h: ' + h.inspect).debug if @debug h[:url] = '#' + h[:title].strip.downcase.gsub(' ', '-') end puts ('doc.xml: ' + doc.xml.inspect) if @debug @to_tree = doc.xml pretty: true end |
Instance Attribute Details
#to_tree ⇒ Object (readonly)
Returns the value of attribute to_tree.
134 135 136 |
# File 'lib/jstreebuilder.rb', line 134 def to_tree @to_tree end |
Instance Method Details
#make_tree(a, indent = 0, hn = 1) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jstreebuilder.rb', line 164 def make_tree(a, indent=0, hn=1) if @debug then puts 'inside make_tree'.debug puts ('a: ' + a.inspect).debug end a.map.with_index do |x, i| puts ('x: ' + x.inspect).debug if @debug if x.is_a? Array then puts 'before make_tree()'.info if @debug make_tree(x, indent+1, hn) else next unless x =~ /<h[#{hn}-4]/ space = i == 0 ? indent-1 : indent heading = (' ' * space) + x[/(?<=\>)[^<]+/] puts ('heading: ' + heading.inspect).debug if @debug heading end end.compact.join("\n") end |
#scan_headings(s, n = 1) ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/jstreebuilder.rb', line 195 def scan_headings(s, n=1) s.split(/(?=<h#{n})/).map do |x| x.include?('<h' + (n+1).to_s) ? scan_headings(x, n+1) : x end end |