jstreebuilder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f98c98e0ff99dffced50db848f6f9ce57845c10c097bef510a26ceb1b3c8dcb8
4
+ data.tar.gz: 3dae6f056654e23b0ae5c77f59a616702675fdaf9cb66e63fc4ad0b6f60c454b
5
+ SHA512:
6
+ metadata.gz: 3aef11d3b58a5b25b70ad72f9101ec95c8de4d55c8dbf42e765f9fed3999e142befdb5b3eb29d13595ba1a222de571dd16ef7c0213ce1adf3f930477c4b36d86
7
+ data.tar.gz: def0b33e5777d7b780f9d5805cc3f57cee0b2ba5cb82faf697fc71fa02286d1982e5248f80f174e72bc73d9da3f921230a39ec3f9ec7849049c94dfec1fc8628
checksums.yaml.gz.sig ADDED
Binary file
@@ -0,0 +1,214 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: jstreebuilder.rb
4
+
5
+ require 'nokogiri'
6
+ require 'polyrex-xslt'
7
+ require 'polyrex'
8
+
9
+
10
+
11
+ XSLT = %q[
12
+ <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
13
+ <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
14
+ <xsl:template match='entries'>
15
+
16
+ <xsl:element name='ul'>
17
+ <xsl:attribute name='id'>myUL</xsl:attribute>
18
+ <xsl:apply-templates select='records/entry' />
19
+ </xsl:element>
20
+
21
+ </xsl:template>
22
+
23
+ <xsl:template match='entry'>
24
+
25
+ <xsl:choose>
26
+ <xsl:when test='records/entry'>
27
+
28
+ <xsl:element name='li'>
29
+
30
+ <span class="caret"><xsl:value-of select='summary/title'/></span>
31
+ <ul class='nested'>
32
+ <xsl:apply-templates select='records/entry' />
33
+ </ul>
34
+ </xsl:element>
35
+
36
+ </xsl:when>
37
+ <xsl:otherwise>
38
+ <xsl:element name='li'>
39
+ <xsl:value-of select='summary/title'/>
40
+ </xsl:element>
41
+ </xsl:otherwise>
42
+ </xsl:choose>
43
+
44
+ </xsl:template>
45
+
46
+ </xsl:stylesheet>
47
+ ]
48
+
49
+
50
+ class JsTreeBuilder
51
+ using ColouredText
52
+
53
+ TREE_CSS = %q[
54
+ /* Remove default bullets */
55
+ ul, #myUL {
56
+ list-style-type: none;
57
+ }
58
+
59
+ /* Remove margins and padding from the parent ul */
60
+ #myUL {
61
+ margin: 0;
62
+ padding: 0;
63
+ }
64
+
65
+ /* Style the caret/arrow */
66
+ .caret {
67
+ cursor: pointer;
68
+ user-select: none; /* Prevent text selection */
69
+ }
70
+
71
+ /* Create the caret/arrow with a unicode, and style it */
72
+ .caret::before {
73
+ content: "\25B6";
74
+ color: black;
75
+ display: inline-block;
76
+ margin-right: 6px;
77
+ }
78
+
79
+ /* Rotate the caret/arrow icon when clicked on (using JavaScript) */
80
+ .caret-down::before {
81
+ transform: rotate(90deg);
82
+ }
83
+
84
+ /* Hide the nested list */
85
+ .nested {
86
+ display: none;
87
+ }
88
+
89
+ /* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
90
+ .active {
91
+ display: block;
92
+ }
93
+ ]
94
+
95
+ TREE_JS =<<EOF
96
+ var toggler = document.getElementsByClassName("caret");
97
+ var i;
98
+
99
+ for (i = 0; i < toggler.length; i++) {
100
+ toggler[i].addEventListener("click", function() {
101
+ this.parentElement.querySelector(".nested").classList.toggle("active");
102
+ this.classList.toggle("caret-down");
103
+ });
104
+ }
105
+ EOF
106
+
107
+
108
+ attr_reader :html, :css, :js
109
+
110
+ def initialize(unknown=nil, options={})
111
+
112
+ if unknown.is_a? String or unknown.is_a? Symbol then
113
+ type = unknown.to_sym
114
+ elsif unknown.is_a? Hash
115
+ options = unknown
116
+ end
117
+
118
+ @debug = options[:debug]
119
+
120
+ @types = %i(tree)
121
+
122
+ build(type, options) if type
123
+
124
+ end
125
+
126
+ def to_css()
127
+ @css
128
+ end
129
+
130
+ def to_html()
131
+ @html
132
+ end
133
+
134
+ def to_js()
135
+ @js
136
+ end
137
+
138
+ def to_ul()
139
+ @ul
140
+ end
141
+
142
+ def to_webpage()
143
+
144
+ a = RexleBuilder.build do |xml|
145
+ xml.html do
146
+ xml.head do
147
+ xml.meta name: "viewport", content: \
148
+ "width=device-width, initial-scale=1"
149
+ xml.style "\nbody {font-family: Arial;}\n\n" + @css
150
+ end
151
+ xml.body @ul
152
+ end
153
+ end
154
+
155
+ doc = Rexle.new(a)
156
+
157
+ doc.root.element('body').add \
158
+ Rexle::Element.new('script').add_text "\n" +
159
+ @js.gsub(/^ +\/\/[^\n]+\n/,'')
160
+
161
+ "<!DOCTYPE html>\n" + doc.xml(pretty: true, declaration: false)\
162
+ .gsub(/<\/div>/,'\0' + "\n").gsub(/\n *<!--[^>]+>/,'')
163
+
164
+ end
165
+
166
+ def to_xml()
167
+ @xml
168
+ end
169
+
170
+
171
+ private
172
+
173
+ def build(type, options)
174
+
175
+ puts 'inside build'.info if @debug
176
+ puts "type: %s\noptions: %s".debug % [type, options] if @debug
177
+
178
+ return unless @types.include? type.to_sym
179
+
180
+ s = method(type.to_sym).call(options)
181
+
182
+ @html = s.gsub(/<\/div>/,'\0' + "\n").strip.lines[1..-2]\
183
+ .map {|x| x.sub(/^ /,'') }.join
184
+
185
+ @css = Object.const_get 'JsTreeBuilder::' + type.to_s.upcase + '_CSS'
186
+ @js = Object.const_get 'JsTreeBuilder::' + type.to_s.upcase + '_JS'
187
+
188
+ end
189
+
190
+
191
+ def tree(opt={})
192
+
193
+ tree = opt[:xml]
194
+
195
+ schema = 'entries/entry[title]'
196
+ xslt_schema = 'tree/item[@title:title]'
197
+
198
+ # transform the tree xml into a polyrex document
199
+ pxsl = PolyrexXSLT.new(schema: schema, xslt_schema: xslt_schema).to_xslt
200
+ puts 'pxsl: ' + pxsl if @debug
201
+ px = Polyrex.new(Rexslt.new(pxsl, tree).to_s)
202
+
203
+ # transform the polyrex xml into a nested HTML list
204
+ #@ul = Rexslt.new(px.to_xml, XSLT).to_xml
205
+
206
+ doc = Nokogiri::XML(px.to_xml)
207
+ xslt = Nokogiri::XSLT(XSLT)
208
+
209
+ @ul = xslt.transform(doc).to_s.lines[1..-1].join
210
+
211
+ end
212
+
213
+ end
214
+
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jstreebuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIEXjCCAsagAwIBAgIBATANBgkqhkiG9w0BAQsFADAsMSowKAYDVQQDDCFnZW1t
14
+ YXN0ZXIvREM9amFtZXNyb2JlcnRzb24vREM9ZXUwHhcNMTkwODA1MjExNDQ4WhcN
15
+ MjAwODA0MjExNDQ4WjAsMSowKAYDVQQDDCFnZW1tYXN0ZXIvREM9amFtZXNyb2Jl
16
+ cnRzb24vREM9ZXUwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCxe/SK
17
+ P63fEkWwc5zLyS7cQUh1CpPliXajuIL1pUxMyF0FBxs+GId3IZhCF8/3+RMVnz/e
18
+ Gs0J9nDBrCctAHSi6VsjFocmiXqu96sx9Fxfs5wTqyT4fD22LoTBNVUZ0pZfxaBm
19
+ G6ztBF1dmnrmsSCtLnqioX2rQFyrbIty+y0GkKGbqiV2CMKheQXvfg/4TOElklND
20
+ jGe1sMwfvOKKR0Xrwfp6V/AU4ecf9jLqVttS+cPspdDowzhDrHmWBGhIEo0ccRXb
21
+ OpTN30OrA+q/WVZBUIiySYcpW1W8JJFhLDi0WMfoPoQ5C2D91adRaQKHwZT4iDen
22
+ usRE9oHWuebCV8wW/I2MqexyBrbDu2k9qQryhufDxeRZT1dbx7meNPSHQ1Ktm21D
23
+ qOOPYpl7OlSr+UP7NwPBWxXB3jLxhe5onyIXKTP/vUQ19Swo2Kx2YJyWpOfrNOOA
24
+ ODSx83g/N/FdgI+dUkAPvtefU9+IQuCRWsQAk1XycafEFcTkNVD5D0Hqp6sCAwEA
25
+ AaOBijCBhzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU54E03Y+Y
26
+ 1F5Sszs2IG3vqhesBXYwJgYDVR0RBB8wHYEbZ2VtbWFzdGVyQGphbWVzcm9iZXJ0
27
+ c29uLmV1MCYGA1UdEgQfMB2BG2dlbW1hc3RlckBqYW1lc3JvYmVydHNvbi5ldTAN
28
+ BgkqhkiG9w0BAQsFAAOCAYEAW1T6VR0ML0Xu0ryrjPVwPa3x5h2tj/YcHEmWDre+
29
+ 7nN5I8Rrgg76JYkjBWIf3Dr2WYPsga4w5NK0wPScphv5U9cwfIA5r0gFroflcug+
30
+ DlC5yiNmOyih46eCeoLfyzmIORM0Ejn0Vq2pqcyEW+vEUB1EFvqMKgfcdRfplKil
31
+ qT2TvvEPSatX8dBwGXEEE03ID2NA4xU/fXEfLu22zO1aMKuwSNJm1wnJAcErkVnF
32
+ ddfalc7UNj3/gt2eE3EZo1x1U7Gbnwzix6AU2XFAzjMhlVgg+iCW4ie2tkaEF52z
33
+ 3JGvpw6sa5K3VjrnW146I3D1yhCuCToLwLZ4evDjS9HWifxzwWBQmAMhvZf4YOCv
34
+ uBJ1bPJvkq1hhYuPfw6FEXgyodyKcU98x77XUgJk7leb6jN1nXiWx5Ox1/TQL8WX
35
+ /YHvF368/KpwUw6rBLVUbFvCsNvDTe+bPAG72P5om2g/aope7mb9Qc4kATQgWiys
36
+ rqRI7UAQ3N9ojGSXl6KSySfi
37
+ -----END CERTIFICATE-----
38
+ date: 2019-08-05 00:00:00.000000000 Z
39
+ dependencies:
40
+ - !ruby/object:Gem::Dependency
41
+ name: nokogiri
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.10'
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 1.10.3
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.10'
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 1.10.3
60
+ - !ruby/object:Gem::Dependency
61
+ name: polyrex
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.3.0
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.3.0
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.3'
80
+ - !ruby/object:Gem::Dependency
81
+ name: polyrex-xslt
82
+ requirement: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 0.2.0
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.2.0
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '0.2'
100
+ description:
101
+ email: james@jamesrobertson.eu
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - lib/jstreebuilder.rb
107
+ homepage: https://github.com/jrobertson/jstreebuilder
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.0.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Generates an HTML tree from XML.
130
+ test_files: []
metadata.gz.sig ADDED
Binary file