hamlit 1.6.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/doc/engine/indent.md +29 -0
- data/doc/engine/text.md +2 -2
- data/doc/faml/engine/indent.md +29 -0
- data/doc/faml/engine/text.md +30 -0
- data/doc/haml/engine/text.md +0 -30
- data/lib/hamlit/concerns/indentable.rb +60 -38
- data/lib/hamlit/concerns/line_reader.rb +3 -2
- data/lib/hamlit/parser.rb +4 -2
- data/lib/hamlit/version.rb +1 -1
- data/spec/hamlit/engine/error_spec.rb +13 -3
- data/spec/hamlit/engine/indent_spec.rb +28 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4848cd3ee1ccb8ba77acac4389eaadeca802b145
|
4
|
+
data.tar.gz: 13c163d9c67e46d506686337b09979a65020cf62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: babb6f83c927734400646d758cf9c2e412ceeb416e77613031b97465b93cf71158d5f0549c940e81c58bec086634e7968d23b3d2b8649bb6b1f32cc48b3a6e94
|
7
|
+
data.tar.gz: a7afb822afce6b75d1fffd69d292146c3d2b86edc0b66a57ff3b865a9847d8f1eab291fe733f9216a8e39ce6fa5ec18b819244eef8234852b858ca9377a931c4
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,13 @@
|
|
1
|
+
## v1.6.1
|
2
|
+
|
3
|
+
- Parse N-space indentation
|
4
|
+
|
1
5
|
## v1.6.0
|
2
6
|
|
3
7
|
- Fix line number of compiled code for new attributes
|
4
8
|
- Render HTML entities normally for plain text
|
9
|
+
- https://github.com/k0kubun/hamlit/issues/27
|
10
|
+
- Thanks to @jeffblake
|
5
11
|
|
6
12
|
## v1.5.9
|
7
13
|
|
data/doc/engine/indent.md
CHANGED
@@ -22,3 +22,32 @@
|
|
22
22
|
|
23
23
|
```
|
24
24
|
|
25
|
+
|
26
|
+
# [indent\_spec.rb:28](/spec/hamlit/engine/indent_spec.rb#L28)
|
27
|
+
## Input
|
28
|
+
```haml
|
29
|
+
%p
|
30
|
+
%span
|
31
|
+
foo
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
## Output
|
36
|
+
### Haml, Hamlit
|
37
|
+
```html
|
38
|
+
<p>
|
39
|
+
<span>
|
40
|
+
foo
|
41
|
+
</span>
|
42
|
+
</p>
|
43
|
+
|
44
|
+
```
|
45
|
+
|
46
|
+
### Faml
|
47
|
+
```html
|
48
|
+
<p></p>
|
49
|
+
%span
|
50
|
+
foo
|
51
|
+
|
52
|
+
```
|
53
|
+
|
data/doc/engine/text.md
CHANGED
@@ -50,7 +50,7 @@ Haml::SyntaxError: Illegal element: classes and ids must have values.
|
|
50
50
|
```
|
51
51
|
|
52
52
|
## Output
|
53
|
-
### Haml
|
53
|
+
### Haml, Hamlit
|
54
54
|
```html
|
55
55
|
|
56
56
|
|
@@ -59,7 +59,7 @@ Haml::SyntaxError: Illegal element: classes and ids must have values.
|
|
59
59
|
|
60
60
|
```
|
61
61
|
|
62
|
-
### Faml
|
62
|
+
### Faml
|
63
63
|
```html
|
64
64
|
nbsp;
|
65
65
|
|
data/doc/faml/engine/indent.md
CHANGED
@@ -22,3 +22,32 @@
|
|
22
22
|
|
23
23
|
```
|
24
24
|
|
25
|
+
|
26
|
+
# [indent\_spec.rb:28](/spec/hamlit/engine/indent_spec.rb#L28)
|
27
|
+
## Input
|
28
|
+
```haml
|
29
|
+
%p
|
30
|
+
%span
|
31
|
+
foo
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
## Output
|
36
|
+
### Faml
|
37
|
+
```html
|
38
|
+
<p></p>
|
39
|
+
%span
|
40
|
+
foo
|
41
|
+
|
42
|
+
```
|
43
|
+
|
44
|
+
### Hamlit
|
45
|
+
```html
|
46
|
+
<p>
|
47
|
+
<span>
|
48
|
+
foo
|
49
|
+
</span>
|
50
|
+
</p>
|
51
|
+
|
52
|
+
```
|
53
|
+
|
data/doc/faml/engine/text.md
CHANGED
@@ -33,3 +33,33 @@
|
|
33
33
|
|
34
34
|
```
|
35
35
|
|
36
|
+
|
37
|
+
# [text\_spec.rb:118](/spec/hamlit/engine/text_spec.rb#L118)
|
38
|
+
## Input
|
39
|
+
```haml
|
40
|
+
|
41
|
+
\
|
42
|
+
!hello
|
43
|
+
\!hello
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
## Output
|
48
|
+
### Faml
|
49
|
+
```html
|
50
|
+
nbsp;
|
51
|
+
|
52
|
+
hello
|
53
|
+
!hello
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
### Hamlit
|
58
|
+
```html
|
59
|
+
|
60
|
+
|
61
|
+
!hello
|
62
|
+
!hello
|
63
|
+
|
64
|
+
```
|
65
|
+
|
data/doc/haml/engine/text.md
CHANGED
@@ -27,33 +27,3 @@ Haml::SyntaxError: Illegal element: classes and ids must have values.
|
|
27
27
|
|
28
28
|
```
|
29
29
|
|
30
|
-
|
31
|
-
# [text\_spec.rb:118](/spec/hamlit/engine/text_spec.rb#L118)
|
32
|
-
## Input
|
33
|
-
```haml
|
34
|
-
|
35
|
-
\
|
36
|
-
!hello
|
37
|
-
\!hello
|
38
|
-
|
39
|
-
```
|
40
|
-
|
41
|
-
## Output
|
42
|
-
### Haml
|
43
|
-
```html
|
44
|
-
|
45
|
-
|
46
|
-
!hello
|
47
|
-
!hello
|
48
|
-
|
49
|
-
```
|
50
|
-
|
51
|
-
### Hamlit
|
52
|
-
```html
|
53
|
-
nbsp;
|
54
|
-
|
55
|
-
hello
|
56
|
-
!hello
|
57
|
-
|
58
|
-
```
|
59
|
-
|
@@ -8,12 +8,14 @@ module Hamlit
|
|
8
8
|
include Concerns::Error
|
9
9
|
|
10
10
|
def reset_indent
|
11
|
+
@indent_logs = []
|
11
12
|
@current_indent = 0
|
12
13
|
end
|
13
14
|
|
14
15
|
# Return nearest line's indent level since next line. This method ignores
|
15
16
|
# empty line. It returns -1 if next_line does not exist.
|
16
17
|
def next_indent
|
18
|
+
return 1 if !@indent_space && fetch_indent(next_line).length > 0
|
17
19
|
count_indent(next_line)
|
18
20
|
end
|
19
21
|
|
@@ -28,19 +30,16 @@ module Hamlit
|
|
28
30
|
@current_indent -= 1
|
29
31
|
end
|
30
32
|
|
31
|
-
def count_indent(line
|
33
|
+
def count_indent(line)
|
32
34
|
return EOF unless line
|
33
|
-
|
35
|
+
return 0 if indent_rule == 0
|
34
36
|
|
35
|
-
|
36
|
-
compile_error!('Expected to count even-width indent') if width.odd?
|
37
|
-
|
38
|
-
width / 2
|
37
|
+
line.match(/\A[ \t]+/).to_s.length / indent_rule
|
39
38
|
end
|
40
39
|
|
41
40
|
def count_width(line)
|
42
41
|
return EOF unless line
|
43
|
-
line[/\A +/].to_s.length
|
42
|
+
line[/\A[ \t]+/].to_s.length
|
44
43
|
end
|
45
44
|
|
46
45
|
def same_indent?(line)
|
@@ -50,31 +49,30 @@ module Hamlit
|
|
50
49
|
|
51
50
|
# Validate current line's indentation
|
52
51
|
def validate_indentation!(ast)
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
if
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
52
|
+
return true unless next_line
|
53
|
+
|
54
|
+
indent = fetch_indent(next_line)
|
55
|
+
if indent.include?(' ') && indent.include?("\t")
|
56
|
+
syntax_error!("Indentation can't use both tabs and spaces.")
|
57
|
+
end
|
58
|
+
@indent_logs << indent
|
59
|
+
|
60
|
+
if !@indent_space && @indent_logs.last != ''
|
61
|
+
@indent_space = @indent_logs.last
|
62
62
|
end
|
63
|
-
|
63
|
+
validate_indentation_consistency!(indent)
|
64
|
+
|
65
|
+
next_indent != @current_indent
|
64
66
|
end
|
65
67
|
|
66
68
|
# Validate the template is using consitent indentation, 2 spaces or a tab.
|
67
|
-
def validate_indentation_consistency!(
|
68
|
-
|
69
|
-
|
70
|
-
indents = template.scan(/^[ \t]+/)
|
71
|
-
indents.each do |indent|
|
72
|
-
if last_indent.include?(' ') && indent.include?("\t") ||
|
73
|
-
last_indent.include?("\t") && indent.include?(' ')
|
74
|
-
syntax_error!(%Q{Inconsistent indentation: #{indent_label(indent)} used for indentation, but the rest of the document was indented using #{indent_label(last_indent)}.})
|
75
|
-
end
|
69
|
+
def validate_indentation_consistency!(indent)
|
70
|
+
return false if indent.empty?
|
71
|
+
return false if !@indent_space || @indent_space.empty?
|
76
72
|
|
77
|
-
|
73
|
+
if indent[0] != @indent_space[0] || indent.length < @indent_space.length
|
74
|
+
syntax_error!("Inconsistent indentation: #{indent_label(indent)} used for indentation, "\
|
75
|
+
"but the rest of the document was indented using #{indent_label(@indent_space)}.")
|
78
76
|
end
|
79
77
|
end
|
80
78
|
|
@@ -87,20 +85,44 @@ module Hamlit
|
|
87
85
|
"#{length} #{label}#{'s' if length > 1}"
|
88
86
|
end
|
89
87
|
|
90
|
-
# Replace hard tabs into 2 spaces
|
91
|
-
def replace_hard_tabs(template)
|
92
|
-
lines = []
|
93
|
-
template.each_line do |line|
|
94
|
-
lines << line.gsub(/^\t+/) do |match|
|
95
|
-
' ' * (match.length * 2)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
lines.join
|
99
|
-
end
|
100
|
-
|
101
88
|
def has_block?
|
89
|
+
return false unless next_line
|
90
|
+
return fetch_indent(next_line).length > 0 unless @indent_space
|
91
|
+
|
102
92
|
next_indent > @current_indent
|
103
93
|
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def indent_label(indent)
|
98
|
+
return %Q{"#{indent}"} if indent.include?(' ') && indent.include?("\t")
|
99
|
+
|
100
|
+
label = indent.include?(' ') ? 'space' : 'tab'
|
101
|
+
length = indent.match(/[ \t]+/).to_s.length
|
102
|
+
|
103
|
+
"#{length} #{label}#{'s' if length > 1}"
|
104
|
+
end
|
105
|
+
|
106
|
+
def count_width(line)
|
107
|
+
return EOF unless line
|
108
|
+
line[/\A +/].to_s.length
|
109
|
+
end
|
110
|
+
|
111
|
+
def next_space
|
112
|
+
next_line[/\A +/].to_s
|
113
|
+
end
|
114
|
+
|
115
|
+
def next_width
|
116
|
+
count_width(next_line)
|
117
|
+
end
|
118
|
+
|
119
|
+
def indent_rule
|
120
|
+
(@indent_space || '').length
|
121
|
+
end
|
122
|
+
|
123
|
+
def fetch_indent(str)
|
124
|
+
str.match(/^[ \t]+/).to_s
|
125
|
+
end
|
104
126
|
end
|
105
127
|
end
|
106
128
|
end
|
@@ -25,7 +25,7 @@ module Hamlit
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def skip_lines
|
28
|
-
while next_indent >= @current_indent
|
28
|
+
while next_line && next_indent >= @current_indent
|
29
29
|
@current_lineno += 1
|
30
30
|
end
|
31
31
|
end
|
@@ -47,7 +47,8 @@ module Hamlit
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def read_line?
|
50
|
-
return
|
50
|
+
return false unless next_line
|
51
|
+
return true if next_line.index(/^#{@indent_logs.last}[ \t]/)
|
51
52
|
|
52
53
|
line = @lines[@current_lineno + 1]
|
53
54
|
return false unless line
|
data/lib/hamlit/parser.rb
CHANGED
@@ -38,7 +38,6 @@ module Hamlit
|
|
38
38
|
# Reset the parser state.
|
39
39
|
def reset(template)
|
40
40
|
validate_indentation_consistency!(template)
|
41
|
-
template = replace_hard_tabs(template)
|
42
41
|
template = preprocess_multilines(template)
|
43
42
|
|
44
43
|
reset_lines(template.split("\n"))
|
@@ -62,6 +61,9 @@ module Hamlit
|
|
62
61
|
ast << [:static, "\n"] unless skip_newline?(node)
|
63
62
|
end
|
64
63
|
ast
|
64
|
+
rescue => e
|
65
|
+
ast << syntax_error(e.message)
|
66
|
+
ast
|
65
67
|
end
|
66
68
|
|
67
69
|
# Parse current line and return AST.
|
@@ -69,7 +71,7 @@ module Hamlit
|
|
69
71
|
return [:multi] if empty_line?(line)
|
70
72
|
|
71
73
|
scanner = wrap_scanner(line)
|
72
|
-
scanner.scan(/ +/)
|
74
|
+
scanner.scan(/[ \t]+/)
|
73
75
|
|
74
76
|
unless inline
|
75
77
|
ast = parse_outer_line(scanner)
|
data/lib/hamlit/version.rb
CHANGED
@@ -11,16 +11,18 @@ describe Hamlit::Engine do
|
|
11
11
|
expect { render_string(<<-HAML.unindent) }.
|
12
12
|
%a
|
13
13
|
%b
|
14
|
+
%a
|
14
15
|
HAML
|
15
|
-
to raise_error(Hamlit::SyntaxError, '
|
16
|
+
to raise_error(Hamlit::SyntaxError, 'Inconsistent indentation: 1 space used for indentation, but the rest of the document was indented using 4 spaces.')
|
16
17
|
end
|
17
18
|
|
18
19
|
it 'raises syntax error for illegal indentation' do
|
19
20
|
expect { render_string(<<-HAML.unindent) }.
|
20
21
|
%a
|
21
|
-
|
22
|
+
\t\t%a
|
23
|
+
\t%a
|
22
24
|
HAML
|
23
|
-
to raise_error(Hamlit::SyntaxError, '
|
25
|
+
to raise_error(Hamlit::SyntaxError, 'Inconsistent indentation: 1 tab used for indentation, but the rest of the document was indented using 2 tabs.')
|
24
26
|
end
|
25
27
|
|
26
28
|
it 'raises syntax error which has correct line number in backtrace' do
|
@@ -59,5 +61,13 @@ describe Hamlit::Engine do
|
|
59
61
|
HAML
|
60
62
|
to raise_error(Hamlit::SyntaxError, 'Unbalanced brackets.')
|
61
63
|
end
|
64
|
+
|
65
|
+
it 'raises syntax error for an inconsistent indentation' do
|
66
|
+
expect { render_string(<<-HAML.unindent) }.
|
67
|
+
%p
|
68
|
+
\t %span
|
69
|
+
HAML
|
70
|
+
to raise_error(Hamlit::SyntaxError, "Indentation can't use both tabs and spaces.")
|
71
|
+
end
|
62
72
|
end
|
63
73
|
end
|
@@ -10,5 +10,33 @@ describe Hamlit::Engine do
|
|
10
10
|
</p>
|
11
11
|
HTML
|
12
12
|
end
|
13
|
+
|
14
|
+
it 'accepts N-space indentation' do
|
15
|
+
assert_render(<<-HAML, <<-HTML)
|
16
|
+
%p
|
17
|
+
%span
|
18
|
+
foo
|
19
|
+
HAML
|
20
|
+
<p>
|
21
|
+
<span>
|
22
|
+
foo
|
23
|
+
</span>
|
24
|
+
</p>
|
25
|
+
HTML
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'accepts N-tab indentation' do
|
29
|
+
assert_render(<<-HAML, <<-HTML, compatible_only: :haml)
|
30
|
+
%p
|
31
|
+
\t%span
|
32
|
+
\t\tfoo
|
33
|
+
HAML
|
34
|
+
<p>
|
35
|
+
<span>
|
36
|
+
foo
|
37
|
+
</span>
|
38
|
+
</p>
|
39
|
+
HTML
|
40
|
+
end
|
13
41
|
end
|
14
42
|
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.
|
4
|
+
version: 1.6.1
|
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-
|
11
|
+
date: 2015-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: escape_utils
|
@@ -603,3 +603,4 @@ test_files:
|
|
603
603
|
- spec/spec_helper/document_generator.rb
|
604
604
|
- spec/spec_helper/render_helper.rb
|
605
605
|
- spec/spec_helper/test_case.rb
|
606
|
+
has_rdoc:
|