hamlit 0.1.2 → 0.1.3

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: 6ac03a589d9cd0fe2450153fb7ec2f1b1b7ae4c8
4
- data.tar.gz: f959a39fcf2966db4858b8beedd51b577e17ba08
3
+ metadata.gz: 5197e691717bb214cdad95076a339e333b5dd792
4
+ data.tar.gz: 9c7c54d9caa52e82fc0b530a2e56a4ced80279f8
5
5
  SHA512:
6
- metadata.gz: f501dcaf6d1b0a3f3fdb49f748b99694e5de11e71c776b4f49505e20e84b3c5226cbe2400565db2531671559a8a6dbd6033a453da7608e1740b3f35558f6e71d
7
- data.tar.gz: 08bfee10675d5192d0b0e3aabdaf6ded3db138cb02c037f2b17a7bd94e45dc95a0cf4a95865849a16ab2c68c490df67bca989fd3b0c6484356a5a54ad66ebd57
6
+ metadata.gz: da4d38596e82c4810fbdf9d89c5e4ea1e41f296b14bc515eba50e1ae23f3393ea9621750c89cad01a5b0e98afe26797b3aba14a1ea5136a1e34f79312a8e34b2
7
+ data.tar.gz: 32c52ed264090db9a982b64f552e691c75fb74d28a7d4e542c98348e04fd639c86014aad59c8d70a9a98f0621abb94ecd4503b31a3524cf14cc59a89f4c5642e
@@ -22,6 +22,11 @@ module Hamlit
22
22
  def build(attributes)
23
23
  result = ''
24
24
  flatten_attributes(attributes).each do |key, value|
25
+ if value == true
26
+ result += " #{key}"
27
+ next
28
+ end
29
+
25
30
  escaped = Temple::Utils.escape_html(value)
26
31
  result += " #{key}=#{@quote}#{escaped}#{@quote}"
27
32
  end
@@ -6,22 +6,32 @@ require 'hamlit/concerns/ripperable'
6
6
  # This module compiles only old-style attribute, which is
7
7
  # surrounded by brackets.
8
8
  module Hamlit
9
+ # This error is raised when hamlit copmiler decide to
10
+ # copmile the attributes on runtime.
11
+ class RuntimeBuild < StandardError; end
12
+
9
13
  module Compilers
10
14
  module OldAttribute
11
15
  include Concerns::AttributeBuilder
12
16
  include Concerns::Balanceable
13
17
  include Concerns::Ripperable
14
18
 
19
+ # For performance, only data can be nested.
20
+ NESTABLE_ATTRIBUTES = %w[data].freeze
15
21
  IGNORED_EXPRESSIONS = %w[false nil].freeze
16
22
 
17
23
  def compile_old_attribute(str)
18
- return runtime_build(str) unless Ripper.sexp(str)
24
+ raise RuntimeBuild unless Ripper.sexp(str)
19
25
 
20
26
  attrs = parse_old_attributes(str)
21
27
  format_attributes(attrs).map do |key, value|
22
28
  next true_attribute(key) if value == 'true'
29
+ assert_static_value!(value) if NESTABLE_ATTRIBUTES.include?(key)
30
+
23
31
  [:html, :attr, key, [:dynamic, value]]
24
32
  end
33
+ rescue RuntimeBuild
34
+ return runtime_build(str)
25
35
  end
26
36
 
27
37
  private
@@ -38,6 +48,14 @@ module Hamlit
38
48
  end
39
49
  end
40
50
 
51
+ # Give up static copmile when variables are detected.
52
+ def assert_static_value!(value)
53
+ tokens = Ripper.lex(value)
54
+ tokens.each do |(row, col), type, str|
55
+ raise RuntimeBuild if type == :on_ident
56
+ end
57
+ end
58
+
41
59
  # Parse brace-balanced string and return the result as hash
42
60
  def parse_old_attributes(str)
43
61
  attributes = {}
@@ -11,7 +11,7 @@ module Hamlit
11
11
  [:code, code]
12
12
  end
13
13
 
14
- def assert!(message)
14
+ def copmile_error!(message)
15
15
  raise CompileError.new(message)
16
16
  end
17
17
 
@@ -34,7 +34,7 @@ module Hamlit
34
34
  width = count_width(line)
35
35
 
36
36
  return (width + 1) / 2 unless strict
37
- assert!('Expected to count even-width indent') if width.odd?
37
+ compile_error!('Expected to count even-width indent') if width.odd?
38
38
 
39
39
  width / 2
40
40
  end
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -48,6 +48,15 @@ describe Hamlit::Engine do
48
48
  HTML
49
49
  end
50
50
 
51
+ it 'renders nested hash whose value is variable' do
52
+ assert_render(<<-'HAML', <<-HTML)
53
+ - hash = { disable: true }
54
+ %span{ data: hash } bar
55
+ HAML
56
+ <span data-disable>bar</span>
57
+ HTML
58
+ end
59
+
51
60
  it 'renders false or nil attributes' do
52
61
  assert_render(<<-'HAML', <<-HTML)
53
62
  - hash = { checked: false }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamlit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takashi Kokubun