jstreebuilder 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98ce236d808f41da975ffe805ae3d50535f73395e875d1f51aaaec7eb981044a
4
- data.tar.gz: fbbfdeacf84b7dd83c721051336b79002772aa17ec75aeab1db66108b150c0e0
3
+ metadata.gz: a34efda76d17c49fe344f4cbdf3ac4e1e12f0ea04015838f51fdba695fe8c6fb
4
+ data.tar.gz: 7d7748ad8a279fe205c0832b610463b26d7f5223235da94e5ed8d0a0c264fd92
5
5
  SHA512:
6
- metadata.gz: 8390cee9564d3164fb91a1ee16f2390b4c18a85f0bc9ec4d99bcc5b6dbe915fadc2f76f2abf664a8bc19a1c0a581dab690639b963346c0fc9c188e3af88281a7
7
- data.tar.gz: 138552607bff3a5d7142c68302df4c413dd69a0056cbd8fba31d1c3f10b4dabeb8806e9765a31ada1a2f6b8407b748e303a43f24ed5808575690a3041248425b
6
+ metadata.gz: e93d1ed1bfb1d5607f2a7b41dea63503fbdf6bfc45a4d1a6c7b45922ebfefeb36b52346af29eda10d600f286ab578ef1653150447dcc79164c8fecb737156505
7
+ data.tar.gz: 1851f88fd3da5fde59ec8d91ff9e6815b3037cc6562167644f7be0fef9407d57c6079d221113a60512e0181906b1a5edb3029004195f709130d87d3fc781a010
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/jstreebuilder.rb CHANGED
@@ -5,7 +5,7 @@
5
5
  require 'nokogiri'
6
6
  require 'polyrex-xslt'
7
7
  require 'polyrex'
8
-
8
+ require 'kramdown'
9
9
 
10
10
 
11
11
  XSLT = %q[
@@ -28,7 +28,20 @@ XSLT = %q[
28
28
 
29
29
  <xsl:element name='li'>
30
30
 
31
- <span class="caret"><xsl:value-of select='summary/title'/></span>
31
+ <xsl:element name='span'>
32
+ <xsl:attribute name="class">caret</xsl:attribute>
33
+ <xsl:choose>
34
+ <xsl:when test='summary/url != ""'>
35
+ <xsl:element name='a'>
36
+ <xsl:attribute name='href'><xsl:value-of select='summary/url'/></xsl:attribute>
37
+ <xsl:value-of select='summary/title'/>
38
+ </xsl:element>
39
+ </xsl:when>
40
+ <xsl:otherwise>
41
+ <xsl:value-of select='summary/title'/>
42
+ </xsl:otherwise>
43
+ </xsl:choose>
44
+ </xsl:element>
32
45
  <ul class='nested'>
33
46
  <xsl:apply-templates select='records/entry' />
34
47
  </ul>
@@ -115,15 +128,90 @@ for (i = 0; i < toggler.length; i++) {
115
128
  }
116
129
  EOF
117
130
 
131
+ class TreeBuilder
132
+ using ColouredText
133
+
134
+ attr_reader :to_tree
135
+
136
+ def initialize(s, debug: false)
137
+
138
+ @debug = debug
139
+ html = Kramdown::Document.new(s).to_html
140
+ puts ('html: ' + html.inspect) if @debug
141
+ a = scan_headings(html)
142
+ puts ('a: ' + a.inspect) if @debug
143
+
144
+ s2 = make_tree(a)
145
+ puts ('s2: ' + s2.inspect) if @debug
146
+ tree = LineTree.new(s2).to_tree
147
+
148
+ puts ('tree: ' + tree.inspect).debug if @debug
149
+
150
+ doc = Rexle.new(tree)
151
+ doc.root.each_recursive do |node|
152
+
153
+ h = node.attributes
154
+ puts ('h: ' + h.inspect).debug if @debug
155
+ h[:url] = '#' + h[:title].strip.downcase.gsub(' ', '-')
156
+
157
+ end
158
+ puts ('doc.xml: ' + doc.xml.inspect) if @debug
159
+
160
+ @to_tree = doc.xml pretty: true
161
+
162
+ end
163
+
164
+ def make_tree(a, indent=0, hn=1)
165
+
166
+ if @debug then
167
+ puts 'inside make_tree'.debug
168
+ puts ('a: ' + a.inspect).debug
169
+ end
170
+
171
+ a.map.with_index do |x, i|
172
+
173
+ puts ('x: ' + x.inspect).debug if @debug
174
+
175
+ if x.is_a? Array then
176
+
177
+ puts 'before make_tree()'.info if @debug
178
+
179
+ make_tree(x, indent+1, hn)
180
+
181
+ else
182
+
183
+ next unless x =~ /<h[#{hn}-4]/
184
+ space = i == 0 ? indent-1 : indent
185
+ heading = (' ' * space) + x[/(?<=\>)[^<]+/]
186
+ puts ('heading: ' + heading.inspect).debug if @debug
187
+ heading
188
+
189
+ end
190
+
191
+ end.compact.join("\n")
192
+
193
+ end
194
+
195
+ def scan_headings(s, n=1)
196
+
197
+ s.split(/(?=<h#{n})/).map do |x|
198
+ x.include?('<h' + (n+1).to_s) ? scan_headings(x, n+1) : x
199
+ end
200
+
201
+ end
202
+
203
+ end
204
+
118
205
 
119
206
  attr_reader :html, :css, :js
120
207
 
121
208
  def initialize(unknown=nil, options={})
122
209
 
123
210
  if unknown.is_a? String or unknown.is_a? Symbol then
124
- type = unknown.to_sym
211
+ type = unkown.to_sym
125
212
  elsif unknown.is_a? Hash
126
- options = unknown
213
+ options = {type: :tree}.merge(unknown)
214
+ type = options[:type]
127
215
  end
128
216
 
129
217
  @debug = options[:debug]
@@ -198,30 +286,40 @@ EOF
198
286
 
199
287
  end
200
288
 
289
+ def build_px(tree)
290
+
291
+ schema = 'entries/entry[title, url]'
292
+ xslt_schema = 'tree/item[@title:title, @url:url]'
293
+
294
+ # transform the tree xml into a polyrex document
295
+ pxsl = PolyrexXSLT.new(schema: schema, xslt_schema: xslt_schema).to_xslt
296
+ puts 'pxsl: ' + pxsl if @debug
297
+ Rexslt.new(pxsl, tree).to_s
298
+
299
+ end
300
+
201
301
 
202
302
  def tree(opt={})
203
303
 
204
- tree = opt[:src]
304
+ src = opt[:src]
205
305
 
206
- s = if tree =~ /<tree>/ then
306
+ s = if src =~ /<tree>/ then
207
307
 
208
- schema = 'entries/entry[title]'
209
- xslt_schema = 'tree/item[@title:title]'
210
-
211
- # transform the tree xml into a polyrex document
212
- pxsl = PolyrexXSLT.new(schema: schema, xslt_schema: xslt_schema).to_xslt
213
- puts 'pxsl: ' + pxsl if @debug
214
- Rexslt.new(pxsl, tree).to_s
308
+ build_px(src)
215
309
 
216
- elsif tree =~ /<?polyrex /
217
- tree
310
+ elsif src =~ /<?polyrex /
311
+ src
312
+ else
313
+ build_px(TreeBuilder.new(src, debug: @debug).to_tree)
218
314
  end
219
315
 
316
+ puts ('s: ' + s.inspect).debug if @debug
220
317
  px = Polyrex.new(s)
221
318
 
222
319
  # transform the polyrex xml into a nested HTML list
223
320
  #@ul = Rexslt.new(px.to_xml, XSLT).to_xml
224
-
321
+ puts ('px: ' + px.inspect).debug if @debug
322
+ puts ('px.to_xml: ' + px.to_xml.inspect).debug if @debug
225
323
  doc = Nokogiri::XML(px.to_xml)
226
324
  xslt = Nokogiri::XSLT(XSLT)
227
325
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jstreebuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -97,6 +97,26 @@ dependencies:
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
99
  version: 0.2.2
100
+ - !ruby/object:Gem::Dependency
101
+ name: kramdown
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.1.0
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.1'
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 2.1.0
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '2.1'
100
120
  description:
101
121
  email: james@jamesrobertson.eu
102
122
  executables: []
@@ -126,5 +146,5 @@ requirements: []
126
146
  rubygems_version: 3.0.3
127
147
  signing_key:
128
148
  specification_version: 4
129
- summary: Generates an HTML tree from XML.
149
+ summary: Generates an HTML tree from XML or Markdown.
130
150
  test_files: []
metadata.gz.sig CHANGED
Binary file