slimi 0.5.1 → 0.6.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: 36247783488eef0fc3711f969b37bc5b0cbc06b40086e7c524dfa4f46bba2823
4
- data.tar.gz: 63e12ac1f2d178840b4d956b3c62195ce3601a3c212b1c48e5cc65ce7ca61a24
3
+ metadata.gz: 12b1a25a767a9ca17da4d9d99cf520d857131cd9d28acaf73d18a7696354c694
4
+ data.tar.gz: fee58aeb65387fd4846e74ce6885bb96e97dca9db3f2f95578693e20752249a6
5
5
  SHA512:
6
- metadata.gz: 549fc053c09c2283042f7bad2200d79d07e970d97c3aadacbd38e5fbbff54534bb0918b145580c23f1d4ffb2cba760167f09c6bc2b41de71c0b457c76c981f18
7
- data.tar.gz: af6cc99ec67062a742f8bff4c76ac6da875a97a7afb5c6d193cd6c50b23e9e7e4ddd62b1ac690d3b94f6094ed5da1986cd2705b58a6147941788b13100ee77d2
6
+ metadata.gz: d97004ce75686f701d0a27bb8ac200e56956d5556fe4dbc874e523870d4c0467ee6eba5d5a1d7e34fc6d792d6df17ec85df689a297e94be56d44dadd33902c66
7
+ data.tar.gz: d719cc21f5288051ae6e7be83cb1f85e82000dc0e7345160d0087f3d61d9aab9a17ce45978437ceefa6d47ce063034022170e2062fd5404ee4182f245edf0792
data/.rubocop.yml CHANGED
@@ -27,5 +27,9 @@ RSpec/MultipleExpectations:
27
27
  RSpec/NamedSubject:
28
28
  Enabled: false
29
29
 
30
+ Security/Eval:
31
+ Exclude:
32
+ - 'spec/**/*.rb'
33
+
30
34
  Style/Documentation:
31
35
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.0 - 2022-01-03
6
+
7
+ ### Added
8
+
9
+ - Support annotate_rendered_view_with_filenames.
10
+
11
+ ### Changed
12
+
13
+ - Rename expression name from slim to slimi.
14
+
15
+ ### Fixed
16
+
17
+ - Fix bug at registering handler to ActionView.
18
+ - Fix Engine options at RailsTemplateHandler.
19
+ - Define missing :generator option at Engine.
20
+
5
21
  ## 0.5.1 - 2022-01-02
6
22
 
7
23
  ### Changed
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in slimi.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'actionview'
8
9
  gem 'rake', '~> 13.0'
9
10
  gem 'rspec'
10
11
  gem 'rubocop'
data/Gemfile.lock CHANGED
@@ -1,18 +1,47 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimi (0.5.1)
4
+ slimi (0.6.0)
5
5
  temple
6
6
  tilt
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ actionview (7.0.0)
12
+ activesupport (= 7.0.0)
13
+ builder (~> 3.1)
14
+ erubi (~> 1.4)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
17
+ activesupport (7.0.0)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 1.6, < 2)
20
+ minitest (>= 5.1)
21
+ tzinfo (~> 2.0)
11
22
  ast (2.4.2)
23
+ builder (3.2.4)
24
+ concurrent-ruby (1.1.9)
25
+ crass (1.0.6)
12
26
  diff-lcs (1.4.4)
27
+ erubi (1.10.0)
28
+ i18n (1.8.11)
29
+ concurrent-ruby (~> 1.0)
30
+ loofah (2.13.0)
31
+ crass (~> 1.0.2)
32
+ nokogiri (>= 1.5.9)
33
+ minitest (5.15.0)
34
+ nokogiri (1.12.5-x86_64-linux)
35
+ racc (~> 1.4)
13
36
  parallel (1.21.0)
14
37
  parser (3.0.3.2)
15
38
  ast (~> 2.4.1)
39
+ racc (1.6.0)
40
+ rails-dom-testing (2.0.3)
41
+ activesupport (>= 4.2.0)
42
+ nokogiri (>= 1.6)
43
+ rails-html-sanitizer (1.4.2)
44
+ loofah (~> 2.3)
16
45
  rainbow (3.0.0)
17
46
  rake (13.0.6)
18
47
  regexp_parser (2.2.0)
@@ -46,12 +75,15 @@ GEM
46
75
  ruby-progressbar (1.11.0)
47
76
  temple (0.8.2)
48
77
  tilt (2.0.10)
78
+ tzinfo (2.0.4)
79
+ concurrent-ruby (~> 1.0)
49
80
  unicode-display_width (2.1.0)
50
81
 
51
82
  PLATFORMS
52
83
  x86_64-linux
53
84
 
54
85
  DEPENDENCIES
86
+ actionview
55
87
  rake (~> 13.0)
56
88
  rspec
57
89
  rubocop
data/README.md CHANGED
@@ -57,3 +57,4 @@ gem 'slimi'
57
57
  - [ ] CLI tools
58
58
  - Slimi-only features
59
59
  - [x] Embedded Ruby code location
60
+ - [x] annotate_rendered_view_with_filenames
data/lib/slimi/engine.rb CHANGED
@@ -9,6 +9,7 @@ module Slimi
9
9
  attr_quote: '"',
10
10
  default_tag: 'div',
11
11
  format: :xhtml,
12
+ generator: ::Temple::Generators::StringBuffer,
12
13
  merge_attrs: { 'class' => ' ' },
13
14
  pretty: false,
14
15
  sort_attrs: true
@@ -28,6 +29,7 @@ module Slimi
28
29
  use Filters::Attribute
29
30
  use(:AttributeRemover) { ::Temple::HTML::AttributeRemover.new(remove_empty_attrs: options[:merge_attrs].keys) }
30
31
  html :Pretty
32
+ use Filters::Amble
31
33
  filter :Escapable
32
34
  filter :ControlFlow
33
35
  filter :MultiFlattener
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Slimi
4
+ module Filters
5
+ # Support Rails annotate_rendered_view_with_filenames feature.
6
+ class Amble < Base
7
+ define_options(
8
+ :postamble,
9
+ :preamble
10
+ )
11
+
12
+ # @param [Array] expression
13
+ # @return [Array]
14
+ def call(expression)
15
+ result = %i[multi]
16
+ result << [:static, options[:preamble]] if options[:preamble]
17
+ result << expression
18
+ result << [:static, options[:postamble]] if options[:postamble]
19
+ result
20
+ end
21
+ end
22
+ end
23
+ end
@@ -3,7 +3,7 @@
3
3
  module Slimi
4
4
  module Filters
5
5
  # Handle `[:slimi, :attributes, ...]`.
6
- class Attribute < ::Temple::HTML::Filter
6
+ class Attribute < Base
7
7
  define_options :merge_attrs
8
8
 
9
9
  # @param [Array<Array>] expressions
@@ -16,7 +16,7 @@ module Slimi
16
16
  # @param [Array] value
17
17
  # @return [Array]
18
18
  def on_html_attr(name, value)
19
- if value[0] == :slim && value[1] == :attrvalue && !options[:merge_attrs][name]
19
+ if value[0] == :slimi && value[1] == :attrvalue && !options[:merge_attrs][name]
20
20
  escape = value[2]
21
21
  code = value[3]
22
22
  case code
@@ -42,7 +42,7 @@ module Slimi
42
42
  # @param [Boolean] escape
43
43
  # @param [String] code\
44
44
  # @return [Array]\
45
- def on_slim_attrvalue(escape, code)
45
+ def on_slimi_attrvalue(escape, code)
46
46
  if (delimiter = options[:merge_attrs][@attr])
47
47
  tmp = unique_name
48
48
  [:multi,
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'temple'
4
+
5
+ module Slimi
6
+ module Filters
7
+ # Pass-through some expressions which are unknown for Temple.
8
+ class Base < ::Temple::HTML::Filter
9
+ # @param [String] code
10
+ # @param [Array] expression
11
+ # @return [Array]
12
+ def on_slimi_control(code, expression)
13
+ [:slimi, :control, code, compile(expression)]
14
+ end
15
+
16
+ # @param [String] type
17
+ # @param [String] code
18
+ # @param [Array] expression
19
+ # @param [Array] attributes
20
+ # @return [Array]
21
+ def on_slimi_embedded(type, expression, attributes)
22
+ [:slimi, :embedded, type, compile(expression), attributes]
23
+ end
24
+
25
+ # @param [Boolean] escape
26
+ # @param [String] code
27
+ # @param [Array] expression
28
+ # @return [Array]
29
+ def on_slimi_output(escape, code, expression)
30
+ [:slimi, :output, escape, code, compile(expression)]
31
+ end
32
+
33
+ # @param [String] type
34
+ # @param [Array] expression
35
+ # @return [Array]
36
+ def on_slimi_text(type, expression)
37
+ [:slimi, :text, type, compile(expression)]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,12 +2,12 @@
2
2
 
3
3
  module Slimi
4
4
  module Filters
5
- # Handle `[:slim, :control, code, multi]`.
6
- class Control < ::Temple::HTML::Filter
5
+ # Handle `[:slimi, :control, code, multi]`.
6
+ class Control < Base
7
7
  # @param [String] code
8
8
  # @param [Array] multi
9
9
  # @return [Array]
10
- def on_slim_control(code, multi)
10
+ def on_slimi_control(code, multi)
11
11
  [
12
12
  :multi,
13
13
  [:code, code],
@@ -3,24 +3,24 @@
3
3
  module Slimi
4
4
  module Filters
5
5
  # Append missing `do` to embedded Ruby code.
6
- class DoInserter < ::Temple::HTML::Filter
6
+ class DoInserter < Base
7
7
  VALID_RUBY_LINE_REGEXP = /(\A(if|unless|else|elsif|when|begin|rescue|ensure|case)\b)|\bdo\s*(\|[^|]*\|\s*)?\Z/.freeze
8
8
 
9
9
  # @param [String] code
10
10
  # @param [Array] expressio
11
11
  # @return [Array]
12
- def on_slim_control(code, expression)
12
+ def on_slimi_control(code, expression)
13
13
  code += ' do' unless code.match?(VALID_RUBY_LINE_REGEXP) || empty_exp?(expression)
14
- [:slim, :control, code, compile(expression)]
14
+ [:slimi, :control, code, compile(expression)]
15
15
  end
16
16
 
17
17
  # @param [Boolean] escape
18
18
  # @param [String] code
19
19
  # @param [Array] expression
20
20
  # @return [Array]
21
- def on_slim_output(escape, code, expression)
21
+ def on_slimi_output(escape, code, expression)
22
22
  code += ' do' unless code.match?(VALID_RUBY_LINE_REGEXP) || empty_exp?(expression)
23
- [:slim, :output, escape, code, compile(expression)]
23
+ [:slimi, :output, escape, code, compile(expression)]
24
24
  end
25
25
  end
26
26
  end
@@ -3,21 +3,21 @@
3
3
  module Slimi
4
4
  module Filters
5
5
  # @api private
6
- class TextCollector < ::Temple::HTML::Filter
6
+ class TextCollector < Base
7
7
  def call(exp)
8
8
  @collected = ''
9
9
  super(exp)
10
10
  @collected
11
11
  end
12
12
 
13
- def on_slim_interpolate(text)
13
+ def on_slimi_interpolate(text)
14
14
  @collected << text
15
15
  nil
16
16
  end
17
17
  end
18
18
 
19
19
  # @api private
20
- class NewlineCollector < ::Temple::HTML::Filter
20
+ class NewlineCollector < Base
21
21
  def call(exp)
22
22
  @collected = [:multi]
23
23
  super(exp)
@@ -31,7 +31,7 @@ module Slimi
31
31
  end
32
32
 
33
33
  # @api private
34
- class OutputProtector < ::Temple::HTML::Filter
34
+ class OutputProtector < Base
35
35
  def call(exp)
36
36
  @protect = []
37
37
  @collected = ''
@@ -45,9 +45,9 @@ module Slimi
45
45
  nil
46
46
  end
47
47
 
48
- def on_slim_output(escape, text, content)
48
+ def on_slimi_output(escape, text, content)
49
49
  @collected << @tag
50
- @protect << [:slim, :output, escape, text, content]
50
+ @protect << [:slimi, :output, escape, text, content]
51
51
  nil
52
52
  end
53
53
 
@@ -64,7 +64,7 @@ module Slimi
64
64
 
65
65
  # Temple filter which processes embedded engines
66
66
  # @api private
67
- class Embedded < ::Temple::HTML::Filter
67
+ class Embedded < Base
68
68
  @engines = {}
69
69
 
70
70
  class << self
@@ -101,12 +101,12 @@ module Slimi
101
101
  @disabled = normalize_engine_list(options[:disable_engines])
102
102
  end
103
103
 
104
- def on_slim_embedded(name, body, attrs)
104
+ def on_slimi_embedded(name, body, attrs)
105
105
  name = name.to_sym
106
106
  raise(Temple::FilterError, "Embedded engine #{name} is disabled") unless enabled?(name)
107
107
 
108
108
  @engines[name] ||= self.class.create(name, options)
109
- @engines[name].on_slim_embedded(name, body, attrs)
109
+ @engines[name].on_slimi_embedded(name, body, attrs)
110
110
  end
111
111
 
112
112
  def enabled?(name)
@@ -122,7 +122,7 @@ module Slimi
122
122
  list&.map(&:to_sym)
123
123
  end
124
124
 
125
- class Engine < ::Temple::HTML::Filter
125
+ class Engine < Base
126
126
  protected
127
127
 
128
128
  def collect_text(body)
@@ -138,7 +138,7 @@ module Slimi
138
138
 
139
139
  # Basic tilt engine
140
140
  class TiltEngine < Engine
141
- def on_slim_embedded(engine, body, _attrs)
141
+ def on_slimi_embedded(engine, body, _attrs)
142
142
  tilt_engine = Tilt[engine] || raise(Temple::FilterError, "Tilt engine #{engine} is not available.")
143
143
  tilt_options = options[engine.to_sym] || {}
144
144
  tilt_options[:default_encoding] ||= 'utf-8'
@@ -194,7 +194,7 @@ module Slimi
194
194
  class TagEngine < Engine
195
195
  disable_option_validator!
196
196
 
197
- def on_slim_embedded(engine, body, attrs)
197
+ def on_slimi_embedded(engine, body, attrs)
198
198
  unless options[:attributes].empty?
199
199
  options[:attributes].map do |k, v|
200
200
  attrs << [:html, :attr, k, [:static, v]]
@@ -207,7 +207,7 @@ module Slimi
207
207
  opts.delete(:tag)
208
208
  opts.delete(:attributes)
209
209
  @engine ||= options[:engine].new(opts)
210
- body = @engine.on_slim_embedded(engine, body, attrs)
210
+ body = @engine.on_slimi_embedded(engine, body, attrs)
211
211
  end
212
212
 
213
213
  [:html, :tag, options[:tag], attrs, body]
@@ -221,14 +221,14 @@ module Slimi
221
221
 
222
222
  set_options tag: :script, attributes: {}
223
223
 
224
- def on_slim_embedded(engine, body, attrs)
224
+ def on_slimi_embedded(engine, body, attrs)
225
225
  super(engine, [:html, :js, body], attrs)
226
226
  end
227
227
  end
228
228
 
229
229
  # Embeds ruby code
230
230
  class RubyEngine < Engine
231
- def on_slim_embedded(_engine, body, _attrs)
231
+ def on_slimi_embedded(_engine, body, _attrs)
232
232
  [:multi, [:newline], [:code, "#{collect_text(body)}\n"]]
233
233
  end
234
234
  end
@@ -3,7 +3,7 @@
3
3
  module Slimi
4
4
  module Filters
5
5
  # Append missing `end` line to embedded Ruby code in control block.
6
- class EndInserter < ::Temple::HTML::Filter
6
+ class EndInserter < Base
7
7
  # @param [Array<Array>] expressions
8
8
  def on_multi(*expressions)
9
9
  result = [:multi]
@@ -49,7 +49,7 @@ module Slimi
49
49
 
50
50
  # @return [Boolean]
51
51
  def control?
52
- @expression[0] == :slim && @expression[1] == :control
52
+ @expression[0] == :slimi && @expression[1] == :control
53
53
  end
54
54
 
55
55
  # @return [Boolean]
@@ -4,29 +4,7 @@ require 'strscan'
4
4
 
5
5
  module Slimi
6
6
  module Filters
7
- class Interpolation
8
- def initialize(*); end
9
-
10
- def call(node)
11
- convert(node)
12
- end
13
-
14
- private
15
-
16
- def convert(value)
17
- if value.instance_of?(::Array)
18
- if value[0] == :slimi && value[1] == :interpolate
19
- on_slimi_interpolate(value[2], value[3], value[4])
20
- else
21
- value.map do |element|
22
- call(element)
23
- end
24
- end
25
- else
26
- value
27
- end
28
- end
29
-
7
+ class Interpolation < Base
30
8
  # @param [Integer] begin_
31
9
  # @param [Integer] end_
32
10
  # @return [Array] S-expression.
@@ -47,7 +25,7 @@ module Slimi
47
25
  else
48
26
  escape = false
49
27
  end
50
- block << [:slimi, :position, begin2, begin2 + code.length, [:slim, :output, escape, code, [:multi]]]
28
+ block << [:slimi, :position, begin2, begin2 + code.length, [:slimi, :output, escape, code, [:multi]]]
51
29
  elsif (value = scanner.scan(/([#\\]?[^#\\]*([#\\][^\\\#{][^#\\]*)*)/)) # rubocop:disable Lint/DuplicateBranch
52
30
  block << [:static, value]
53
31
  end
@@ -2,8 +2,8 @@
2
2
 
3
3
  module Slimi
4
4
  module Filters
5
- # Handle `[:slim, :output, escape, code, multi]`.
6
- class Output < ::Temple::HTML::Filter
5
+ # Handle `[:slimi, :output, escape, code, multi]`.
6
+ class Output < Base
7
7
  define_options :disable_capture
8
8
 
9
9
  IF_REGEXP = /\A(if|unless)\b|\bdo\s*(\|[^|]*\|)?\s*$/.freeze
@@ -12,7 +12,7 @@ module Slimi
12
12
  # @param [String] code
13
13
  # @param [Array] multi
14
14
  # @return [Array]
15
- def on_slim_output(escape, code, multi)
15
+ def on_slimi_output(escape, code, multi)
16
16
  if code.match?(IF_REGEXP)
17
17
  tmp = unique_name
18
18
  [
@@ -2,12 +2,12 @@
2
2
 
3
3
  module Slimi
4
4
  module Filters
5
- # Handle `[:slim, :text, multi]`.
6
- class Text < ::Temple::HTML::Filter
5
+ # Handle `[:slimi, :text, multi]`.
6
+ class Text < Base
7
7
  # @param [Symbol] _type
8
8
  # @param [Array] multi
9
9
  # @return [Array]
10
- def on_slim_text(_type, multi)
10
+ def on_slimi_text(_type, multi)
11
11
  compile(multi)
12
12
  end
13
13
  end
@@ -2,29 +2,13 @@
2
2
 
3
3
  module Slimi
4
4
  module Filters
5
- class Unposition
6
- def initialize(*); end
7
-
8
- # @param [Array] node S-expression.
9
- # @return [Array] S-expression.
10
- def call(node)
11
- convert(node)
12
- end
13
-
14
- private
15
-
16
- def convert(value)
17
- if value.instance_of?(::Array)
18
- if value[0] == :slimi && value[1] == :position
19
- call(value[4])
20
- else
21
- value.map do |element|
22
- call(element)
23
- end
24
- end
25
- else
26
- value
27
- end
5
+ class Unposition < Base
6
+ # @param [Integer] _begin
7
+ # @param [Integer] _end
8
+ # @param [Array] expression
9
+ # @return [Array]
10
+ def on_slimi_position(_begin, _end, expression)
11
+ compile(expression)
28
12
  end
29
13
  end
30
14
  end
data/lib/slimi/filters.rb CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  module Slimi
4
4
  module Filters
5
+ autoload :Amble, 'slimi/filters/amble'
5
6
  autoload :Attribute, 'slimi/filters/attribute'
7
+ autoload :Base, 'slimi/filters/base'
6
8
  autoload :Control, 'slimi/filters/control'
7
9
  autoload :DoInserter, 'slimi/filters/do_inserter'
8
10
  autoload :Embedded, 'slimi/filters/embedded'
data/lib/slimi/parser.rb CHANGED
@@ -115,7 +115,7 @@ module Slimi
115
115
 
116
116
  embedded_template_engine_name = @scanner[1]
117
117
  attributes = parse_attributes
118
- @stacks.last << [:slim, :embedded, embedded_template_engine_name, parse_text_block, attributes]
118
+ @stacks.last << [:slimi, :embedded, embedded_template_engine_name, parse_text_block, attributes]
119
119
  end
120
120
 
121
121
  # @return [Boolean]
@@ -150,14 +150,14 @@ module Slimi
150
150
  block = [:multi]
151
151
  @stacks.last.insert(-2, [:static, ' ']) if with_leading_white_space2
152
152
  @scanner.skip(/[ \t]+/)
153
- tag << with_position { [:slim, :output, escape, parse_broken_lines, block] }
153
+ tag << with_position { [:slimi, :output, escape, parse_broken_lines, block] }
154
154
  @stacks.last << [:static, ' '] if with_trailing_white_space2
155
155
  @stacks << block
156
156
  elsif @scanner.skip(%r{[ \t]*/[ \t]*})
157
157
  syntax_error!(Errors::UnexpectedTextAfterClosedTagError) unless @scanner.match?(/\r?\n/)
158
158
  else
159
159
  @scanner.skip(/[ \t]+/)
160
- tag << [:slim, :text, :inline, parse_text_block]
160
+ tag << [:slimi, :text, :inline, parse_text_block]
161
161
  end
162
162
  true
163
163
  else
@@ -272,7 +272,7 @@ module Slimi
272
272
  charpos = @scanner.charpos
273
273
  attribute_value = parse_ruby_attribute_value(attribute_delimiter_closing)
274
274
  syntax_error!(Errors::InvalidEmptyAttributeError) if attribute_value.empty?
275
- attributes << [:html, :attr, attribute_name, [:slimi, :position, charpos, charpos + attribute_value.length, [:slim, :attrvalue, escape, attribute_value]]]
275
+ attributes << [:html, :attr, attribute_name, [:slimi, :position, charpos, charpos + attribute_value.length, [:slimi, :attrvalue, escape, attribute_value]]]
276
276
  elsif !attribute_delimiter_closing_part_regexp
277
277
  break
278
278
  elsif @scanner.skip(boolean_attribute_regexp)
@@ -334,7 +334,7 @@ module Slimi
334
334
  def parse_html_comment
335
335
  if @scanner.skip(%r{/!})
336
336
  text_block = parse_text_block
337
- text = [:slim, :text, :verbatim, text_block]
337
+ text = [:slimi, :text, :verbatim, text_block]
338
338
  @stacks.last << [:html, :comment, text]
339
339
  true
340
340
  else
@@ -381,7 +381,7 @@ module Slimi
381
381
  def parse_verbatim_text_block_inner
382
382
  if @scanner.skip(/([|']) ?/)
383
383
  with_trailing_white_space = @scanner[1] == "'"
384
- @stacks.last << [:slim, :text, :verbatim, parse_text_block]
384
+ @stacks.last << [:slimi, :text, :verbatim, parse_text_block]
385
385
  @stacks.last << [:static, ' '] if with_trailing_white_space
386
386
  true
387
387
  else
@@ -420,7 +420,7 @@ module Slimi
420
420
  if @scanner.skip(/-/)
421
421
  block = [:multi]
422
422
  @scanner.skip(/[ \t]+/)
423
- @stacks.last << with_position { [:slim, :control, parse_broken_lines, block] }
423
+ @stacks.last << with_position { [:slimi, :control, parse_broken_lines, block] }
424
424
  @stacks << block
425
425
  true
426
426
  else
@@ -443,7 +443,7 @@ module Slimi
443
443
  block = [:multi]
444
444
  @stacks.last << [:static, ' '] if with_trailing_white_space
445
445
  @scanner.skip(/[ \t]+/)
446
- @stacks.last << with_position { [:slim, :output, escape, parse_broken_lines, block] }
446
+ @stacks.last << with_position { [:slimi, :output, escape, parse_broken_lines, block] }
447
447
  @stacks.last << [:static, ' '] if with_leading_white_space
448
448
  @stacks << block
449
449
  else
@@ -1,14 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimi
4
+ # Render Slim template in response to requests from Rails.
4
5
  class RailsTemplateHandler
5
- def initialize
6
- @engine = Engine.new
6
+ # @param [ActionView::Template] template
7
+ # @param [String, nil] source
8
+ # @return [String]
9
+ def call(template, source = nil)
10
+ Renderer.new(
11
+ source: source,
12
+ template: template
13
+ ).call
7
14
  end
8
15
 
9
- def call(template, source = nil)
10
- source ||= template.source
11
- @engine.call(source)
16
+ # Render HTML from given source and options.
17
+ class Renderer
18
+ # @param [String] source
19
+ # @param [ActionView::Template] template
20
+ def initialize(
21
+ source:,
22
+ template:
23
+ )
24
+ @source = source
25
+ @template = template
26
+ end
27
+
28
+ # @return [String]
29
+ def call
30
+ engine.call(source)
31
+ end
32
+
33
+ private
34
+
35
+ # @return [Slimi::Engine]
36
+ def engine
37
+ Engine.new(engine_options)
38
+ end
39
+
40
+ # @return [Hash{Symbol => Object}]
41
+ def engine_options
42
+ engine_default_options.merge(engine_amble_options)
43
+ end
44
+
45
+ # @return [Hash{Symbol => Object}]
46
+ def engine_default_options
47
+ {
48
+ generator: ::Temple::Generators::RailsOutputBuffer,
49
+ streaming: true,
50
+ use_html_safe: true
51
+ }
52
+ end
53
+
54
+ # @return [Hash{Symbol => Object}]
55
+ def engine_amble_options
56
+ if with_annotate_rendered_view_with_filenames?
57
+ {
58
+ postamble: "<!-- END #{@template.short_identifier} -->\n",
59
+ preamble: "<!-- BEGIN #{@template.short_identifier} -->\n"
60
+ }
61
+ else
62
+ {}
63
+ end
64
+ end
65
+
66
+ # @return [String]
67
+ def source
68
+ @source || @template.source
69
+ end
70
+
71
+ # @return [Boolean]
72
+ def with_annotate_rendered_view_with_filenames?
73
+ ::ActionView::Base.try(:annotate_rendered_view_with_filenames) && @template.format == :html
74
+ end
12
75
  end
13
76
  end
14
77
  end
data/lib/slimi/railtie.rb CHANGED
@@ -5,6 +5,7 @@ module Slimi
5
5
  initializer 'Register Slimi template handler' do
6
6
  ::ActiveSupport.on_load(:action_view) do
7
7
  ::ActionView::Template.register_template_handler(
8
+ :slim,
8
9
  RailsTemplateHandler.new
9
10
  )
10
11
  end
data/lib/slimi/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Slimi
4
- VERSION = '0.5.1'
4
+ VERSION = '0.6.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slimi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
@@ -60,7 +60,9 @@ files:
60
60
  - lib/slimi/engine.rb
61
61
  - lib/slimi/errors.rb
62
62
  - lib/slimi/filters.rb
63
+ - lib/slimi/filters/amble.rb
63
64
  - lib/slimi/filters/attribute.rb
65
+ - lib/slimi/filters/base.rb
64
66
  - lib/slimi/filters/control.rb
65
67
  - lib/slimi/filters/do_inserter.rb
66
68
  - lib/slimi/filters/embedded.rb