faml 0.2.16 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  require 'parser/current'
2
2
  require 'temple'
3
- require 'faml/ast'
3
+ require 'haml_parser/ast'
4
4
  require 'faml/error'
5
5
  require 'faml/filter_compilers'
6
6
  require 'faml/helpers'
@@ -55,25 +55,25 @@ module Faml
55
55
 
56
56
  def compile(ast)
57
57
  case ast
58
- when Ast::Root
58
+ when HamlParser::Ast::Root
59
59
  compile_root(ast)
60
- when Ast::Doctype
60
+ when HamlParser::Ast::Doctype
61
61
  compile_doctype(ast)
62
- when Ast::HtmlComment
62
+ when HamlParser::Ast::HtmlComment
63
63
  compile_html_comment(ast)
64
- when Ast::HamlComment
64
+ when HamlParser::Ast::HamlComment
65
65
  compile_haml_comment(ast)
66
- when Ast::Empty
66
+ when HamlParser::Ast::Empty
67
67
  [:multi]
68
- when Ast::Element
68
+ when HamlParser::Ast::Element
69
69
  compile_element(ast)
70
- when Ast::Script
70
+ when HamlParser::Ast::Script
71
71
  compile_script(ast)
72
- when Ast::SilentScript
72
+ when HamlParser::Ast::SilentScript
73
73
  compile_silent_script(ast)
74
- when Ast::Text
74
+ when HamlParser::Ast::Text
75
75
  compile_text(ast)
76
- when Ast::Filter
76
+ when HamlParser::Ast::Filter
77
77
  compile_filter(ast)
78
78
  else
79
79
  raise "InternalError: Unknown AST node #{ast.class}: #{ast.inspect}"
@@ -108,13 +108,13 @@ module Faml
108
108
 
109
109
  def need_newline?(child)
110
110
  case child
111
- when Ast::Script
111
+ when HamlParser::Ast::Script
112
112
  child.children.empty?
113
- when Ast::SilentScript, Ast::HamlComment, Ast::Empty
113
+ when HamlParser::Ast::SilentScript, HamlParser::Ast::HamlComment, HamlParser::Ast::Empty
114
114
  false
115
- when Ast::Element
115
+ when HamlParser::Ast::Element
116
116
  !child.nuke_outer_whitespace
117
- when Ast::Filter
117
+ when HamlParser::Ast::Filter
118
118
  FilterCompilers.find(child.name).need_newline?
119
119
  else
120
120
  true
@@ -122,11 +122,11 @@ module Faml
122
122
  end
123
123
 
124
124
  def suppress_code_newline?(ast)
125
- ast.is_a?(Ast::Script) ||
126
- ast.is_a?(Ast::SilentScript) ||
127
- (ast.is_a?(Ast::Element) && suppress_code_newline?(ast.oneline_child)) ||
128
- (ast.is_a?(Ast::Element) && !ast.children.empty?) ||
129
- (ast.is_a?(Ast::HtmlComment) && !ast.conditional.empty?)
125
+ ast.is_a?(HamlParser::Ast::Script) ||
126
+ ast.is_a?(HamlParser::Ast::SilentScript) ||
127
+ (ast.is_a?(HamlParser::Ast::Element) && suppress_code_newline?(ast.oneline_child)) ||
128
+ (ast.is_a?(HamlParser::Ast::Element) && !ast.children.empty?) ||
129
+ (ast.is_a?(HamlParser::Ast::HtmlComment) && !ast.conditional.empty?)
130
130
  end
131
131
 
132
132
  def compile_text(ast)
@@ -373,9 +373,7 @@ module Faml
373
373
  else
374
374
  temple << [:code, "#{sym} = #{ast.script}"] << [:newline]
375
375
  compile_children(ast, temple)
376
- if !ast.mid_block_keyword
377
- temple << [:code, 'end']
378
- end
376
+ temple << [:code, 'end']
379
377
  end
380
378
  if !ast.escape_html && ast.preserve
381
379
  temple << [:haml, :preserve, sym]
@@ -2,7 +2,7 @@ require 'temple'
2
2
  require 'faml/compiler'
3
3
  require 'faml/html'
4
4
  require 'faml/newline'
5
- require 'faml/parser'
5
+ require 'haml_parser/parser'
6
6
 
7
7
  module Faml
8
8
  class Engine < Temple::Engine
@@ -11,7 +11,7 @@ module Faml
11
11
  filename: nil,
12
12
  )
13
13
 
14
- use Parser
14
+ use HamlParser::Parser
15
15
  use Compiler
16
16
  use Html
17
17
  filter :Escapable
@@ -1,6 +1,6 @@
1
1
  require 'strscan'
2
2
  require 'faml/error'
3
- require 'faml/parser_utils'
3
+ require 'haml_parser/utils'
4
4
 
5
5
  module Faml
6
6
  class TextCompiler
@@ -59,7 +59,7 @@ module Faml
59
59
 
60
60
  def find_close_brace(scanner, lineno)
61
61
  pos = scanner.pos
62
- depth = ParserUtils.balance(scanner, '{', '}')
62
+ depth = HamlParser::Utils.balance(scanner, '{', '}')
63
63
  if depth != 0
64
64
  raise InvalidInterpolation.new(scanner.string, lineno)
65
65
  else
@@ -1,3 +1,3 @@
1
1
  module Faml
2
- VERSION = "0.2.16"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -54,7 +54,7 @@ RSpec.describe 'Faml with Rails', type: :request do
54
54
  end
55
55
 
56
56
  describe 'compile time errors' do
57
- describe Faml::SyntaxError do
57
+ describe HamlParser::Error do
58
58
  it 'has proper backtrace' do
59
59
  expect { get '/books/syntax_error' }.to raise_error { |e|
60
60
  expect(e.backtrace[0]).to end_with('app/views/books/syntax_error.html.haml:2')
@@ -62,7 +62,7 @@ RSpec.describe 'Faml with Rails', type: :request do
62
62
  end
63
63
  end
64
64
 
65
- describe Faml::IndentTracker::IndentMismatch do
65
+ describe HamlParser::IndentTracker::IndentMismatch do
66
66
  it 'has proper backtrace' do
67
67
  expect { get '/books/indent_error' }.to raise_error { |e|
68
68
  expect(e.backtrace[0]).to end_with('app/views/books/indent_error.html.haml:3')
@@ -9,10 +9,6 @@ RSpec.describe 'Attributes rendering', type: :render do
9
9
  expect(render_string('%span{class: "x", "old" => 2} hello')).to eq(%Q{<span class='x' old='2'>hello</span>\n})
10
10
  end
11
11
 
12
- it 'is not element with id attribute' do
13
- expect(render_string('#{1 + 2}')).to eq("3\n")
14
- end
15
-
16
12
  it 'renders attributes with symbol literal' do
17
13
  expect(render_string("%span{foo: 'baz'}")).to eq("<span foo='baz'></span>\n")
18
14
  expect(render_string("%span{:foo => 'baz'}")).to eq("<span foo='baz'></span>\n")
@@ -76,10 +72,6 @@ HAML
76
72
  expect(render_string(%q|%span{class: "x\"y'z"} hello|)).to eq(%Q{<span class='x&quot;y&#39;z'>hello</span>\n})
77
73
  end
78
74
 
79
- it "doesn't parse extra brace" do
80
- expect(render_string('%span{foo: 1}{bar: 2}')).to eq("<span foo='1'>{bar: 2}</span>\n")
81
- end
82
-
83
75
  it 'renders only name if value is true' do
84
76
  expect(render_string(%q|%span{foo: true, bar: 1} hello|)).to eq(%Q{<span bar='1' foo>hello</span>\n})
85
77
  end
@@ -142,26 +134,6 @@ HAML
142
134
  expect(render_string("- h = {foo_bar: 'baz'}\n%span{data: h}")).to eq("<span data-foo-bar='baz'></span>\n")
143
135
  end
144
136
 
145
- context 'with unmatched brace' do
146
- it 'raises error' do
147
- expect { render_string('%span{foo hello') }.to raise_error(Faml::SyntaxError)
148
- end
149
-
150
- it 'tries to parse next lines' do
151
- expect(render_string(<<HAML)).to eq("<span bar='2' foo='1'>hello</span>\n")
152
- %span{foo: 1,
153
- bar: 2} hello
154
- HAML
155
- end
156
-
157
- it "doesn't try to parse next lines without trailing comma" do
158
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError)
159
- %span{foo: 1
160
- , bar: 2} hello
161
- HAML
162
- end
163
- end
164
-
165
137
  context 'with data attributes' do
166
138
  it 'renders nested attributes' do
167
139
  expect(render_string(%q|%span{data: {foo: 1, bar: 'baz', :hoge => :fuga, k1: { k2: 'v3' }}} hello|)).to eq(%Q{<span data-bar='baz' data-foo='1' data-hoge='fuga' data-k1-k2='v3'>hello</span>\n})
@@ -189,90 +161,4 @@ HAML
189
161
  a: __LINE__}
190
162
  HAML
191
163
  end
192
-
193
- describe 'with HTML-style attributes' do
194
- it 'parses simple values' do
195
- expect(render_string('%span(foo=1 bar=3) hello')).to eq("<span bar='3' foo='1'>hello</span>\n")
196
- end
197
-
198
- it 'parses variables' do
199
- expect(render_string(<<HAML)).to eq("<span bar='3' foo='xxx'>hello</span>\n")
200
- - foo = 'xxx'
201
- %span(foo=foo bar=3) hello
202
- HAML
203
- end
204
-
205
- it 'parses attributes with old syntax' do
206
- expect(render_string(<<HAML)).to eq("<span bar='3' foo='foo'>hello</span>\n")
207
- - foo = 'foo'
208
- %span(foo=foo){bar: 3} hello
209
- HAML
210
- end
211
-
212
- it 'parses multiline attribute list' do
213
- expect(render_string(<<HAML)).to eq("<span data-bar='2' data-foo='1'>\n<span>hello</span>\n</span>\n")
214
- %span{data: {foo: 1,
215
- bar: 2}}
216
- %span hello
217
- HAML
218
- end
219
-
220
- it 'parses HTML-style multiline attribute list' do
221
- expect(render_string(<<HAML)).to eq("<span bar='3' foo='1'>hello</span>\n")
222
- %span(foo=1
223
-
224
- bar=3) hello
225
- HAML
226
- end
227
-
228
- it "doesn't parse extra parens" do
229
- expect(render_string('%span(foo=1)(bar=3) hello')).to eq("<span foo='1'>(bar=3) hello</span>\n")
230
- end
231
-
232
- it 'parses quoted value' do
233
- expect(render_string('%span(foo=1 bar="baz") hello')).to eq("<span bar='baz' foo='1'>hello</span>\n")
234
- expect(render_string("%span(foo=1 bar='baz') hello")).to eq("<span bar='baz' foo='1'>hello</span>\n")
235
- end
236
-
237
- it 'parses key-only attribute' do
238
- expect(render_string('%span(foo bar=1) hello')).to eq("<span bar='1' foo>hello</span>\n")
239
- end
240
-
241
- it 'renders string interpolation' do
242
- expect(render_string(%q|%span(foo=1 bar="baz#{1 + 2}") hello|)).to eq("<span bar='baz3' foo='1'>hello</span>\n")
243
- expect(render_string(%q|%span(foo=1 bar='baz#{1 + 2}') hello|)).to eq("<span bar='baz3' foo='1'>hello</span>\n")
244
- end
245
-
246
- it 'parses escapes' do
247
- expect(render_string(%q|%span(foo=1 bar="ba\"z") hello|)).to eq("<span bar='ba&quot;z' foo='1'>hello</span>\n")
248
- expect(render_string(%q|%span(foo=1 bar='ba\'z') hello|)).to eq("<span bar='ba&#39;z' foo='1'>hello</span>\n")
249
- end
250
-
251
- it 'raises error when attributes list is unterminated' do
252
- expect { render_string('%span(foo=1 bar=2') }.to raise_error(Faml::SyntaxError)
253
- end
254
-
255
- it 'raises error when key is not alnum' do
256
- expect { render_string('%span(foo=1 3.14=3) hello') }.to raise_error(Faml::SyntaxError)
257
- end
258
-
259
- it 'raises error when value is missing' do
260
- expect { render_string('%span(foo=1 bar=) hello') }.to raise_error(Faml::SyntaxError)
261
- end
262
-
263
- it 'raises error when quote is unterminated' do
264
- expect { render_string('%span(foo=1 bar="baz) hello') }.to raise_error(Faml::SyntaxError)
265
- end
266
-
267
- it 'raises error when string interpolation is unterminated' do
268
- expect { render_string('%span(foo=1 bar="ba#{1") hello') }.to raise_error(Faml::SyntaxError)
269
- end
270
-
271
- it 'renders __LINE__ correctly' do
272
- expect(render_string(<<HAML)).to eq("<span a='2' b='1'></span>\n")
273
- %span(b=__LINE__
274
- a=__LINE__)
275
- HAML
276
- end
277
- end
278
164
  end
@@ -45,17 +45,6 @@ HAML
45
45
  /[[if IE]]
46
46
  %span hello
47
47
  world
48
- HAML
49
- end
50
-
51
- it 'raises error if conditional comment bracket is unbalanced' do
52
- expect { render_string('/[[if IE]') }.to raise_error(Faml::SyntaxError)
53
- end
54
-
55
- it 'raises error if both comment text and children are given' do
56
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError)
57
- / hehehe
58
- %span hello
59
48
  HAML
60
49
  end
61
50
  end
@@ -54,11 +54,4 @@ RSpec.describe 'Doctype rendering', type: :render do
54
54
  expect(render_string('!!! 5', format: :xhtml)).to eq(%Q|<!DOCTYPE html>\n|)
55
55
  end
56
56
  end
57
-
58
- it 'raises error when doctype has children' do
59
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError, /nesting within a header command/)
60
- !!!
61
- hello
62
- HAML
63
- end
64
57
  end
@@ -22,15 +22,6 @@ HAML
22
22
  HAML
23
23
  end
24
24
 
25
- it 'parses multi-line texts' do
26
- expect(render_string(<<HAML)).to eq("<span>\n<b>\nhello\nworld\n</b>\n</span>\n")
27
- %span
28
- %b
29
- hello
30
- world
31
- HAML
32
- end
33
-
34
25
  it 'skips empty lines' do
35
26
  expect(render_string(<<HAML)).to eq("<span>\n<b>\nhello\n</b>\n</span>\n")
36
27
  %span
@@ -54,31 +45,6 @@ HAML
54
45
  expect(render_string('%span.foo#foo-bar.bar hello')).to eq(%Q{<span class='foo bar' id='foo-bar'>hello</span>\n})
55
46
  end
56
47
 
57
- it "doesn't skip spaces before attribute list" do
58
- expect(render_string('%span {hello}')).to eq("<span>{hello}</span>\n")
59
- expect(render_string('%span (hello)')).to eq("<span>(hello)</span>\n")
60
- end
61
-
62
- context 'with invalid tag name' do
63
- it 'raises error' do
64
- expect { render_string('%.foo') }.to raise_error(Faml::SyntaxError)
65
- end
66
- end
67
-
68
- context 'with invalid classes' do
69
- it 'raises error' do
70
- expect { render_string('%span. hello') }.to raise_error(Faml::SyntaxError)
71
- expect { render_string('%span.{foo: "bar"} hello') }.to raise_error(Faml::SyntaxError)
72
- end
73
- end
74
-
75
- context 'with invalid ids' do
76
- it 'raises error' do
77
- expect { render_string('%span# hello') }.to raise_error(Faml::SyntaxError)
78
- expect { render_string('%span#{foo: "bar"} hello') }.to raise_error(Faml::SyntaxError)
79
- end
80
- end
81
-
82
48
  it 'parses #' do
83
49
  expect(render_string('#main')).to eq(%Q{<div id='main'></div>\n})
84
50
  end
@@ -87,14 +53,6 @@ HAML
87
53
  expect(render_string('.wrapper.main')).to eq(%Q{<div class='wrapper main'></div>\n})
88
54
  end
89
55
 
90
- it 'parses Ruby multiline' do
91
- expect(render_string(<<HAML)).to eq("<div>\n<span>2+3i</span>\n</div>\n")
92
- %div
93
- %span= Complex(2,
94
- 3)
95
- HAML
96
- end
97
-
98
56
  it 'parses string interpolation' do
99
57
  expect(render_string(%q|%span hello <span> #{'</span>'} </span>|)).to eq("<span>hello <span> &lt;/span&gt; </span></span>\n")
100
58
  expect(render_string(<<'HAML')).to eq("<span>\nhello <span> &lt;/span&gt; </span>\n</span>\n")
@@ -123,19 +81,4 @@ HAML
123
81
  it 'renders some attributes as self-closing by default' do
124
82
  expect(render_string('%meta{"http-equiv" => "Content-Type", :content => "text/html"}')).to eq("<meta content='text/html' http-equiv='Content-Type'>\n")
125
83
  end
126
-
127
- it 'parses == syntax' do
128
- expect(render_string('%p== =#{1+2}hello')).to eq("<p>=3hello</p>\n")
129
- end
130
-
131
- it 'raises error if self-closing tag have text' do
132
- expect { render_string('%p/ hello') }.to raise_error(Faml::SyntaxError)
133
- end
134
-
135
- it 'raises error if self-closing tag have children' do
136
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError)
137
- %p/
138
- hello
139
- HAML
140
- end
141
84
  end
@@ -1,10 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe 'filter rendering', type: :render do
4
- it 'raises error if invalid filter name is given' do
5
- expect { render_string(':filter with spaces') }.to raise_error(Faml::SyntaxError)
6
- end
7
-
8
4
  it 'raises error if unregistered filter name is given' do
9
5
  expect { render_string(':eagletmt') }.to raise_error(Faml::FilterCompilers::NotFound)
10
6
  end
@@ -17,11 +17,4 @@ HAML
17
17
  it 'raises error when interpolation is unterminated' do
18
18
  expect { render_string('%span foo#{1 + 2') }.to raise_error(Faml::TextCompiler::InvalidInterpolation)
19
19
  end
20
-
21
- it 'raises error when text has children' do
22
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError, /nesting within plain text/)
23
- hello
24
- world
25
- HAML
26
- end
27
20
  end
@@ -26,11 +26,4 @@ HAML
26
26
  expect(render_string('%span&~ "<p>hello</p>"')).to eq("<span>&lt;p&gt;hello&lt;/p&gt;</span>\n")
27
27
  end
28
28
  end
29
-
30
- context 'without Ruby code' do
31
- it 'raises error' do
32
- expect { render_string('%span&=') }.to raise_error(Faml::SyntaxError)
33
- expect { render_string('&=') }.to raise_error(Faml::SyntaxError)
34
- end
35
- end
36
29
  end
@@ -55,27 +55,4 @@ HAML
55
55
  %p= title
56
56
  HAML
57
57
  end
58
-
59
- it 'parses Ruby multiline' do
60
- expect(render_string(<<HAML)).to eq("<div>\n<span>\n2+3i\n</span>\n</div>\n")
61
- %div
62
- %span
63
- = Complex(2,
64
- 3)
65
- HAML
66
- end
67
-
68
- it 'parses == syntax' do
69
- expect(render_string('== =#{1+2}hello')).to eq("=3hello\n")
70
- end
71
-
72
- context 'without Ruby code' do
73
- it 'raises error' do
74
- expect { render_string('%span=') }.to raise_error(Faml::SyntaxError)
75
- expect { render_string(<<HAML) }.to raise_error(Faml::SyntaxError)
76
- %span
77
- =
78
- HAML
79
- end
80
- end
81
58
  end