component_embedded_ruby 0.3.1 → 0.4.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: 91b564ee3159a60cbbfe9b257d5555e8b069d1dceb5f6ab994eade21ea7e1ca0
4
- data.tar.gz: da9e56970bd03fb446bbf81b55bbb87a7dabd504207ba3745d33765b9f676ed5
3
+ metadata.gz: '089c24f463e193714b349930149881745fa3c2bbd9e2e9431d8ffef0a0fb4771'
4
+ data.tar.gz: 76c4000440ccb4221775c514021e9b1879fd6aefb7ab4f8fcbf2845169fade2b
5
5
  SHA512:
6
- metadata.gz: 4bbbe258a5ebca28b84dc1c21da65558a5aa05a098aeaf458fdbbc291de1d9b143364b24dac218a1bdb2da474e96d37152cc0bf8672baf6644f69e5f36ff607b
7
- data.tar.gz: 4ed824781bca023e45b1cd6078f7b12b3bee322c42a036221a758d6f5d7ae2b875c292920b68fdddf63339e54e7de2e42cfe81c1223857987256f2caef5f4dff
6
+ metadata.gz: 39aa248edbc731061c12759da94f7a882ac9f61dc218c156ed7155daf8751cc4a569c77ca6ad72f394b1c77d1d5078ceb677c805cafeef829942a87a5dc5fc3f
7
+ data.tar.gz: 467152d8857450fef48ef9b23d82e9f7684b7d86c3d2656a4b3068d8f7af7a81f88c0d59b3547b40ba3e9036d630ca91a3fd20987e70c5a2a7b601b957d040e9
@@ -86,7 +86,7 @@ GIT
86
86
  PATH
87
87
  remote: .
88
88
  specs:
89
- component_embedded_ruby (0.3.1)
89
+ component_embedded_ruby (0.4.0)
90
90
 
91
91
  GEM
92
92
  remote: https://rubygems.org/
@@ -2,16 +2,24 @@
2
2
 
3
3
  module ComponentEmbeddedRuby
4
4
  class Compiler
5
- def initialize(nodes, output_var_name: "__crb_out", skip_return: false)
5
+ def initialize(
6
+ nodes,
7
+ output_var_name: "__crb_out",
8
+ skip_return: false,
9
+ safe_append_method: "<<",
10
+ unsafe_append_method: "<<"
11
+ )
6
12
  @nodes = Array(nodes)
7
13
  @functions = {}
8
14
  @output_var_name = output_var_name
9
15
  @skip_return = skip_return
16
+ @safe_append_method = safe_append_method
17
+ @unsafe_append_method = unsafe_append_method
10
18
  end
11
19
 
12
20
  def to_ruby
13
21
  <<~RUBY
14
- #{output_var_name} = '';
22
+ #{output_var_name} ||= '';
15
23
 
16
24
  #{nodes.map(&method(:render)).join("\n")}
17
25
 
@@ -25,25 +33,25 @@ module ComponentEmbeddedRuby
25
33
  if node.component?
26
34
  <<~RUBY
27
35
  #{children_to_ruby(node)}
28
- #{output_var_name}.<< render(#{node.component_class}.new(#{attributes_for_component(node).join(',')})) { |component|
36
+ #{output_var_name}.#{@safe_append_method} render(#{node.component_class}.new(#{attributes_for_component(node).join(',')})) { |component|
29
37
  __c_#{node.hash.to_s.gsub('-', '_')}
30
38
  };
31
39
  RUBY
32
40
  elsif node.ruby?
33
41
  if node.output_ruby?
34
- "#{output_var_name}.<< (#{node.children.value}).to_s;\n"
42
+ "#{output_var_name}.#{@unsafe_append_method} (#{node.children.value}).to_s;\n"
35
43
  else
36
44
  "#{node.children.value};\n"
37
45
  end
38
46
  elsif node.text?
39
- "#{output_var_name}.<< \"#{node.children}\";\n"
47
+ "#{output_var_name}.#{@safe_append_method} \"#{node.children}\";\n"
40
48
  elsif node.html?
41
49
  <<~RUBY
42
- #{output_var_name}.<< \"<#{node.tag}\";
50
+ #{output_var_name}.#{@safe_append_method} \"<#{node.tag}\";
43
51
  #{attributes_for_tag(node).join("\n")};
44
- #{output_var_name}.<< \">\";
52
+ #{output_var_name}.#{@safe_append_method} \">\";
45
53
  #{node.children.map(&method(:render)).join("\n")}
46
- #{output_var_name}.<< \"</#{node.tag}>\";
54
+ #{output_var_name}.#{@safe_append_method} \"</#{node.tag}>\";
47
55
  RUBY
48
56
  end
49
57
  end
@@ -54,7 +62,9 @@ module ComponentEmbeddedRuby
54
62
  self.class.new(
55
63
  node.children,
56
64
  output_var_name: "__c_#{node.hash.to_s.gsub('-', '_')}",
57
- skip_return: true
65
+ skip_return: true,
66
+ safe_append_method: @safe_append_method,
67
+ unsafe_append_method: @unsafe_append_method
58
68
  ).to_ruby
59
69
  end
60
70
 
@@ -72,13 +82,13 @@ module ComponentEmbeddedRuby
72
82
  node.attributes.map do |key, value|
73
83
  if value.is_a?(Eval)
74
84
  <<~RUBY
75
- #{output_var_name}.<< " #{key}=\\"";
76
- #{output_var_name}.<< (#{value.value}).to_s;
77
- #{output_var_name}.<< "\\"";
85
+ #{output_var_name}.#{@safe_append_method} " #{key}=\\"";
86
+ #{output_var_name}.#{@unsafe_append_method} (#{value.value}).to_s;
87
+ #{output_var_name}.#{@safe_append_method} "\\"";
78
88
  RUBY
79
89
  else
80
90
  <<~RUBY
81
- #{output_var_name}.<< " #{key}=\\"#{value}\\""
91
+ #{output_var_name}.#{@safe_append_method} " #{key}=\\"#{value}\\""
82
92
  RUBY
83
93
  end
84
94
  end
@@ -5,7 +5,17 @@ module ComponentEmbeddedRuby
5
5
  def self.call(template, source = nil)
6
6
  source ||= template.source
7
7
 
8
- Template.new(source).to_ruby
8
+ template_source = Template.new(
9
+ source,
10
+ safe_append_method: "safe_append=",
11
+ unsafe_append_method: "append=",
12
+ output_var_name: "@output_buffer"
13
+ ).to_ruby
14
+
15
+ <<~RUBY
16
+ @output_buffer = ActionView::OutputBuffer.new('')
17
+ #{template_source}
18
+ RUBY
9
19
  end
10
20
  end
11
21
  end
@@ -2,14 +2,27 @@
2
2
 
3
3
  module ComponentEmbeddedRuby
4
4
  class Template
5
- def initialize(template)
5
+ def initialize(
6
+ template,
7
+ safe_append_method: "<<",
8
+ unsafe_append_method: "<<",
9
+ output_var_name: "__crb_out"
10
+ )
6
11
  @template = template
12
+ @safe_append_method = safe_append_method
13
+ @unsafe_append_method = unsafe_append_method
14
+ @output_var_name = output_var_name
7
15
  end
8
16
 
9
17
  def to_ruby
10
18
  tokens = Lexer.new(@template).lex
11
19
  nodes = Parser.parse(tokens)
12
- Compiler.new(nodes).to_ruby
20
+ Compiler.new(
21
+ nodes,
22
+ safe_append_method: @safe_append_method,
23
+ unsafe_append_method: @unsafe_append_method,
24
+ output_var_name: @output_var_name
25
+ ).to_ruby
13
26
  end
14
27
 
15
28
  def to_s(binding: TOPLEVEL_BINDING)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ComponentEmbeddedRuby
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: component_embedded_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Williams