htx 0.0.9 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +1 -1
- data/VERSION +1 -1
- data/lib/htx/template.rb +42 -48
- data/lib/htx/version.rb +1 -1
- data/lib/htx.rb +0 -14
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83c2b45f8852d11b5faa35989c0552a4135c3270d20aaaa2af8d170e696ad840
|
4
|
+
data.tar.gz: dcbbb3aac2484f55b445a029c9916e282dc75b93197f630b40227110aba02c28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a26b13b0f0cdddf7d9338f1876c23e59adaef4abd2d706cae4a984bd74ca207465e93d6c1d1f0877adf5f27999ddbfd59d93c703ffe098ea7cfc239bc8f391d
|
7
|
+
data.tar.gz: e6ace0fd8c2289e1b2062e6f9e976e017b3e639cfd0eb0777bf5e4bd9ded53d8774c843cfcb1cc55a1c491d0fe6d4f81e337455897ec471bebc0d333db202ba5
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright 2019-
|
1
|
+
Copyright 2019-2023 Nate Pickens
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
4
4
|
documentation files (the "Software"), to deal in the Software without restriction, including without
|
data/README.md
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/htx/template.rb
CHANGED
@@ -17,7 +17,6 @@ module HTX
|
|
17
17
|
'svg' => 'http://www.w3.org/2000/svg',
|
18
18
|
}.freeze
|
19
19
|
|
20
|
-
INDENT_VALID = /^( +|\t+)$/.freeze
|
21
20
|
INDENT_GUESS = /^( +|\t+)(?=\S)/.freeze
|
22
21
|
INDENT_REGEX = /\n(?=[^\n])/.freeze
|
23
22
|
|
@@ -32,17 +31,14 @@ module HTX
|
|
32
31
|
NON_WHITESPACE = /\S/.freeze
|
33
32
|
|
34
33
|
##
|
35
|
-
# Returns
|
36
|
-
# <htx-content>), will return true if Nokogiri's HTML5 parser is available. To use it now, monkey patch
|
37
|
-
# this method to return true.
|
34
|
+
# Returns true if Nokogiri's HTML5 parser is available.
|
38
35
|
#
|
39
36
|
def self.html5_parser?
|
40
|
-
|
37
|
+
defined?(Nokogiri::HTML5)
|
41
38
|
end
|
42
39
|
|
43
40
|
##
|
44
|
-
# Returns Nokogiri's HTML5 parser if available
|
45
|
-
# otherwise.
|
41
|
+
# Returns Nokogiri's HTML5 parser if available or Nokogiri's default HTML (4) parser otherwise.
|
46
42
|
#
|
47
43
|
def self.nokogiri_parser
|
48
44
|
html5_parser? ? Nokogiri::HTML5 : Nokogiri::HTML
|
@@ -62,24 +58,11 @@ module HTX
|
|
62
58
|
##
|
63
59
|
# Compiles the HTX template.
|
64
60
|
#
|
65
|
-
# * +assign_to+ - JavaScript object to assign the template function to (default: +
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
def compile(assign_to: nil, indent: (indent_omitted = true; nil))
|
71
|
-
@assign_to = assign_to || 'window'
|
72
|
-
@indent =
|
73
|
-
if indent.kind_of?(Numeric)
|
74
|
-
' ' * indent
|
75
|
-
elsif indent && !indent.match?(INDENT_VALID)
|
76
|
-
raise("Invalid indent value #{indent.inspect}: only spaces and tabs (but not both) are allowed")
|
77
|
-
else
|
78
|
-
indent || @content[INDENT_GUESS] || INDENT_DEFAULT
|
79
|
-
end
|
80
|
-
|
81
|
-
warn('The indent: option for HTX template compilation is deprecated.') unless indent_omitted
|
82
|
-
|
61
|
+
# * +assign_to+ - JavaScript object to assign the template function to (default: +globalThis+).
|
62
|
+
#
|
63
|
+
def compile(assign_to: nil)
|
64
|
+
@assign_to = assign_to || 'globalThis'
|
65
|
+
@base_indent = @indent = @content[INDENT_GUESS] || INDENT_DEFAULT
|
83
66
|
@static_key = 0
|
84
67
|
@close_count = 0
|
85
68
|
@whitespace_buff = nil
|
@@ -158,14 +141,35 @@ module HTX
|
|
158
141
|
# * +node+ - Nokogiri node to process.
|
159
142
|
#
|
160
143
|
def process_fragment_node(node)
|
161
|
-
append(
|
162
|
-
|
144
|
+
append(
|
145
|
+
<<~JS
|
146
|
+
#{@assign_to}['#{@name}'] = ((HTX) => {
|
147
|
+
#{@indent}function render($rndr) {
|
148
|
+
JS
|
149
|
+
)
|
150
|
+
|
151
|
+
@indent = @base_indent * 2
|
163
152
|
|
164
153
|
node.children.each do |child|
|
165
154
|
process(child)
|
166
155
|
end
|
167
156
|
|
168
|
-
append("\n
|
157
|
+
append("\n\n#{@indent}return $rndr.rootNode")
|
158
|
+
|
159
|
+
@indent = @base_indent
|
160
|
+
|
161
|
+
append(
|
162
|
+
+<<~JS
|
163
|
+
|
164
|
+
#{@indent}}
|
165
|
+
|
166
|
+
#{@indent}return function Template(context) {
|
167
|
+
#{@indent * 2}this.render = render.bind(context, new HTX.Renderer)
|
168
|
+
#{@indent}}
|
169
|
+
})(globalThis.HTX ||= {});
|
170
|
+
JS
|
171
|
+
)
|
172
|
+
|
169
173
|
flush
|
170
174
|
end
|
171
175
|
|
@@ -179,15 +183,10 @@ module HTX
|
|
179
183
|
children = node.children
|
180
184
|
childless = children.empty? || (children.size == 1 && self.class.formatting_node?(children.first))
|
181
185
|
dynamic_key = self.class.attribute_value(node.attr(DYNAMIC_KEY_ATTR))
|
182
|
-
attributes = self.class.process_attributes(node)
|
186
|
+
attributes = self.class.process_attributes(node, xmlns: xmlns)
|
183
187
|
xmlns ||= !!self.class.namespace(node)
|
184
188
|
|
185
189
|
if self.class.htx_content_node?(node)
|
186
|
-
if node.name != CONTENT_TAG
|
187
|
-
warn("#{@name}:#{node.line}: The <#{node.name}> tag has been deprecated. Use <#{CONTENT_TAG}> "\
|
188
|
-
"for identical functionality.")
|
189
|
-
end
|
190
|
-
|
191
190
|
if attributes.size > 0
|
192
191
|
raise(MalformedTemplateError.new("<#{node.name}> tags may not have attributes other than "\
|
193
192
|
"#{DYNAMIC_KEY_ATTR}", @name, node))
|
@@ -269,7 +268,7 @@ module HTX
|
|
269
268
|
close_count = @close_count
|
270
269
|
@close_count = 0
|
271
270
|
|
272
|
-
append("
|
271
|
+
append("$rndr.close(#{close_count unless close_count == 1})")
|
273
272
|
end
|
274
273
|
|
275
274
|
if @whitespace_buff
|
@@ -300,7 +299,7 @@ module HTX
|
|
300
299
|
args << 0 unless args.last.kind_of?(Integer)
|
301
300
|
args[-1] |= (@static_key += 1) << FLAG_BITS
|
302
301
|
|
303
|
-
append("
|
302
|
+
append("$rndr.node(#{args.join(', ')})")
|
304
303
|
end
|
305
304
|
|
306
305
|
##
|
@@ -336,7 +335,7 @@ module HTX
|
|
336
335
|
# * +node+ - Nokogiri node to check.
|
337
336
|
#
|
338
337
|
def self.htx_content_node?(node)
|
339
|
-
node
|
338
|
+
node&.name == CONTENT_TAG
|
340
339
|
end
|
341
340
|
|
342
341
|
##
|
@@ -344,10 +343,10 @@ module HTX
|
|
344
343
|
#
|
345
344
|
# * +node+ - Nokogiri node to process the attributes of.
|
346
345
|
#
|
347
|
-
def self.process_attributes(node)
|
346
|
+
def self.process_attributes(node, xmlns:)
|
348
347
|
attributes = []
|
349
348
|
|
350
|
-
if !node.attribute('xmlns') && (xmlns = namespace(node))
|
349
|
+
if !xmlns && !node.attribute('xmlns') && (xmlns = namespace(node))
|
351
350
|
attributes.push(
|
352
351
|
attribute_name('xmlns'),
|
353
352
|
attribute_value(xmlns)
|
@@ -402,15 +401,10 @@ module HTX
|
|
402
401
|
text ? TextParser.new(text, statement_allowed: false).parse : nil
|
403
402
|
end
|
404
403
|
|
405
|
-
# The Nokogiri
|
406
|
-
#
|
407
|
-
#
|
408
|
-
#
|
409
|
-
# Note: Nokogiri's newer HTML5 parser resulting from the Nokogumbo merge fixes this issue, but it is
|
410
|
-
# currently not available for JRuby. It also does not parse <:> as a tag, which is why it's been
|
411
|
-
# deprecated in favor of <htx-content>. Once support for <:> has been completely removed, the HTML5
|
412
|
-
# parser will be used for regular Ruby and this tag and attribute mapping hack reserved for JRuby (and
|
413
|
-
# any other potential environments where the HTML5 parser is not available).
|
404
|
+
# The Nokogiri::HTML5 parser is used whenever possible, which correctly handles mix-cased SVG tag and
|
405
|
+
# attribute names. But when falling back to the Nokogiri::HTML parser (e.g. in JRuby environments where
|
406
|
+
# Nokogiri::HTML5 is not available), all tag and attribute names get downcased. These maps are used to
|
407
|
+
# restore the correct case.
|
414
408
|
|
415
409
|
# Source: https://developer.mozilla.org/en-US/docs/Web/SVG/Element
|
416
410
|
TAG_MAP = %w[
|
data/lib/htx/version.rb
CHANGED
data/lib/htx.rb
CHANGED
@@ -21,18 +21,4 @@ module HTX
|
|
21
21
|
def self.compile(name, content, options = EMPTY_HASH)
|
22
22
|
Template.new(name, content).compile(**options)
|
23
23
|
end
|
24
|
-
|
25
|
-
##
|
26
|
-
# DEPRECATED. Use HTX::Template.new instead. HTX was formerly a class that would be instantiated for
|
27
|
-
# compilation. This method allows HTX.new calls to continue working (but support will be removed in the
|
28
|
-
# near future).
|
29
|
-
#
|
30
|
-
# * +name+ - Template name. Conventionally the path of the template file.
|
31
|
-
# * +content+ - Template content.
|
32
|
-
#
|
33
|
-
def self.new(name, content)
|
34
|
-
warn('HTX.new is deprecated. Please use HTX::Template.new instead.')
|
35
|
-
|
36
|
-
Template.new(name, content)
|
37
|
-
end
|
38
24
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: htx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Pickens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
96
|
- !ruby/object:Gem::Version
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
|
-
rubygems_version: 3.
|
99
|
+
rubygems_version: 3.4.6
|
100
100
|
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: A Ruby compiler for HTX templates.
|