htx 0.1.0 → 0.1.1

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: 83c2b45f8852d11b5faa35989c0552a4135c3270d20aaaa2af8d170e696ad840
4
- data.tar.gz: dcbbb3aac2484f55b445a029c9916e282dc75b93197f630b40227110aba02c28
3
+ metadata.gz: bbd968d3152b9ef4c7813f1e08139a4dc386ae99f8822758a0eb2574eb8beab5
4
+ data.tar.gz: e2fa1f7035a09e3518be5e3fe5f8c053fd4d7d8022f6f3d121e8caecea180d6a
5
5
  SHA512:
6
- metadata.gz: 8a26b13b0f0cdddf7d9338f1876c23e59adaef4abd2d706cae4a984bd74ca207465e93d6c1d1f0877adf5f27999ddbfd59d93c703ffe098ea7cfc239bc8f391d
7
- data.tar.gz: e6ace0fd8c2289e1b2062e6f9e976e017b3e639cfd0eb0777bf5e4bd9ded53d8774c843cfcb1cc55a1c491d0fe6d4f81e337455897ec471bebc0d333db202ba5
6
+ metadata.gz: 3c15b1d31a60c7447137bd599bb2e7e767896d4f8ebef93878efb5981138beddd7a712744a974ea4150edd9b511593acf1d8ca0cb6754ae04f17ffbc85ce9ed1
7
+ data.tar.gz: adab39540cfe4261e26697a0a6af1081a620ab55305b38abcae5e446a919dfd5a5ac96cc01b5fa3cdd28bbbd3e6755c1c00a98f492530333ef102da43b4c5381
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # HTX Ruby Compiler
2
2
 
3
3
  HTX templates are compiled to JavaScript before being used. This library provides a Ruby implementation of
4
- the compiler. For more information on HTX, see
5
- [https://github.com/npickens/htx](https://github.com/npickens/htx).
4
+ the compiler. For more information on HTX, see the main [README](https://github.com/npickens/htx).
6
5
 
7
6
  ## Installation
8
7
 
@@ -20,24 +19,39 @@ gem install htx
20
19
 
21
20
  ## Usage
22
21
 
23
- To compile an HTX template, pass a name (conventionally the path of the template file) and template content
24
- as strings to the `HTX.compile` method (all other arguments are optional):
22
+ HTX templates can be compiled to either a JavaScript module format or an IIFE assigned to a property on
23
+ either `globalThis` or a custom object. To compile as a module:
25
24
 
26
25
  ```ruby
27
26
  path = '/components/crew.htx'
28
- template = File.read(File.join('some/asset/dir', path))
27
+ content = File.read("/assets#{path}")
28
+
29
+ HTX.compile(path, content, as_module: true, import_path: 'vendor/htx.js')
30
+ # => "import * as HTX from 'vendor/htx.js'
31
+ #
32
+ # ...
33
+ #
34
+ # export function Template { ... }"
35
+ #
36
+ ```
29
37
 
30
- HTX.compile(path, template)
38
+ Note that with the module format the name of the template does not appear anywhere in its compiled form, but
39
+ is still used/useful for tracking down errors when compilation is handled by an overall asset management
40
+ system (such as [Darkroom](https://github.com/npickens/darkroom)).
31
41
 
32
- # globalThis['/components/crew.htx'] = function(htx) {
33
- # // ...
34
- # }
42
+ To compile to an IIFE assigned to a custom object:
35
43
 
36
- HTX.compile(path, template, assign_to: 'myTemplates')
44
+ ```ruby
45
+ HTX.compile(path, content, as_module: false, assign_to: 'myTemplates')
46
+ # => "myTemplates['/components/crew.htx'] = ..."
47
+ ```
37
48
 
38
- # myTemplates['/components/crew.htx'] = function(htx) {
39
- # // ...
40
- # }
49
+ Options can be configured globally so they don't have to be passed to `HTX.compile` every time:
50
+
51
+ ```ruby
52
+ HTX.as_module = true # Default: false
53
+ HTX.import_path = 'vendor/htx.js' # Default: "/htx/htx.js"
54
+ HTX.assign_to = 'myTemplates' # Default: "globalThis"
41
55
  ```
42
56
 
43
57
  ## Contributing
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/htx/template.rb CHANGED
@@ -58,10 +58,14 @@ module HTX
58
58
  ##
59
59
  # Compiles the HTX template.
60
60
  #
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'
61
+ # * +as_module+ - Boolean indicating whether or not to compile as a JavaScript module.
62
+ # * +import_path+ - Path to HTX JavaScript library for module import statement.
63
+ # * +assign_to+ - JavaScript object to assign the template function to if module not being used.
64
+ #
65
+ def compile(as_module: nil, import_path: nil, assign_to: nil)
66
+ @as_module = as_module.nil? ? HTX.as_module : as_module
67
+ @import_path = import_path || HTX.import_path
68
+ @assign_to = assign_to || HTX.assign_to
65
69
  @base_indent = @indent = @content[INDENT_GUESS] || INDENT_DEFAULT
66
70
  @static_key = 0
67
71
  @close_count = 0
@@ -141,32 +145,30 @@ module HTX
141
145
  # * +node+ - Nokogiri node to process.
142
146
  #
143
147
  def process_fragment_node(node)
144
- append(
145
- <<~JS
146
- #{@assign_to}['#{@name}'] = ((HTX) => {
147
- #{@indent}function render($rndr) {
148
- JS
149
- )
148
+ append("#{
149
+ @as_module ? "import * as HTX from '#{@import_path}'\n\n"
150
+ : "#{@assign_to}['#{@name}'] = ((HTX) => {\n#{@indent}"
151
+ }function render($renderer) {\n")
150
152
 
151
- @indent = @base_indent * 2
153
+ @indent = @base_indent * 2 unless @as_module
152
154
 
153
155
  node.children.each do |child|
154
156
  process(child)
155
157
  end
156
158
 
157
- append("\n\n#{@indent}return $rndr.rootNode")
159
+ append("\n\n#{@indent}return $renderer.rootNode")
158
160
 
159
- @indent = @base_indent
161
+ @indent = @as_module ? '' : @base_indent
160
162
 
161
163
  append(
162
- +<<~JS
164
+ <<~JS
163
165
 
164
166
  #{@indent}}
165
167
 
166
- #{@indent}return function Template(context) {
167
- #{@indent * 2}this.render = render.bind(context, new HTX.Renderer)
168
- #{@indent}}
169
- })(globalThis.HTX ||= {});
168
+ #{@indent}#{@as_module ? 'export' : 'return'} function Template(context) {
169
+ #{@indent}#{@base_indent}this.render = render.bind(context, new HTX.Renderer)
170
+ #{@indent}}#{
171
+ "\n})(globalThis.HTX ||= {});" unless @as_module}
170
172
  JS
171
173
  )
172
174
 
@@ -268,7 +270,7 @@ module HTX
268
270
  close_count = @close_count
269
271
  @close_count = 0
270
272
 
271
- append("$rndr.close(#{close_count unless close_count == 1})")
273
+ append("$renderer.close(#{close_count unless close_count == 1})")
272
274
  end
273
275
 
274
276
  if @whitespace_buff
@@ -288,9 +290,9 @@ module HTX
288
290
  end
289
291
 
290
292
  ##
291
- # Appends an +htx.node+ call to the compiled template function string.
293
+ # Appends an +$renderer.node+ call to the compiled template function string.
292
294
  #
293
- # * +args+ - Arguments to use for the +htx.node+ call (any +nil+ ones are removed).
295
+ # * +args+ - Arguments to use for the +$renderer.node+ call (any +nil+ ones are removed).
294
296
  #
295
297
  def append_htx_node(*args)
296
298
  return if args.first.nil?
@@ -299,7 +301,7 @@ module HTX
299
301
  args << 0 unless args.last.kind_of?(Integer)
300
302
  args[-1] |= (@static_key += 1) << FLAG_BITS
301
303
 
302
- append("$rndr.node(#{args.join(', ')})")
304
+ append("$renderer.node(#{args.join(', ')})")
303
305
  end
304
306
 
305
307
  ##
data/lib/htx/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HTX
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/htx.rb CHANGED
@@ -11,6 +11,12 @@ require('htx/version')
11
11
  module HTX
12
12
  EMPTY_HASH = {}.freeze
13
13
 
14
+ @as_module = false
15
+ @import_path = '/htx/htx.js'
16
+ @assign_to = 'globalThis'
17
+
18
+ class << self; attr_accessor(:as_module, :import_path, :assign_to); end
19
+
14
20
  ##
15
21
  # Convenience method to create a new Template instance and compile it.
16
22
  #
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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Pickens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-07 00:00:00.000000000 Z
11
+ date: 2023-07-15 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.4.6
99
+ rubygems_version: 3.4.15
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: A Ruby compiler for HTX templates.