hamlit 1.6.7 → 1.7.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
  SHA1:
3
- metadata.gz: ed569855be3cfd062957586173c438777bbad1bf
4
- data.tar.gz: cb9f5d24e5290a801e38a9c1558e01e18dfd2c8b
3
+ metadata.gz: ca12e0120f1176a67b7f89077b8f00b832cafe16
4
+ data.tar.gz: fe98fcc635527e47b22441eadb3409bbd4c5864e
5
5
  SHA512:
6
- metadata.gz: c599f0dcbac927f422137f528949f4ee6111a3f8104caf062e7abe1e23dc861f8ba4f3aae4119bf4dc72b2b5d86265250e7d7dd1d8a967d1d0d887fa2f04c98c
7
- data.tar.gz: 8f53b9e6279a35eb5430846173017b09a3b88350b8c29c8f113c2bef31735524760ea9b19e8a12d185f7b35df5fd8e4a3b5938af6f8a705ca71d9259c3be260e
6
+ metadata.gz: f233e410357f3812775992bcfc6da6bfc27f6ed2b4153f60f050c34df4221084711da794ca22da329ae4665d43a2c9d1f700528038381bbd5ab56d9959b5658f
7
+ data.tar.gz: 200d020c6ed62d628aeeb03634b71f842f9d5ff245a4a6205b820b5293124fd622a766e843371fd7755096c185d8962c226463ffa796525cc354405aa18684d0
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v1.7.0
2
+
3
+ - Support Ruby 2.2.0 hash syntax
4
+ - like `{ "hyphened-key": "value" }`
5
+
1
6
  ## v1.6.7
2
7
 
3
8
  - Remove unused variables and avoid shadowing
@@ -12,22 +12,15 @@ require 'slim'
12
12
  require 'hamlit'
13
13
 
14
14
  class Benchmarks
15
- def initialize(time)
15
+ def initialize(time, escape_enabled)
16
16
  @time = time
17
17
  @benches = []
18
18
  @versions = {}
19
- end
20
19
 
21
- def init_compiled_benches(escape_enabled)
20
+ init_compiled_benches(escape_enabled)
22
21
  if escape_enabled
23
- @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
24
- @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
25
- @slim_code = File.read(File.dirname(__FILE__) + '/view.escaped.slim')
26
22
  init_escaped_benches
27
23
  else
28
- @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
29
- @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
30
- @slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
31
24
  init_plain_benches
32
25
  end
33
26
  end
@@ -45,11 +38,26 @@ class Benchmarks
45
38
 
46
39
  private
47
40
 
41
+ def init_compiled_benches(escape_enabled)
42
+ slim_path = "/view#{'.escaped' if escape_enabled}.slim"
43
+ @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
44
+ @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
45
+ @slim_code = File.read(File.dirname(__FILE__) + slim_path)
46
+ end
47
+
48
48
  # Totally the same as slim-template/slim's compiled bench.
49
49
  def init_plain_benches
50
50
  puts 'Compiled rendering benchmarks without HTML escape'
51
51
  context = Context.new
52
+ define_plain_methods!(context)
53
+
54
+ bench('hamlit', Hamlit::VERSION) { context.run_hamlit }
55
+ bench('erubis', Erubis::VERSION) { context.run_erubis }
56
+ bench('slim', Slim::VERSION) { context.run_slim_ugly }
57
+ bench('haml', Haml::VERSION) { context.run_haml_ugly }
58
+ end
52
59
 
60
+ def define_plain_methods!(context)
53
61
  haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true)
54
62
  haml_ugly.def_method(context, :run_haml_ugly)
55
63
  context.instance_eval %{
@@ -57,19 +65,23 @@ class Benchmarks
57
65
  def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
58
66
  def run_hamlit; #{Hamlit::Engine.new(escape_html: false).call @haml_code}; end
59
67
  }
60
-
61
- bench('hamlit', Hamlit::VERSION) { context.run_hamlit }
62
- bench('erubis', Erubis::VERSION) { context.run_erubis }
63
- bench('slim', Slim::VERSION) { context.run_slim_ugly }
64
- bench('haml', Haml::VERSION) { context.run_haml_ugly }
65
68
  end
66
69
 
67
70
  # slim-template/slim's compiled bench with HTML escaping.
68
71
  def init_escaped_benches
69
72
  puts 'Compiled rendering benchmarks with HTML escape'
70
73
  context = Context.new
71
- context.instance_eval("def header; '<script>'; end")
74
+ define_escaped_methods!(context)
75
+
76
+ bench('hamlit', Hamlit::VERSION) { context.run_hamlit }
77
+ bench('slim', Slim::VERSION) { context.run_slim_ugly }
78
+ bench('faml', Faml::VERSION) { context.run_faml }
79
+ bench('erubis', Erubis::VERSION) { context.run_erubis }
80
+ bench('haml', Haml::VERSION) { context.run_haml_ugly }
81
+ end
72
82
 
83
+ def define_escaped_methods!(context)
84
+ context.instance_eval("def header; '<script>'; end")
73
85
  haml_ugly = Haml::Engine.new(@haml_code, format: :html5, ugly: true, escape_html: true)
74
86
  haml_ugly.def_method(context, :run_haml_ugly)
75
87
  context.instance_eval %{
@@ -78,12 +90,6 @@ class Benchmarks
78
90
  def run_faml; #{Faml::Engine.new.call @haml_code}; end
79
91
  def run_hamlit; #{Hamlit::Engine.new.call @haml_code}; end
80
92
  }
81
-
82
- bench('hamlit', Hamlit::VERSION) { context.run_hamlit }
83
- bench('slim', Slim::VERSION) { context.run_slim_ugly }
84
- bench('faml', Faml::VERSION) { context.run_faml }
85
- bench('erubis', Erubis::VERSION) { context.run_erubis }
86
- bench('haml', Haml::VERSION) { context.run_haml_ugly }
87
93
  end
88
94
 
89
95
  def bench(name, version, &block)
@@ -100,6 +106,5 @@ class Benchmarks
100
106
  end
101
107
 
102
108
  time = (ENV['TIME'] || 5).to_i
103
- bench = Benchmarks.new(time)
104
- bench.init_compiled_benches(ENV['HTML_ESCAPE'])
109
+ bench = Benchmarks.new(time, ENV['HTML_ESCAPE'] == '1')
105
110
  bench.run
@@ -57,18 +57,22 @@ module Hamlit
57
57
  target = flatten_attributes(target)
58
58
 
59
59
  (base.keys | target.keys).each do |key|
60
- if base[key] && target[key]
61
- case key
62
- when :id
63
- result[key] = [base[key], target[key]].compact.join('_')
64
- when :class
65
- result[key] = [base[key], target[key]].flatten.compact.map(&:to_s).sort.uniq.join(' ')
66
- end
67
- else
68
- result[key] = base[key].nil? ? target[key] : base[key]
69
- end
60
+ result[key] = merge_attribute_value(base, target, key)
70
61
  end
71
62
  result
72
63
  end
64
+
65
+ def merge_attribute_value(base, target, key)
66
+ return target[key] unless base[key]
67
+ return base[key] unless target[key]
68
+
69
+ values = [base[key], target[key]].flatten.compact
70
+ case key
71
+ when :id
72
+ values.join('_')
73
+ when :class
74
+ values.map(&:to_s).sort.uniq.join(' ')
75
+ end
76
+ end
73
77
  end
74
78
  end
@@ -31,6 +31,27 @@ module Hamlit
31
31
  itemscope allowfullscreen default inert sortable
32
32
  truespeed typemustmatch data].freeze
33
33
 
34
+ DEFAULT_COUNTS = {
35
+ brace: -1,
36
+ emb: 0,
37
+ paren: 0,
38
+ bracket: 0,
39
+ }.freeze
40
+
41
+ OPEN_TOKEN_TYPES = {
42
+ on_lbrace: :brace,
43
+ on_embexpr_beg: :emb,
44
+ on_lparen: :paren,
45
+ on_lbracket: :bracket,
46
+ }.freeze
47
+
48
+ CLOSE_TOKEN_TYPES = {
49
+ on_rbrace: :brace,
50
+ on_embexpr_end: :emb,
51
+ on_rparen: :paren,
52
+ on_rbracket: :bracket,
53
+ }.freeze
54
+
34
55
  def compile_old_attribute(str)
35
56
  raise RuntimeBuild unless valid_hash?(str)
36
57
 
@@ -99,16 +120,9 @@ module Hamlit
99
120
  attributes = {}
100
121
 
101
122
  split_hash(str).each do |attr|
102
- tokens = Ripper.lex("{#{attr}")
103
- tokens = tokens.drop(1)
104
-
123
+ tokens = Ripper.lex("{#{attr}").drop(1)
105
124
  key = read_hash_key!(tokens)
106
- val = tokens.map(&:last).join.strip
107
-
108
- skip_tokens!(tokens, :on_sp)
109
- if type_of(tokens.first) == :on_lbrace
110
- val = parse_old_attributes(val)
111
- end
125
+ val = read_hash_value!(tokens)
112
126
 
113
127
  attributes[key] = val if key
114
128
  end
@@ -116,6 +130,16 @@ module Hamlit
116
130
  attributes
117
131
  end
118
132
 
133
+ def read_hash_value!(tokens)
134
+ skip_tokens!(tokens, :on_sp)
135
+ val = tokens.map(&:last).join.strip
136
+
137
+ if type_of(tokens.first) == :on_lbrace
138
+ val = parse_old_attributes(val)
139
+ end
140
+ val
141
+ end
142
+
119
143
  def read_hash_key!(tokens)
120
144
  skip_tokens!(tokens, :on_sp)
121
145
 
@@ -131,17 +155,21 @@ module Hamlit
131
155
  end
132
156
  assert_rocket!(tokens)
133
157
  when :on_tstring_beg
134
- str = read_string!(tokens)
135
- assert_rocket!(tokens)
158
+ str = read_string!(tokens, assert_rocket: true)
136
159
  end
137
160
  str
138
161
  end
139
162
 
140
- def read_string!(tokens)
163
+ def read_string!(tokens, assert_rocket: false)
141
164
  _, type, str = tokens.shift
142
165
  return '' if type == :on_tstring_end
143
166
 
144
- raise SyntaxError if type_of(tokens.shift) != :on_tstring_end
167
+ next_token = tokens.shift
168
+ return str if RUBY_VERSION >= "2.2.0" && type_of(next_token) == :on_label_end
169
+
170
+ raise SyntaxError if type_of(next_token) != :on_tstring_end
171
+ assert_rocket!(tokens) if assert_rocket
172
+
145
173
  str
146
174
  end
147
175
 
@@ -168,40 +196,26 @@ module Hamlit
168
196
 
169
197
  def reject_nested_columns(str, columns)
170
198
  result = []
171
- open_count = 0
172
- count = {
173
- emb: 0,
174
- paren: 0,
175
- bracket: 0,
176
- }
199
+ counts = DEFAULT_COUNTS.dup
177
200
 
178
201
  Ripper.lex(str).each do |(_, col), type, _|
179
- if columns.include?(col) && open_count == 1 && count.values.all?(&:zero?)
202
+ if columns.include?(col) && counts.values.all?(&:zero?)
180
203
  result << col
181
204
  end
182
-
183
- case type
184
- when :on_lbrace
185
- open_count += 1
186
- when :on_rbrace
187
- open_count -= 1
188
- when :on_embexpr_beg
189
- count[:emb] += 1
190
- when :on_embexpr_end
191
- count[:emb] -= 1
192
- when :on_lparen
193
- count[:paren] += 1
194
- when :on_rparen
195
- count[:paren] -= 1
196
- when :on_lbracket
197
- count[:bracket] += 1
198
- when :on_rbracket
199
- count[:bracket] -= 1
200
- end
205
+ update_open_counts(counts, type)
201
206
  end
202
207
  result
203
208
  end
204
209
 
210
+ def update_open_counts(counts, type)
211
+ case
212
+ when OPEN_TOKEN_TYPES[type]
213
+ counts[OPEN_TOKEN_TYPES[type]] += 1
214
+ when CLOSE_TOKEN_TYPES[type]
215
+ counts[CLOSE_TOKEN_TYPES[type]] -= 1
216
+ end
217
+ end
218
+
205
219
  class HashParser < Ripper
206
220
  attr_reader :columns
207
221
 
@@ -2,7 +2,13 @@ module Hamlit
2
2
  module Concerns
3
3
  module StringInterpolation
4
4
  def string_literal(str)
5
- unescape_interpolation(str)
5
+ res = ''
6
+ rest = handle_interpolation(str.inspect) do |scan|
7
+ escapes = (scan[2].size - 1) / 2
8
+ res << scan.matched[0...-3 - escapes]
9
+ res << (escapes.odd? ? '#{' : unescape_interpolation(scan))
10
+ end
11
+ res + rest
6
12
  end
7
13
 
8
14
  def contains_interpolation?(str)
@@ -11,19 +17,9 @@ module Hamlit
11
17
 
12
18
  private
13
19
 
14
- def unescape_interpolation(str)
15
- res = ''
16
- rest = handle_interpolation(str.inspect) do |scan|
17
- escapes = (scan[2].size - 1) / 2
18
- res << scan.matched[0...-3 - escapes]
19
- if escapes % 2 == 1
20
- res << '#{'
21
- else
22
- content = eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
23
- res << '#{' + content + '}'
24
- end
25
- end
26
- res + rest
20
+ def unescape_interpolation(scan)
21
+ content = eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
22
+ '#{' + content + '}'
27
23
  end
28
24
 
29
25
  def handle_interpolation(str)
@@ -34,15 +30,19 @@ module Hamlit
34
30
 
35
31
  def balance(scanner, start, finish, count = 0)
36
32
  str = ''
37
- scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
38
- regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
39
- while scanner.scan(regexp)
33
+ while balanced_scan(scanner, start, finish)
40
34
  str << scanner.matched
41
35
  count += 1 if scanner.matched[-1] == start
42
36
  count -= 1 if scanner.matched[-1] == finish
43
37
  return [str.strip, scanner.rest] if count == 0
44
38
  end
45
39
  end
40
+
41
+ def balanced_scan(scanner, start, finish)
42
+ regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
43
+ scanner = StringScanner.new(scanner) unless scanner.is_a?(StringScanner)
44
+ scanner.scan(regexp)
45
+ end
46
46
  end
47
47
  end
48
48
  end
@@ -39,6 +39,31 @@ module Hamlit
39
39
  end
40
40
  lines
41
41
  end
42
+
43
+ def compile_html(tag, lines)
44
+ ast = [:haml, :text, compile_lines(lines, indent_width: 2), false]
45
+ ast = [:multi, [:static, "\n"], ast]
46
+ ast = [:html, :tag, tag, [:html, :attrs], ast]
47
+ ast
48
+ end
49
+
50
+ def compile_xhtml(tag, type, lines)
51
+ attr = [:html, :attr, 'type', [:static, type]]
52
+ attrs = [:html, :attrs, attr]
53
+
54
+ content = [:haml, :text, compile_lines(lines, indent_width: 4)]
55
+ multi = [:multi, [:static, "\n"], *cdata_for(type, content)]
56
+ [:html, :tag, tag, attrs, multi]
57
+ end
58
+
59
+ def cdata_for(type, ast)
60
+ case type
61
+ when 'text/javascript'
62
+ [[:static, " //<![CDATA[\n"], ast, [:static, " //]]>\n"]]
63
+ when 'text/css'
64
+ [[:static, " /*<![CDATA[*/\n"], ast, [:static, " /*]]>*/\n"]]
65
+ end
66
+ end
42
67
  end
43
68
  end
44
69
  end
@@ -4,30 +4,11 @@ module Hamlit
4
4
  module Filters
5
5
  class Css < Base
6
6
  def compile(lines)
7
- return compile_xhtml(lines) if options[:format] == :xhtml
7
+ if options[:format] == :xhtml
8
+ return compile_xhtml('style', 'text/css', lines)
9
+ end
8
10
 
9
- compile_html(lines)
10
- end
11
-
12
- private
13
-
14
- def compile_html(lines)
15
- ast = [:haml, :text, compile_lines(lines, indent_width: 2), false]
16
- ast = [:multi, [:static, "\n"], ast]
17
- ast = [:html, :tag, 'style', [:html, :attrs], ast]
18
- ast
19
- end
20
-
21
- def compile_xhtml(lines)
22
- attr = [:html, :attr, 'type', [:static, 'text/css']]
23
- attrs = [:html, :attrs, attr]
24
-
25
- multi = [:multi, [:static, "\n"]]
26
- multi << [:static, " /*<![CDATA[*/\n"]
27
- multi << [:haml, :text, compile_lines(lines, indent_width: 4)]
28
- multi << [:static, " /*]]>*/\n"]
29
-
30
- [:html, :tag, 'style', attrs, multi]
11
+ compile_html('style', lines)
31
12
  end
32
13
  end
33
14
  end
@@ -4,30 +4,11 @@ module Hamlit
4
4
  module Filters
5
5
  class Javascript < Base
6
6
  def compile(lines)
7
- return compile_xhtml(lines) if options[:format] == :xhtml
7
+ if options[:format] == :xhtml
8
+ return compile_xhtml('script', 'text/javascript', lines)
9
+ end
8
10
 
9
- compile_html(lines)
10
- end
11
-
12
- private
13
-
14
- def compile_html(lines)
15
- ast = [:haml, :text, compile_lines(lines, indent_width: 2), false]
16
- ast = [:multi, [:static, "\n"], ast]
17
- ast = [:html, :tag, 'script', [:html, :attrs], ast]
18
- ast
19
- end
20
-
21
- def compile_xhtml(lines)
22
- attr = [:html, :attr, 'type', [:static, 'text/javascript']]
23
- attrs = [:html, :attrs, attr]
24
-
25
- multi = [:multi, [:static, "\n"]]
26
- multi << [:static, " //<![CDATA[\n"]
27
- multi << [:haml, :text, compile_lines(lines, indent_width: 4)]
28
- multi << [:static, " //]]>\n"]
29
-
30
- [:html, :tag, 'script', attrs, multi]
11
+ compile_html('script', lines)
31
12
  end
32
13
  end
33
14
  end
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "1.6.7"
2
+ VERSION = "1.7.0"
3
3
  end
@@ -371,5 +371,26 @@ describe Hamlit::Engine do
371
371
  HTML
372
372
  end
373
373
  end
374
+
375
+ if RUBY_VERSION >= "2.2.0"
376
+ describe 'Ruby 2.2 syntax' do
377
+ it 'renders static attributes' do
378
+ assert_render(<<-HAML, <<-HTML)
379
+ %meta{ content: 'IE=edge', 'http-equiv': 'X-UA-Compatible' }
380
+ HAML
381
+ <meta content='IE=edge' http-equiv='X-UA-Compatible'>
382
+ HTML
383
+ end
384
+
385
+ it 'renders dynamic attributes' do
386
+ assert_render(<<-HAML, <<-HTML)
387
+ - hash = { content: 'IE=edge' }
388
+ %meta{ hash, 'http-equiv': 'X-UA-Compatible' }
389
+ HAML
390
+ <meta content='IE=edge' http-equiv='X-UA-Compatible'>
391
+ HTML
392
+ end
393
+ end
394
+ end
374
395
  end
375
396
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamlit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.7
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-27 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils
@@ -626,4 +626,3 @@ test_files:
626
626
  - spec/spec_helper/document_generator.rb
627
627
  - spec/spec_helper/render_helper.rb
628
628
  - spec/spec_helper/test_case.rb
629
- has_rdoc: