hamlit 1.4.3 → 1.4.5

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: 930fc5e028d874b28cbf4c78e7d9434d3f41d73a
4
- data.tar.gz: a8bab06e6c9d0f6af1da12913e843b8582ad778d
3
+ metadata.gz: 7de4f077471615790e1d0d4993e8fe0926ef84be
4
+ data.tar.gz: b404e0b74b3a0857287424f22d530a5923eaa2f8
5
5
  SHA512:
6
- metadata.gz: 2d7dc13fd325f3586af88a73214afc60f55392d67fa3dfbbe4f13d7a8a639cc2217eba60618c1db6f12eefa1eebaa23756a6ae9f8270df5400d5268b57de98ff
7
- data.tar.gz: 8d028d23a2e55d4b3c923bc299a3d4aa02793da3826ad76a37f4b1829c291b62eb49da5f8dc3a0e26e9bc846bb1d99b2177a2493674ca7e1600db475bc45b8de
6
+ metadata.gz: f53a569a02a7aa2163559e29793f3a3890e1c6aff8f69e64864a296f738d770c99dc75196cc76643ac8bd209ada3887d916b2660ae63b97c0b7853c43424fcca
7
+ data.tar.gz: a21d15241aef3b22a3419ac27561b2b020abb342cb5a01357efa2451dea4f06419a35d22a03379b6b47941b513ed98efbb822f3e9f3d6d7712aca32b04a709cd
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## v1.4.5
2
+
3
+ - Support Ruby 2.0 and 2.1 for v1.4.4
4
+
5
+ ## v1.4.4 (yanked)
6
+
7
+ - Fix old attribute parser to be more flexible
8
+ - Accept multiple hashes as old attributes
9
+ - Accept old attributes with hash and literal
10
+
1
11
  ## v1.4.3
2
12
 
3
13
  - Allow `when` to have multiple candidates
@@ -10,18 +10,22 @@ module Hamlit
10
10
  class Attribute
11
11
  include Concerns::AttributeBuilder
12
12
 
13
- def self.build(quote, base, attributes = {})
13
+ def self.build(quote, *args)
14
14
  builder = self.new(quote)
15
- builder.build(base, attributes)
15
+ builder.build(*args)
16
16
  end
17
17
 
18
18
  def initialize(quote)
19
19
  @quote = quote
20
20
  end
21
21
 
22
- def build(base, attributes)
22
+ def build(*args)
23
23
  result = ''
24
- merge_attributes(base, attributes).each do |key, value|
24
+ attributes = args.inject({}) do |attributes, arg|
25
+ merge_attributes(attributes, arg)
26
+ end
27
+
28
+ attributes.each do |key, value|
25
29
  if value == true
26
30
  result += " #{key}"
27
31
  next
@@ -32,7 +32,7 @@ module Hamlit
32
32
  truespeed typemustmatch data].freeze
33
33
 
34
34
  def compile_old_attribute(str)
35
- raise RuntimeBuild unless Ripper.sexp(str)
35
+ raise RuntimeBuild unless valid_hash?(str)
36
36
 
37
37
  attrs = parse_old_attributes(str)
38
38
  assert_no_boolean_attributes!(attrs)
@@ -51,6 +51,13 @@ module Hamlit
51
51
 
52
52
  private
53
53
 
54
+ def valid_hash?(str)
55
+ sexp = Ripper.sexp(str)
56
+ return false unless sexp
57
+
58
+ sexp.flatten[1] == :hash
59
+ end
60
+
54
61
  def format_attributes(attributes)
55
62
  attributes = flatten_attributes(attributes)
56
63
  ignore_falsy_values(attributes)
@@ -11,6 +11,11 @@ module Hamlit
11
11
  fetch_balanced_tokens(all_tokens, :on_lparen, :on_rparen)
12
12
  end
13
13
 
14
+ def fetch_balanced_embexprs(all_tokens)
15
+ tokens = all_tokens[1..-1] # ignore first `"`
16
+ fetch_balanced_tokens(tokens, :on_embexpr_beg, :on_embexpr_end)
17
+ end
18
+
14
19
  def balanced_braces_exist?(tokens)
15
20
  balanced_tokens_exist?(tokens, :on_lbrace, :on_rbrace)
16
21
  end
@@ -19,6 +24,11 @@ module Hamlit
19
24
  balanced_tokens_exist?(tokens, :on_lparen, :on_rparen)
20
25
  end
21
26
 
27
+ def balanced_embexprs_exist?(tokens)
28
+ tokens = tokens[1..-1] # ignore first `"`
29
+ balanced_tokens_exist?(tokens, :on_embexpr_beg, :on_embexpr_end)
30
+ end
31
+
22
32
  private
23
33
 
24
34
  def fetch_balanced_tokens(all_tokens, open_token, close_token)
@@ -6,6 +6,8 @@ module Hamlit
6
6
  module Attribute
7
7
  include Concerns::Balanceable
8
8
 
9
+ EMBEXPR_PREFIX = '"#'.freeze
10
+
9
11
  def parse_attributes(scanner)
10
12
  if scanner.match?(/{/)
11
13
  parse_old_attributes(scanner) + parse_new_attributes(scanner)
@@ -16,20 +18,24 @@ module Hamlit
16
18
 
17
19
  private
18
20
 
21
+ # NOTE: Old attributes are not valid as Ruby expression.
22
+ # So Ripper is broken if you give an original expression to it.
23
+ # This method bypasses it by changing expression to string interpolation.
24
+ # Ideally you should implement an original lexer for haml old attributes.
19
25
  def parse_old_attributes(scanner)
20
26
  return [] unless scanner.match?(/{/)
21
27
 
22
- tokens = Ripper.lex(scanner.rest)
23
- until balanced_braces_exist?(tokens)
28
+ tokens = try_lex(EMBEXPR_PREFIX + scanner.rest)
29
+ until balanced_embexprs_exist?(tokens)
24
30
  @current_lineno += 1
25
31
  break unless @lines[@current_lineno]
26
32
  scanner.concat(current_line)
27
- tokens = Ripper.lex(scanner.rest)
33
+ tokens = try_lex(EMBEXPR_PREFIX + scanner.rest)
28
34
  end
29
35
 
30
- tokens = fetch_balanced_braces(tokens)
31
- scanner.pos += tokens.last.first.last + 1
32
- [tokens.map(&:last).join]
36
+ tokens = fetch_balanced_embexprs(tokens)
37
+ scanner.pos += tokens.last.first.last - 1 # remove EMBEXPR_PREFIX's offset
38
+ [tokens.map(&:last).join.gsub(/\A#/, '')]
33
39
  end
34
40
 
35
41
  def parse_new_attributes(scanner)
@@ -47,6 +53,15 @@ module Hamlit
47
53
  scanner.pos += tokens.last.first.last + 1
48
54
  [tokens.map(&:last).join]
49
55
  end
56
+
57
+ # Ripper.lex and reject tokens whose row is 0 (invalid).
58
+ # This should be used only for an expression which can
59
+ # be invalid as Ruby and valid as haml.
60
+ def try_lex(str)
61
+ Ripper.lex(str).reject do |(row, col), type, str|
62
+ row == 0
63
+ end
64
+ end
50
65
  end
51
66
  end
52
67
  end
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "1.4.3"
2
+ VERSION = "1.4.5"
3
3
  end
@@ -56,15 +56,6 @@ describe Hamlit::Engine do
56
56
  HTML
57
57
  end
58
58
 
59
- it 'renders runtime hash attribute' do
60
- assert_render(<<-'HAML', <<-HTML)
61
- - hash = { foo: 'bar' }
62
- %span{ hash }
63
- HAML
64
- <span foo='bar'></span>
65
- HTML
66
- end
67
-
68
59
  it 'renders multi-byte chars as static attribute value' do
69
60
  assert_render(<<-'HAML', <<-HTML)
70
61
  %img{ alt: 'こんにちは' }
@@ -73,6 +64,39 @@ describe Hamlit::Engine do
73
64
  HTML
74
65
  end
75
66
 
67
+ describe 'runtime attributes' do
68
+ it 'renders runtime hash attribute' do
69
+ assert_render(<<-'HAML', <<-HTML)
70
+ - hash = { foo: 'bar' }
71
+ %span{ hash }
72
+ HAML
73
+ <span foo='bar'></span>
74
+ HTML
75
+ end
76
+
77
+ it 'renders multiples hashes' do
78
+ assert_render(<<-'HAML', <<-HTML)
79
+ - h1 = { a: 'b' }
80
+ - h2 = { c: 'd' }
81
+ - h3 = { e: 'f' }
82
+ %span{ h1, h2, h3 }
83
+ HAML
84
+ <span a='b' c='d' e='f'></span>
85
+ HTML
86
+ end
87
+
88
+ it 'renders multiples hashes and literal hash' do
89
+ assert_render(<<-'HAML', <<-HTML)
90
+ - h1 = { a: 'b' }
91
+ - h2 = { c: 'd' }
92
+ - h3 = { e: 'f' }
93
+ %span{ h1, h2, h3, g: 'h', i: 'j' }
94
+ HAML
95
+ <span a='b' c='d' e='f' g='h' i='j'></span>
96
+ HTML
97
+ end
98
+ end
99
+
76
100
  describe 'joinable attributes' do
77
101
  it 'joins class with a space' do
78
102
  assert_render(<<-'HAML', <<-HTML)
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.4.3
4
+ version: 1.4.5
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-01 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escape_utils