hamlit 0.3.3 → 0.3.4

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
  SHA1:
3
- metadata.gz: f17fccee0547b3167279c8273055849258c7260d
4
- data.tar.gz: 5af28ebd2215a7c46071937e0deb80ae160b1eab
3
+ metadata.gz: 1cd8171af3715df926bcc809abc6bac110e99fb1
4
+ data.tar.gz: 94789c593c71581ff6d36ed947914acee93e5e6f
5
5
  SHA512:
6
- metadata.gz: e9d03161eaf316804f4f036f47adb97ef18f187130ea638a573f8ebb06a4e5137a69f086ec5ce70f732295c448e96571e6db9f473832d615bf27d6e6119fc93c
7
- data.tar.gz: fcf649a051f1aa761fdb6ed9fefdbca8658a34c7e4c74e5cbc8a72cd634dab8c9300f8c1eb88126e021836338ee3c9788f30f499262709653898211bc9f29088
6
+ metadata.gz: 32471cca6c6628befa00c74685089ebac83cdde4497f51e2ba103d41a2657e925ca1360792d24c054b578fbfa091738d42848707b873811a9dcc4dc8800e6b7f
7
+ data.tar.gz: 082bf0f71595fe4c730a747f197665d5a17944ce738d8bbc9c427cdd21746a6a263a1115f56b4c474523e477f9dc6aeb23d1ba1bf71db4b726ffc3330b19836d
@@ -11,6 +11,10 @@ module Hamlit
11
11
  [:code, code]
12
12
  end
13
13
 
14
+ def syntax_error!(message)
15
+ raise Hamlit::SyntaxError.new(message)
16
+ end
17
+
14
18
  def copmile_error!(message)
15
19
  raise CompileError.new(message)
16
20
  end
@@ -48,6 +48,41 @@ module Hamlit
48
48
  return false unless line
49
49
  count_indent(line) == @current_indent
50
50
  end
51
+
52
+ # Validate the template is using consitent indentation, 2 spaces or a tab.
53
+ def validate_indentation!(template)
54
+ last_indent = ''
55
+
56
+ indents = template.scan(/^[ \t]+/)
57
+ indents.each do |indent|
58
+ if last_indent.include?(' ') && indent.include?("\t") ||
59
+ last_indent.include?("\t") && indent.include?(' ')
60
+ syntax_error!(%Q{Inconsistent indentation: #{indent_label(indent)} used for indentation, but the rest of the document was indented using #{indent_label(last_indent)}.})
61
+ end
62
+
63
+ last_indent = indent
64
+ end
65
+ end
66
+
67
+ def indent_label(indent)
68
+ return %Q{"#{indent}"} if indent.include?(' ') && indent.include?("\t")
69
+
70
+ label = indent.include?(' ') ? 'space' : 'tab'
71
+ length = indent.match(/[ \t]+/).to_s.length
72
+
73
+ "#{length} #{label}#{'s' if length > 1}"
74
+ end
75
+
76
+ # Replace hard tabs into 2 spaces
77
+ def replace_hard_tabs(template)
78
+ lines = []
79
+ template.each_line do |line|
80
+ lines << line.gsub(/^\t+/) do |match|
81
+ ' ' * (match.length * 2)
82
+ end
83
+ end
84
+ lines.join
85
+ end
51
86
  end
52
87
  end
53
88
  end
@@ -39,7 +39,10 @@ module Hamlit
39
39
 
40
40
  # Reset the parser state.
41
41
  def reset(template)
42
+ validate_indentation!(template)
43
+ template = replace_hard_tabs(template)
42
44
  template = preprocess_multilines(template)
45
+
43
46
  reset_lines(template.split("\n"))
44
47
  reset_indent
45
48
  reset_outer_removal
@@ -1,3 +1,3 @@
1
1
  module Hamlit
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -43,5 +43,14 @@ describe Hamlit::Engine do
43
43
  end
44
44
  end
45
45
  end
46
+
47
+ it 'raises syntax error for an inconsistent indentation' do
48
+ expect { render_string(<<-HAML.unindent) }.
49
+ %a
50
+ %b
51
+ \t\t%b
52
+ HAML
53
+ to raise_error(Hamlit::SyntaxError, 'Inconsistent indentation: 2 tabs used for indentation, but the rest of the document was indented using 2 spaces.')
54
+ end
46
55
  end
47
56
  end
@@ -0,0 +1,14 @@
1
+ describe Hamlit::Engine do
2
+ describe 'tab indent' do
3
+ it 'accepts tab indentation' do
4
+ assert_render(<<-HAML, <<-HTML)
5
+ %p
6
+ \t%a
7
+ HAML
8
+ <p>
9
+ <a></a>
10
+ </p>
11
+ HTML
12
+ end
13
+ end
14
+ 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: 0.3.3
4
+ version: 0.3.4
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-04-01 00:00:00.000000000 Z
11
+ date: 2015-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: temple
@@ -361,6 +361,7 @@ files:
361
361
  - spec/hamlit/engine/comment_spec.rb
362
362
  - spec/hamlit/engine/doctype_spec.rb
363
363
  - spec/hamlit/engine/error_spec.rb
364
+ - spec/hamlit/engine/indent_spec.rb
364
365
  - spec/hamlit/engine/multiline_spec.rb
365
366
  - spec/hamlit/engine/new_attribute_spec.rb
366
367
  - spec/hamlit/engine/old_attributes_spec.rb
@@ -474,6 +475,7 @@ test_files:
474
475
  - spec/hamlit/engine/comment_spec.rb
475
476
  - spec/hamlit/engine/doctype_spec.rb
476
477
  - spec/hamlit/engine/error_spec.rb
478
+ - spec/hamlit/engine/indent_spec.rb
477
479
  - spec/hamlit/engine/multiline_spec.rb
478
480
  - spec/hamlit/engine/new_attribute_spec.rb
479
481
  - spec/hamlit/engine/old_attributes_spec.rb