handlebarsjs 0.2.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 596f9e558bf5308c4fe912079507eb42a0ad15647ffb298f7400487d4d545146
4
- data.tar.gz: e0b7380f763e9fbb808a0246f03c2870f608a860369f621253b5789e20122d7e
3
+ metadata.gz: 151163c301c19424a48a278b6685d86f01c9cde58244de9343a17834d40cef3a
4
+ data.tar.gz: a857eea83005555d1541e623f081950e739d394a3b511de944e62dbd8d4851b9
5
5
  SHA512:
6
- metadata.gz: 665d312a9a202e08085879dd57b9ed6577318de92e29f3843297ca68f4a506c203fc082ad78dbb8574f200489dba0d835c5c32ef6e66fab083d05392b3b54525
7
- data.tar.gz: a3f4b97ab1ac479428bb972c7350767a74a83337da1e03e6612bda7494ed1cac9121f02d62df2de405778c9a2909c47a029309ba0fb5d905f8613eeae8f4957e
6
+ metadata.gz: 7b9f7e84df1bb4afe9c3a651de43f775c27a18072ce060d4045b33b2ed0704f8992491d58c87d2d7b2b9461fdbb0a78167674963e66c1b0c9d2aa3c24ea61d62
7
+ data.tar.gz: dd8774994fe3dd4b2093d7cdffdf6e708de847c790e9ad44420a65b3b9430dc58369e31f5efe5bb483aa6eb3e560936d26fd07aac4855ece300e0c1c60371b31
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ # [0.4.0](https://github.com/klueless-io/handlebarsjs/compare/v0.3.0...v0.4.0) (2022-07-06)
2
+
3
+
4
+ ### Features
5
+
6
+ * add handlebars add_helper support ([19a5636](https://github.com/klueless-io/handlebarsjs/commit/19a5636698515c6c3128040d708d54527ba62dec))
7
+
8
+ # [0.3.0](https://github.com/klueless-io/handlebarsjs/compare/v0.2.0...v0.3.0) (2022-07-06)
9
+
10
+
11
+ ### Features
12
+
13
+ * refactor snapshot builder to handlebars snapshot ([bc6d079](https://github.com/klueless-io/handlebarsjs/commit/bc6d079d12b87f0198ae2a32c94c6349929c8c70))
14
+
15
+ # [0.2.0](https://github.com/klueless-io/handlebarsjs/compare/v0.1.2...v0.2.0) (2022-07-02)
16
+
17
+
18
+ ### Features
19
+
20
+ * add support for basic template processing ([13d9649](https://github.com/klueless-io/handlebarsjs/commit/13d9649c03fa7de0326bed71d08441453194e4e1))
21
+ * add support for basic template processing ([64966e5](https://github.com/klueless-io/handlebarsjs/commit/64966e5ad396910dd0e7edc11a4d8427a10cae29))
22
+
1
23
  ## [0.1.2](https://github.com/klueless-io/handlebarsjs/compare/v0.1.1...v0.1.2) (2022-07-01)
2
24
 
3
25
 
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Handlebarsjs
4
+ # Extend base helper for each of your custom handlebars-helpers
5
+ class BaseHelper
6
+ # Wrap the parse method in a handlebars context aware block
7
+ # and return as a lambda/proc so that it is available to the
8
+ # Handlebars template engine
9
+ def to_proc
10
+ ->(value, _opts) { wrapper(parse(value)) }
11
+ end
12
+
13
+ # All child classes will generally implement this method
14
+ def parse(value)
15
+ value
16
+ end
17
+
18
+ # If you need to wrap the return value in a specific
19
+ # Handlebars Type, eg. SafeString, then you can override this method
20
+ def wrapper(value)
21
+ value
22
+ end
23
+ end
24
+ end
@@ -6,13 +6,14 @@ module Handlebarsjs
6
6
  def initialize
7
7
  super
8
8
  # Handlebars 4.7.7
9
- snapshot_builder.add_library('handlebars', path: Handlebarsjs::HANDLEBARS_LIBRARY_PATH)
9
+ handlebars_snapshot.add_library('handlebars', path: Handlebarsjs::HANDLEBARS_LIBRARY_PATH)
10
10
 
11
11
  # Support functions for working with
12
- snapshot_builder.add_library('handlebars-api', path: Handlebarsjs::HANDLEBARS_API_PATH)
12
+ handlebars_snapshot.add_library('handlebars-api', path: Handlebarsjs::HANDLEBARS_API_PATH)
13
13
  end
14
14
 
15
15
  def process_template(template, options = {})
16
+ # TODO: process template function may be improved with some type of caching
16
17
  context.call('process_template', template, options)
17
18
  end
18
19
  end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Handlebarsjs
4
+ # Wraps MiniRacer snapshot with specific emphasis on
5
+ # loading Handlebars helpers onto the MiniRacer context
6
+ # in the correct order. So that new contexts are preloaded
7
+ # with the handlebars library and the configured helpers.
8
+ class HandlebarsSnapshot
9
+ attr_reader :scripts
10
+ attr_reader :helpers
11
+
12
+ def initialize
13
+ @scripts = []
14
+ @helpers = []
15
+ end
16
+
17
+ def add_library(name, script: nil, path: nil)
18
+ add_script(name, 'library', script: script, path: path)
19
+ end
20
+
21
+ def add_snippet(name, script: nil, path: nil)
22
+ add_script(name, 'snippet', script: script, path: path)
23
+ end
24
+
25
+ def add_helper(name, helper)
26
+ callback = nil
27
+ callback = helper if helper.is_a?(Proc)
28
+ callback = helper.to_proc if callback.nil? && helper.respond_to?(:to_proc)
29
+
30
+ raise Handlebarsjs::Error, 'helper must be a callback or implement to_proc method' if callback.nil?
31
+
32
+ add_helper_callback(name, callback)
33
+ end
34
+
35
+ def register_helper(name)
36
+ add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})")
37
+ end
38
+
39
+ def script
40
+ scripts.map { |script| "// #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n")
41
+ end
42
+
43
+ def snapshot
44
+ @snapshot ||= MiniRacer::Snapshot.new(script)
45
+ end
46
+
47
+ # rubocop:disable Style/DocumentDynamicEvalDefinition
48
+ def new_context
49
+ context = MiniRacer::Context.new(snapshot: snapshot)
50
+
51
+ helpers.each do |helper|
52
+ context.attach("ruby_#{helper[:name]}", helper[:callback])
53
+ context.eval("Handlebars.registerHelper('#{helper[:name]}', ruby_#{helper[:name]})")
54
+ end
55
+
56
+ context
57
+ end
58
+ # rubocop:enable Style/DocumentDynamicEvalDefinition
59
+
60
+ # not currently used
61
+ def dirty?
62
+ @snapshot.nil?
63
+ end
64
+
65
+ def debug
66
+ puts script
67
+ end
68
+
69
+ private
70
+
71
+ def add_script(name, type, script: nil, path: nil)
72
+ raise Handlebarsjs::Error, 'script or path is required' if script.nil? && path.nil?
73
+ raise Handlebarsjs::Error, 'script and path are mutually exclusive' if script && path
74
+
75
+ script ||= File.read(path)
76
+ add_script_item(name, type, script, path)
77
+ end
78
+
79
+ def add_helper_callback(name, callback)
80
+ @helpers << {
81
+ name: name,
82
+ callback: callback
83
+ }
84
+ end
85
+
86
+ def add_script_item(name, type, script, path = nil)
87
+ @snapshot = nil
88
+ scripts << {
89
+ name: name,
90
+ type: type,
91
+ script: script,
92
+ path: path
93
+ }
94
+ end
95
+ end
96
+ end
@@ -5,6 +5,7 @@
5
5
 
6
6
  // This is the main function that is called by the client.
7
7
  function process_template(template, data) {
8
+ // The process template function may be improved with some type of caching
8
9
  const compiled_template = Handlebars.compile(template);
9
10
  return compiled_template(data);
10
11
  }
@@ -3,10 +3,10 @@
3
3
  module Handlebarsjs
4
4
  # API for interacting with Javascript while providing native Ruby helpers
5
5
  class Javascript
6
- attr_reader :snapshot_builder
6
+ attr_reader :handlebars_snapshot
7
7
 
8
8
  def initialize
9
- @snapshot_builder = SnapshotBuilder.new
9
+ @handlebars_snapshot = HandlebarsSnapshot.new
10
10
  end
11
11
 
12
12
  def eval(script)
@@ -24,7 +24,8 @@ module Handlebarsjs
24
24
  private
25
25
 
26
26
  def context
27
- @context ||= MiniRacer::Context.new(snapshot: snapshot_builder.build)
27
+ puts 'Snapshot and context are out of date, calls to snapshot should happen before any calls to context' if !@context.nil? && handlebars_snapshot.dirty?
28
+ @context ||= handlebars_snapshot.new_context
28
29
  end
29
30
  end
30
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Handlebarsjs
4
- VERSION = '0.2.0'
4
+ VERSION = '0.5.0'
5
5
  end
data/lib/handlebarsjs.rb CHANGED
@@ -2,9 +2,10 @@
2
2
 
3
3
  require 'mini_racer'
4
4
  require_relative 'handlebarsjs/version'
5
- require_relative 'handlebarsjs/snapshot_builder'
6
5
  require_relative 'handlebarsjs/javascript'
6
+ require_relative 'handlebarsjs/handlebars_snapshot'
7
7
  require_relative 'handlebarsjs/handlebars'
8
+ require_relative 'handlebarsjs/base_helper'
8
9
 
9
10
  module Handlebarsjs
10
11
  HANDLEBARS_LIBRARY_PATH = 'lib/handlebarsjs/javascript/handlebars-4.7.7.js'
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "handlebarsjs",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "handlebarsjs",
9
- "version": "0.2.0",
9
+ "version": "0.5.0",
10
10
  "devDependencies": {
11
11
  "@klueless-js/semantic-release-rubygem": "github:klueless-js/semantic-release-rubygem",
12
12
  "@semantic-release/changelog": "^6.0.1",
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "handlebarsjs",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "description": "handlebarsjs GEM wraps the handlebars.js library and provides ruby/javascript interoperability",
5
5
  "scripts": {
6
6
  "release": "semantic-release"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebarsjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-02 00:00:00.000000000 Z
11
+ date: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: k_log
@@ -71,13 +71,13 @@ files:
71
71
  - docs/project-plan/project_in_progress.svg
72
72
  - lib/handlebarsjs.rb
73
73
  - lib/handlebarsjs/api.rb
74
+ - lib/handlebarsjs/base_helper.rb
74
75
  - lib/handlebarsjs/handlebars.rb
75
- - lib/handlebarsjs/helpers/comparison/and.rb
76
+ - lib/handlebarsjs/handlebars_snapshot.rb
76
77
  - lib/handlebarsjs/javascript.rb
77
78
  - lib/handlebarsjs/javascript/handlebars-4.7.7.js
78
79
  - lib/handlebarsjs/javascript/handlebars-api.js
79
80
  - lib/handlebarsjs/javascript/handlebars-helpers.js
80
- - lib/handlebarsjs/snapshot_builder.rb
81
81
  - lib/handlebarsjs/version.rb
82
82
  - package-lock.json
83
83
  - package.json
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
- # require 'active_support/core_ext/string'
5
-
6
- # require 'handlebars/helpers/base_helper'
7
-
8
- module Handlebarsjs
9
- module Helpers
10
- # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
- module Comparison
12
- # And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
13
- # < Handlebars::Helpers::BaseHelper
14
- class And
15
- # Parse will And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
16
- #
17
- # @example
18
- #
19
- # {{#if (and p1 p2 p3 p4 p5)}}
20
- # found
21
- # {{/if}}
22
- #
23
- # @example
24
- #
25
- # @example
26
- # {{#if (and name age)}}
27
- # {{name}}-{{age}}
28
- # {{else}}
29
- # no name or age
30
- # {{/if}}
31
- #
32
- # @param values list of values (via *splat) to be checked via AND condition
33
- # @return [String] return block when every value is truthy
34
- def parse(values)
35
- values.all? { |value| value }
36
- end
37
-
38
- def handlebars_helper
39
- # Exclude last paramater which is the context V8::Object
40
- proc { |_context, *values| wrapper(parse(values[0..-2])) }
41
- end
42
- end
43
- end
44
- end
45
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Handlebarsjs
4
- # Wraps MiniRacer snapshot and provides clean API
5
- class SnapshotBuilder
6
- attr_reader :scripts
7
-
8
- def initialize
9
- @scripts = []
10
- end
11
-
12
- def add_library(name, script: nil, path: nil)
13
- add_script(name, 'library', script: script, path: path)
14
- end
15
-
16
- def register_helper(name)
17
- add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})")
18
- end
19
-
20
- def script
21
- scripts.map { |script| "// #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n")
22
- end
23
-
24
- def build
25
- @build ||= MiniRacer::Snapshot.new(script)
26
- end
27
-
28
- private
29
-
30
- def add_script(name, type, script: nil, path: nil)
31
- raise Handlebarsjs::Error, 'script or path is required' if script.nil? && path.nil?
32
- raise Handlebarsjs::Error, 'script and path are mutually exclusive' if script && path
33
-
34
- script ||= File.read(path)
35
- add_script_item(name, type, script, path)
36
- end
37
-
38
- def add_script_item(name, type, script, path = nil)
39
- @build = nil
40
- scripts << {
41
- name: name,
42
- type: type,
43
- script: script,
44
- path: path
45
- }
46
- end
47
- end
48
- end