baby_erubis 1.0.0 → 2.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86a6cc722ec2c51e207330c70c8202f7f6108b07
4
+ data.tar.gz: 9f0062ca834b2954323f772dc65e9cb4d8ba5055
5
+ SHA512:
6
+ metadata.gz: 8f716b9a9866e7d4d9787208f5323cdf58e1c1c22d79ed1a605f4fc4b10f2ae98b3eb6d5a30c5125061497fa91448d2cfd51dd9ac5c9f7e2a960506c24efbc42
7
+ data.tar.gz: 2f2753b15878d8ae7dad9eaf3673e972ce970ec04aa1f1c4769c983fbe26ea2f4b354708eebbbb8cd29660d1180fa57906630b8d14016f610718246c65f2f29d
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  BabyErubis.rb
2
2
  =============
3
3
 
4
- $Release: 1.0.0 $
4
+ $Release: 2.0.0 $
5
5
 
6
6
  BabyErubis is an yet another eRuby implementation, based on Erubis.
7
7
 
@@ -9,6 +9,7 @@ BabyErubis is an yet another eRuby implementation, based on Erubis.
9
9
  * Easy to customize
10
10
  * Supports HTML as well as plain text
11
11
  * Accepts both template file and template string
12
+ * Supports Ruby on Rails template
12
13
 
13
14
  BabyErubis supports Ruby >= 1.8 and Rubinius >= 2.0.
14
15
 
@@ -77,6 +78,19 @@ Expression in `<%= ... %>` is escaped according to template class.
77
78
  (Experimental) `<%- ... -%>` and `<%-= ... -%>` are handled same as
78
79
  `<% ... %>` and `<%= ... %>` respectively.
79
80
 
81
+ (Experimental) Block argument expression supported since version 2.0.
82
+ Example:
83
+
84
+ ## template
85
+ <%== form_for(:article) do |f| %>
86
+ ...
87
+ <% end %>
88
+
89
+ ## compiled ruby code
90
+ _buf << form_for(:article) do |f| _buf << '
91
+ ...
92
+ '; end;
93
+
80
94
 
81
95
 
82
96
  Advanced Topics
@@ -151,6 +165,40 @@ And you can control whether to use freeze() or not.
151
165
  # '.freeze; _buf.to_s
152
166
 
153
167
 
168
+ Ruby on Rails Template
169
+ ----------------------
170
+
171
+ `BabyErubis::RailsTemplate` class generates Rails-style ruby code.
172
+
173
+ require 'baby_erubis'
174
+ require 'baby_erubis/rails'
175
+
176
+ t = BabyErubis::RailsTemplate.new.from_str <<'END'
177
+ <div>
178
+ <%= form_for :article do |f| %>
179
+ ...
180
+ <% end %>
181
+ </div>
182
+ END
183
+ print t.src
184
+
185
+ Result:
186
+
187
+ @output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_append='<div>
188
+ ';@output_buffer.append= form_for :article do |f| ;@output_buffer.safe_append='
189
+ ...
190
+ '.freeze; end;
191
+ @output_buffer.safe_append='</div>
192
+ ';@output_buffer.to_s
193
+
194
+ You can check syntax of Rails template in command-line:
195
+
196
+ $ baby_erubis -Rx app/views/articles/index.html.erb | ruby -wc
197
+
198
+
199
+ (TODO: How to use BabyErubis in Ruby on Rails instead of Erubis)
200
+
201
+
154
202
 
155
203
  Customizing
156
204
  ===========
@@ -313,7 +361,7 @@ Sample code:
313
361
  Todo
314
362
  ====
315
363
 
316
- * Support Rails syntax (= `<%= form_for do |f| %>`)
364
+ * [Done] Support Rails syntax (= `<%= form_for do |f| %>`)
317
365
 
318
366
 
319
367
 
@@ -321,6 +369,14 @@ Changes
321
369
  =======
322
370
 
323
371
 
372
+ Release 2.0.0 (2014-12-09)
373
+ --------------------------
374
+
375
+ * [enhance] Ruby on Rails template support
376
+ * [enhance] Block argument expression support
377
+ * [enhance] New command-line option '-R' and '--format=rails'
378
+
379
+
324
380
  Release 1.0.0 (2014-05-17)
325
381
  --------------------------
326
382
 
data/Rakefile CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ###
4
4
 
5
- RELEASE = ENV['rel'] || '0.0.0'
5
+ RELEASE = ENV['rel'] || '2.0.0'
6
6
  COPYRIGHT = 'copyright(c) 2014 kuwata-lab.com all rights reserved'
7
7
  LICENSE = 'MIT License'
8
8
 
data/baby_erubis.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/ruby
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $License: MIT License $
6
6
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
7
7
  ###
@@ -13,7 +13,8 @@ Gem::Specification.new do |s|
13
13
  s.name = "baby_erubis"
14
14
  s.author = "makoto kuwata"
15
15
  s.email = "kwa(at)kuwata-lab.com"
16
- s.version = "$Release: 1.0.0 $".split()[1]
16
+ s.version = "$Release: 2.0.0 $".split()[1]
17
+ s.license = "MIT License"
17
18
  s.platform = Gem::Platform::RUBY
18
19
  s.homepage = "https://github.com/kwatch/BabyErubis/tree/ruby"
19
20
  s.summary = "yet another eRuby implementation based on Erubis"
@@ -30,7 +31,8 @@ END
30
31
 
31
32
  ## files
32
33
  files = []
33
- files += Dir.glob('lib/*.rb')
34
+ files << 'lib/baby_erubis.rb'
35
+ files << 'lib/baby_erubis/rails.rb'
34
36
  files += Dir.glob('test/*.rb')
35
37
  files += ['bin/baby_erubis']
36
38
  files += %w[README.md MIT-LICENSE setup.rb baby_erubis.gemspec Rakefile]
data/bin/baby_erubis CHANGED
@@ -2,7 +2,7 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  ###
5
- ### $Release: 1.0.0 $
5
+ ### $Release: 2.0.0 $
6
6
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
7
7
  ### $License: MIT License $
8
8
  ###
@@ -245,10 +245,9 @@ module BabyErubis
245
245
 
246
246
  module HideTextEnhander
247
247
 
248
- def build_text(text)
249
- return text.to_s.gsub(/^.*\n/, "\n").gsub(/./, ' ')
248
+ def add_text(src, text)
249
+ src << text.to_s.gsub(/^.*\n/, "\n").gsub(/./, ' ')
250
250
  end
251
- alias _t build_text
252
251
 
253
252
  end
254
253
 
@@ -284,7 +283,8 @@ class Main
284
283
  return
285
284
  end
286
285
  #
287
- klass = handle_format(options['format'] || (options['H'] ? 'html' : nil))
286
+ format = options['format'] || (options['H'] && 'html') || (options['R'] && 'rails')
287
+ klass = handle_format(format)
288
288
  if options['X']
289
289
  klass = Class.new(klass)
290
290
  klass.class_eval { include BabyErubis::HideTextEnhander }
@@ -330,8 +330,9 @@ class Main
330
330
  parser.option("-c context : context string (yaml inline style or ruby code)")
331
331
  parser.option("-f file : context data file (*.yaml, *.json, or *.rb)")
332
332
  parser.option("-H : same as --format=html")
333
- parser.option(" --format={text|html} : format (default: text)")\
334
- .validation {|arg| "'text' or 'html' expected" if arg !~ /\A(text|html)\z/ }
333
+ parser.option("-R : same as --format=rails")
334
+ parser.option(" --format=format : 'text', 'html' or 'rails' (default: text)")\
335
+ .validation {|arg| "'text', 'html' or 'rails' expected" if arg !~ /\A(text|html|rails)\z/ }
335
336
  parser.option(" --encoding=name : encoding (default: utf-8)")
336
337
  parser.option(" --freeze={true|false} : use String#freeze() or not")\
337
338
  .validation {|arg| "'true' or 'false' expected" if arg !~ /\A(true|false)\z/ }
@@ -341,7 +342,7 @@ class Main
341
342
 
342
343
  def build_help_message(parser)
343
344
  s = "Usage: #{@cmdname} [..options..] [erubyfile]\n"
344
- s << parser.help_message(30)
345
+ s << parser.help_message(28)
345
346
  s << "\n"
346
347
  s << <<"END"
347
348
  Example:
@@ -365,6 +366,7 @@ END
365
366
  when nil ; return BabyErubis::Text
366
367
  when 'text'; return BabyErubis::Text
367
368
  when 'html'; return BabyErubis::Html
369
+ when 'rails'; require 'baby_erubis/rails'; return BabyErubis::RailsTemplate
368
370
  else
369
371
  raise "** unreachable: format=#{format.inspect}"
370
372
  end
data/lib/baby_erubis.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -25,7 +25,7 @@
25
25
 
26
26
  module BabyErubis
27
27
 
28
- RELEASE = '$Release: 1.0.0 $'.split(' ')[1]
28
+ RELEASE = '$Release: 2.0.0 $'.split(' ')[1]
29
29
 
30
30
 
31
31
  class Template
@@ -54,8 +54,8 @@ module BabyErubis
54
54
 
55
55
  attr_reader :src
56
56
 
57
- #PATTERN = /(^[ \t]*)?<%(==?|\#)?(.*?)%>([ \t]*\r?\n)?/m
58
- PATTERN = /(^[ \t]*)?<%-?(==?|\#)? ?(.*?) ?-?%>([ \t]*\r?\n)?/m
57
+ #PATTERN = /(^[ \t]*)?<%(\#)?(==?)?(.*?)%>([ \t]*\r?\n)?/m
58
+ PATTERN = /(^[ \t]*)?<%-?(\#)?(==?)? ?(.*?) ?-?%>([ \t]*\r?\n)?/m
59
59
 
60
60
  def pattern
61
61
  return self.class.const_get(:PATTERN)
@@ -68,32 +68,37 @@ module BabyErubis
68
68
  end
69
69
 
70
70
  def parse(input)
71
- src = "_buf = '';" # preamble
71
+ src = ""
72
+ add_preamble(src) # preamble
73
+ spc = ""
72
74
  pos = 0
73
- input.scan(pattern()) do |lspace, ch, code, rspace|
75
+ input.scan(pattern()) do |lspace, sharp, ch, code, rspace|
74
76
  match = Regexp.last_match
75
77
  text = input[pos, match.begin(0) - pos]
76
78
  pos = match.end(0)
77
- src << _t(text)
78
- if ! ch # statement
79
- if lspace && rspace
80
- src << "#{lspace} #{code};#{rspace}"
81
- else
82
- src << _t(lspace) << " #{code};" << _t(rspace)
79
+ if sharp # comment
80
+ code = ("\n" * code.count("\n"))
81
+ if ! ch && lspace && rspace # trimmed statement
82
+ add_text(src, "#{spc}#{text}"); add_stmt(src, "#{code}#{rspace}")
83
+ rspace = ""
84
+ else # other statement or expression
85
+ add_text(src, "#{spc}#{text}#{lspace}"); add_stmt(src, code)
83
86
  end
84
- elsif ch == '=' # expression (escaping)
85
- src << _t(lspace) << " _buf << #{escaped_expr(code)};" << _t(rspace)
86
- elsif ch == '==' # expression (without escaping)
87
- src << _t(lspace) << " _buf << (#{code}).to_s;" << _t(rspace)
88
- elsif ch == '#' # comment
89
- src << _t(lspace) << ("\n" * code.count("\n")) << _t(rspace)
90
87
  else
91
- raise "** unreachable: ch=#{ch.inspect}"
88
+ if ch # expression
89
+ add_text(src, "#{spc}#{text}#{lspace}"); add_expr(src, code, ch)
90
+ elsif lspace && rspace # statement (trimming)
91
+ add_text(src, "#{spc}#{text}"); add_stmt(src, "#{lspace} #{code};#{rspace}")
92
+ rspace = ""
93
+ else # statement (without trimming)
94
+ add_text(src, "#{spc}#{text}#{lspace}"); add_stmt(src, " #{code};")
95
+ end
92
96
  end
97
+ spc = rspace
93
98
  end
94
99
  text = pos == 0 ? input : input[pos..-1] # or $' || input
95
- src << _t(text)
96
- src << " _buf.to_s\n" # postamble
100
+ add_text(src, "#{spc}#{text}")
101
+ add_postamble(src) # postamble
97
102
  return src
98
103
  end
99
104
 
@@ -108,24 +113,47 @@ module BabyErubis
108
113
 
109
114
  protected
110
115
 
111
- def escaped_expr(code)
112
- return "(#{code}).to_s"
116
+ def add_preamble(src)
117
+ src << "_buf = '';"
113
118
  end
114
119
 
115
- private
120
+ def add_postamble(src)
121
+ src << " _buf.to_s\n"
122
+ end
116
123
 
117
- def build_text(text)
124
+ def add_text(src, text)
125
+ return if !text || text.empty?
118
126
  freeze = @freeze ? '.freeze' : ''
119
- return text && !text.empty? ? " _buf << '#{escape_text(text)}'#{freeze};" : ''
120
- #return text && !text.empty? ? " _buf << %q`#{escape_text(text)}`#{freeze};" : ''
127
+ text.gsub!(/['\\]/, '\\\\\&')
128
+ src << " _buf << '#{text}'#{freeze};"
129
+ end
130
+
131
+ def add_stmt(src, stmt)
132
+ return if !stmt || stmt.empty?
133
+ src << stmt
134
+ end
135
+
136
+ def add_expr(src, expr, indicator)
137
+ return if !expr || expr.empty?
138
+ if expr_has_block(expr)
139
+ src << " _buf << #{expr}"
140
+ elsif indicator == '=' # escaping
141
+ src << " _buf << #{escaped_expr(expr)};"
142
+ else # without escaping
143
+ src << " _buf << (#{expr}).to_s;"
144
+ end
145
+ end
146
+
147
+ def escaped_expr(code)
148
+ return "(#{code}).to_s"
121
149
  end
122
- alias _t build_text
123
150
 
124
- def escape_text(text)
125
- return text.gsub!(/['\\]/, '\\\\\&') || text
126
- #return text.gsub!(/[`\\]/, '\\\\\&') || text
151
+ def expr_has_block(expr)
152
+ return expr =~ /(\bdo|\{)\s*(\|[^|]*?\|\s*)?\z/
127
153
  end
128
154
 
155
+ private
156
+
129
157
  def empty_binding
130
158
  return binding()
131
159
  end
@@ -0,0 +1,53 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 2.0.0 $
5
+ ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ require 'baby_erubis'
10
+
11
+
12
+ module BabyErubis
13
+
14
+
15
+ class RailsTemplate < Template
16
+
17
+ protected
18
+
19
+ def add_preamble(src)
20
+ src << "@output_buffer = output_buffer || ActionView::OutputBuffer.new;"
21
+ end
22
+
23
+ def add_postamble(src)
24
+ src << "@output_buffer.to_s\n"
25
+ end
26
+
27
+ def add_text(src, text)
28
+ return if !text || text.empty?
29
+ freeze = @freeze ? '.freeze' : ''
30
+ text.gsub!(/['\\]/, '\\\\\&')
31
+ src << "@output_buffer.safe_append='#{text}'#{freeze};"
32
+ end
33
+
34
+ def add_stmt(src, stmt)
35
+ return if !stmt || stmt.empty?
36
+ src << stmt
37
+ end
38
+
39
+ def add_expr(src, expr, indicator)
40
+ return if !expr || expr.empty?
41
+ l = '('; r = ')'
42
+ l = r = ' ' if expr_has_block(expr)
43
+ if indicator == '=' # escaping
44
+ src << "@output_buffer.append=#{l}#{expr}#{r};"
45
+ else # without escaping
46
+ src << "@output_buffer.safe_append=#{l}#{expr}#{r};"
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+
53
+ end
data/test/context_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -0,0 +1,106 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 2.0.0 $
5
+ ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ libpath = File.class_eval { join(dirname(dirname(__FILE__)), 'lib') }
10
+ $: << libpath unless $:.include?(libpath)
11
+
12
+ require 'minitest/autorun'
13
+
14
+ require 'baby_erubis/rails'
15
+
16
+
17
+
18
+ describe 'BabyErubis::RailsTemplate' do
19
+
20
+ let(:tmpl) { BabyErubis::RailsTemplate.new }
21
+
22
+ def _modify(ruby_code)
23
+ if (''.freeze).equal?(''.freeze)
24
+ return ruby_code.gsub(/([^'])';/m, "\\1'.freeze;")
25
+ else
26
+ return ruby_code
27
+ end
28
+ end
29
+
30
+ eruby_template = <<'END'
31
+ <h1>New Article</h1>
32
+
33
+ <%= form_for :article, url: articles_path do |f| %>
34
+ <p>
35
+ <%= f.label :title %><br>
36
+ <%= f.text_field :title %>
37
+ </p>
38
+
39
+ <p>
40
+ <%= f.label :text %><br>
41
+ <%= f.text_area :text %>
42
+ </p>
43
+
44
+ <p>
45
+ <%= f.submit %>
46
+ </p>
47
+ <% end %>
48
+ END
49
+
50
+ ruby_code = <<'END'
51
+ @output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_append='<h1>New Article</h1>
52
+
53
+ ';@output_buffer.append= form_for :article, url: articles_path do |f| ;@output_buffer.safe_append='
54
+ <p>
55
+ ';@output_buffer.append=(f.label :title);@output_buffer.safe_append='<br>
56
+ ';@output_buffer.append=(f.text_field :title);@output_buffer.safe_append='
57
+ </p>
58
+
59
+ <p>
60
+ ';@output_buffer.append=(f.label :text);@output_buffer.safe_append='<br>
61
+ ';@output_buffer.append=(f.text_area :text);@output_buffer.safe_append='
62
+ </p>
63
+
64
+ <p>
65
+ ';@output_buffer.append=(f.submit);@output_buffer.safe_append='
66
+ </p>
67
+ '; end;
68
+ @output_buffer.to_s
69
+ END
70
+
71
+
72
+ describe '#parse()' do
73
+
74
+ it "converts eRuby template into ruby code with Rails style." do
75
+ tmpl = BabyErubis::RailsTemplate.new.from_str(eruby_template)
76
+ expected = _modify(ruby_code)
77
+ assert_equal expected, tmpl.src
78
+ end
79
+
80
+ it "can understand block such as <%= form_for do |x| %>." do
81
+ s = <<-'END'
82
+ <%= (1..3).each do %>
83
+ Hello
84
+ <% end %>
85
+ END
86
+ tmpl = BabyErubis::RailsTemplate.new.from_str(s)
87
+ assert_match /\@output_buffer.append= \(1\.\.3\)\.each do ;/, tmpl.src
88
+ #
89
+ s = <<-'END'
90
+ <%= (1..3).each do |x, y|%>
91
+ Hello
92
+ <% end %>
93
+ END
94
+ tmpl = BabyErubis::RailsTemplate.new.from_str(s)
95
+ assert_match /\@output_buffer.append= \(1\.\.3\)\.each do \|x, y\| ;/, tmpl.src
96
+ end
97
+
98
+ it "doesn't misunderstand <%= @todo %> as block" do
99
+ tmpl = BabyErubis::RailsTemplate.new.from_str("<b><%= @todo %></b>")
100
+ assert_match /\@output_buffer\.append=\(\@todo\);/, tmpl.src
101
+ end
102
+
103
+ end
104
+
105
+
106
+ end
data/test/run_all.rb CHANGED
@@ -1,12 +1,12 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
8
8
 
9
9
  here = File.dirname(File.expand_path(__FILE__))
10
- Dir.glob(here + '/**/*.rb').each do |fpath|
10
+ Dir.glob(here + '/**/*_test.rb').each do |fpath|
11
11
  require fpath
12
12
  end
data/test/script_test.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -71,19 +71,20 @@ describe Main do
71
71
 
72
72
  help_message = <<'END'.gsub(/\$SCRIPT/, File.basename($0))
73
73
  Usage: $SCRIPT [..options..] [erubyfile]
74
- -h, --help : help
75
- -v, --version : version
76
- -x : show ruby code
77
- -X : show ruby code only (no text part)
78
- -N : numbering: add line numbers (for '-x/-X')
79
- -U : unique: compress empty lines (for '-x/-X')
80
- -C : compact: remove empty lines (for '-x/-X')
81
- -c context : context string (yaml inline style or ruby code)
82
- -f file : context data file (*.yaml, *.json, or *.rb)
83
- -H : same as --format=html
84
- --format={text|html} : format (default: text)
85
- --encoding=name : encoding (default: utf-8)
86
- --freeze={true|false} : use String#freeze() or not
74
+ -h, --help : help
75
+ -v, --version : version
76
+ -x : show ruby code
77
+ -X : show ruby code only (no text part)
78
+ -N : numbering: add line numbers (for '-x/-X')
79
+ -U : unique: compress empty lines (for '-x/-X')
80
+ -C : compact: remove empty lines (for '-x/-X')
81
+ -c context : context string (yaml inline style or ruby code)
82
+ -f file : context data file (*.yaml, *.json, or *.rb)
83
+ -H : same as --format=html
84
+ -R : same as --format=rails
85
+ --format=format : 'text', 'html' or 'rails' (default: text)
86
+ --encoding=name : encoding (default: utf-8)
87
+ --freeze={true|false} : use String#freeze() or not
87
88
 
88
89
  Example:
89
90
  ## convert eRuby file into Ruby code
@@ -145,6 +146,22 @@ _buf = ''; _buf << '<html>
145
146
  </body>
146
147
  </html>
147
148
  '; _buf.to_s
149
+ END
150
+ SOURCE_RAILS = <<'END'
151
+ @output_buffer = output_buffer || ActionView::OutputBuffer.new;@output_buffer.safe_append='<html>
152
+ <body>
153
+ <h1>';@output_buffer.append=(@title);@output_buffer.safe_append='</h1>
154
+ <h1>';@output_buffer.safe_append=(@title);@output_buffer.safe_append='</h1>
155
+ <div>
156
+ <ul>
157
+ '; for item in @items;
158
+ @output_buffer.safe_append=' <li>';@output_buffer.append=(item);@output_buffer.safe_append='</li>
159
+ '; end;
160
+ @output_buffer.safe_append=' </ul>
161
+ </div>
162
+ </body>
163
+ </html>
164
+ ';@output_buffer.to_s
148
165
  END
149
166
  SOURCE_NO_TEXT = <<'END'
150
167
  _buf = '';
@@ -345,6 +362,19 @@ END
345
362
  end
346
363
 
347
364
 
365
+ describe '-R' do
366
+
367
+ it "uses Rails-style template." do
368
+ sout, serr = with_erubyfile do |fname|
369
+ dummy_stdio { Main.main(['-Rx', fname]) }
370
+ end
371
+ assert_equal _modify(SOURCE_RAILS), sout
372
+ assert_equal "", serr
373
+ end
374
+
375
+ end
376
+
377
+
348
378
  describe '-c cotnext' do
349
379
 
350
380
  it "can specify context data in YAML format." do
@@ -501,7 +531,7 @@ END
501
531
  end
502
532
 
503
533
 
504
- describe '--format={text|html}' do
534
+ describe '--format={text|html|rails}' do
505
535
 
506
536
  it "can enforce text format." do
507
537
  ctx_str = "{title: Love&Peace, items: [A, B, C]}"
@@ -521,6 +551,14 @@ END
521
551
  assert_equal "", serr
522
552
  end
523
553
 
554
+ it "can enforce rails format." do
555
+ sout, serr = with_erubyfile do |fname|
556
+ dummy_stdio { Main.main(['--format=rails', '-x', fname]) }
557
+ end
558
+ assert_equal _modify(SOURCE_RAILS), sout
559
+ assert_equal "", serr
560
+ end
561
+
524
562
  it "reports error when argument is missng." do
525
563
  status = nil
526
564
  sout, serr = with_erubyfile do |fname|
@@ -537,7 +575,7 @@ END
537
575
  dummy_stdio { status = Main.main(['-x', '--format=json', fname]) }
538
576
  end
539
577
  assert_equal "", sout
540
- assert_equal "#{File.basename($0)}: --format=json: 'text' or 'html' expected\n", serr
578
+ assert_equal "#{File.basename($0)}: --format=json: 'text', 'html' or 'rails' expected\n", serr
541
579
  assert_equal 1, status
542
580
  end
543
581
 
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  ###
4
- ### $Release: 1.0.0 $
4
+ ### $Release: 2.0.0 $
5
5
  ### $Copyright: copyright(c) 2014 kuwata-lab.com all rights reserved $
6
6
  ### $License: MIT License $
7
7
  ###
@@ -39,7 +39,7 @@ items:
39
39
  END
40
40
  expected = <<'END'
41
41
  _buf = ''; _buf << 'title: '; _buf << (@title).to_s; _buf << '
42
- '; _buf << 'items:
42
+ items:
43
43
  '; for item in @items;
44
44
  _buf << ' - '; _buf << (item).to_s; _buf << '
45
45
  '; end;
@@ -107,14 +107,14 @@ END
107
107
  expected = <<'END'
108
108
  _buf = '';
109
109
 
110
- _buf << '
111
- '; _buf << 'x = ';
110
+
111
+ _buf << 'x = ';
112
112
 
113
113
  _buf << '
114
114
  ';
115
115
 
116
- _buf << '
117
- '; _buf.to_s
116
+
117
+ _buf.to_s
118
118
  END
119
119
  expected = _modify(expected)
120
120
  assert_equal expected, template.parse(input)
@@ -184,6 +184,62 @@ END
184
184
  assert_equal expected, template.parse(input)
185
185
  end
186
186
 
187
+ it "concats spaces around embedded expressions." do
188
+ input = <<'END'
189
+ <form>
190
+ <%= f.text :email %>
191
+ <%= f.password :password %>
192
+ <%= f.submit 'Login' %>
193
+ </form>
194
+ END
195
+ expected = <<'END'
196
+ _buf = ''; _buf << '<form>
197
+ '; _buf << (f.text :email).to_s; _buf << '
198
+ '; _buf << (f.password :password).to_s; _buf << '
199
+ '; _buf << (f.submit 'Login').to_s; _buf << '
200
+ </form>
201
+ '; _buf.to_s
202
+ END
203
+ template = BabyErubis::Text.new(:freeze=>false)
204
+ assert_equal expected, template.parse(input)
205
+ end
206
+
207
+ it "can recognize block argument in embedded expression correctly." do
208
+ input = <<'END'
209
+ <%= form_for(:article) do |f| %>
210
+ ...
211
+ <% end %>
212
+ END
213
+ expected = <<'END'
214
+ _buf = ''; _buf << form_for(:article) do |f| _buf << '
215
+ ...
216
+ '; end;
217
+ _buf.to_s
218
+ END
219
+ template = BabyErubis::Text.new(:freeze=>false)
220
+ assert_equal expected, template.parse(input)
221
+ #
222
+ input = <<'END'
223
+ <%= form_for :article do %>
224
+ ...
225
+ <% end %>
226
+ END
227
+ expected = <<'END'
228
+ _buf = ''; _buf << form_for :article do _buf << '
229
+ ...
230
+ '; end;
231
+ _buf.to_s
232
+ END
233
+ template = BabyErubis::Text.new(:freeze=>false)
234
+ assert_equal expected, template.parse(input)
235
+ end
236
+
237
+ it "doesn't misunderstand <%= @todo %> as block" do
238
+ tmpl = BabyErubis::Text.new
239
+ src = tmpl.parse("<b><%= @todo %></b>")
240
+ assert_match /\ _buf << \(\@todo\)\.to_s;/, src
241
+ end
242
+
187
243
  end
188
244
 
189
245
 
metadata CHANGED
@@ -1,31 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baby_erubis
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - makoto kuwata
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-05-17 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: ! 'BabyErubis is an yet another eRuby implementation, based on Erubis.
15
-
13
+ description: |
14
+ BabyErubis is an yet another eRuby implementation, based on Erubis.
16
15
 
17
16
  * Small and fast
18
-
19
17
  * Supports HTML as well as plain text
20
-
21
18
  * Accepts both template file and template string
22
-
23
19
  * Easy to customize
24
20
 
25
-
26
21
  BabyErubis support Ruby 1.9 or higher, and will work on 1.8 very well.
27
-
28
- '
29
22
  email: kwa(at)kuwata-lab.com
30
23
  executables:
31
24
  - baby_erubis
@@ -33,7 +26,9 @@ extensions: []
33
26
  extra_rdoc_files: []
34
27
  files:
35
28
  - lib/baby_erubis.rb
29
+ - lib/baby_erubis/rails.rb
36
30
  - test/context_test.rb
31
+ - test/rails_test.rb
37
32
  - test/run_all.rb
38
33
  - test/script_test.rb
39
34
  - test/template_test.rb
@@ -44,28 +39,28 @@ files:
44
39
  - baby_erubis.gemspec
45
40
  - Rakefile
46
41
  homepage: https://github.com/kwatch/BabyErubis/tree/ruby
47
- licenses: []
42
+ licenses:
43
+ - MIT License
44
+ metadata: {}
48
45
  post_install_message:
49
46
  rdoc_options: []
50
47
  require_paths:
51
48
  - lib
52
49
  required_ruby_version: !ruby/object:Gem::Requirement
53
- none: false
54
50
  requirements:
55
- - - ! '>='
51
+ - - '>='
56
52
  - !ruby/object:Gem::Version
57
53
  version: '0'
58
54
  required_rubygems_version: !ruby/object:Gem::Requirement
59
- none: false
60
55
  requirements:
61
- - - ! '>='
56
+ - - '>='
62
57
  - !ruby/object:Gem::Version
63
58
  version: '0'
64
59
  requirements: []
65
60
  rubyforge_project:
66
- rubygems_version: 1.8.25
61
+ rubygems_version: 2.0.14
67
62
  signing_key:
68
- specification_version: 3
63
+ specification_version: 4
69
64
  summary: yet another eRuby implementation based on Erubis
70
65
  test_files:
71
66
  - test/run_all.rb