ember 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,31 @@
1
1
  %#--
2
- %# Copyright 2009 Suraj N. Kurapati
3
- %# See the LICENSE file for details.
2
+ %# Copyright protects this work.
3
+ %# See LICENSE file for details.
4
4
  %#++
5
5
 
6
6
  % api_url = './api/index.html'
7
- % repo_url = 'http://github.com/sunaku/' + $program
8
- % repo_scm = '[Git](http://git-scm.com)'
7
+ % src_url = 'http://github.com/sunaku/' + $program
8
+ % src_scm = '[Git](http://git-scm.com)'
9
+
9
10
 
10
11
  %|chapter "Introduction"
12
+
13
+
11
14
  %|project
12
- <%= $project %> is an [eRuby template](http://en.wikipedia.org/wiki/ERuby) processsor that facilitates debugging, reduces markup, and improves composability of eRuby templates.
15
+
16
+ <%= $project %> is an [eRuby template](http://en.wikipedia.org/wiki/ERuby) processsor that allows debugging, reduces markup, and improves composability of eRuby templates.
17
+
18
+
19
+ * <%= xref "History", "What's new?" %> --- history of project releases.
20
+ * [Source code](<%= src_url %>) --- obtain via <%= src_scm %> or browse online.
21
+ * [API reference](<%= api_url %>) --- documentation for source code.
22
+ * [Project home](<%= $website %>) --- the <%= $project %> project home page.
23
+
24
+ To get help or provide feedback, simply
25
+ <%= xref "License", "contact the author(s)" %>.
26
+
27
+
28
+ %|section "Features"
13
29
 
14
30
  <%= $project %> is exciting because:
15
31
  * It reports correct line numbers in stack traces.
@@ -18,62 +34,20 @@
18
34
  * It completely silences code-only eRuby directives.
19
35
  * It is implemented in <%= `sloccount lib`[/^\d+/] %> lines of pure Ruby.
20
36
 
21
- These features distinguish <%= $project %> from the competition:
37
+
38
+ %|section "Etymology"
39
+
40
+ <%= $project %> stands for *embe*dded *r*uby.
41
+
42
+
43
+ %|section "License"
44
+
45
+ %< "../LICENSE"
46
+
47
+
48
+ %|section "Related works"
49
+
22
50
  * [Erubis](http://www.kuwata-lab.com/erubis/)
23
51
  * [eruby](http://modruby.net/en/)
24
52
  * [ERB](http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/)
25
53
 
26
- %|paragraph "Etymology"
27
- <%= $project %> stands for *embe*dded *r*uby.
28
-
29
- %|section "Logistics"
30
- * <%= xref "History", "Release notes" %> --- history of project releases.
31
- * [Source code](<%= repo_url %>) --- obtain via <%= repo_scm %> or browse online.
32
- * [API reference](<%= api_url %>) --- documentation for source code.
33
- * [Project home](<%= $website %>) --- the <%= $project %> project home page.
34
-
35
- To get help or provide feedback, simply
36
- <%= xref "License", "contact the author(s)" %>.
37
-
38
- %|paragraph "Version numbers"
39
- <%= $project %> releases are numbered in *major.minor.patch*
40
- form according to the [RubyGems rational versioning
41
- policy](http://www.rubygems.org/read/chapter/7), which
42
- can be summarized thus:
43
-
44
- <table markdown="1">
45
- <thead>
46
- <tr>
47
- <td rowspan="2">What increased in the version number?</td>
48
- <td colspan="3">The increase indicates that the release:</td>
49
- </tr>
50
- <tr>
51
- <th>Is backward compatible?</th>
52
- <th>Has new features?</th>
53
- <th>Has bug fixes?</th>
54
- </tr>
55
- </thead>
56
- <tbody>
57
- <tr>
58
- <th>major</th>
59
- <td style="background-color: #FFE4E1;">No</td>
60
- <td>Yes</td>
61
- <td>Yes</td>
62
- </tr>
63
- <tr>
64
- <th>minor</th>
65
- <td>Yes</td>
66
- <td>Yes</td>
67
- <td>Yes</td>
68
- </tr>
69
- <tr>
70
- <th>patch</th>
71
- <td>Yes</td>
72
- <td style="background-color: #FFE4E1;">No</td>
73
- <td>Yes</td>
74
- </tr>
75
- </tbody>
76
- </table>
77
-
78
- %|paragraph "License"
79
- %< "../LICENSE"
@@ -1,10 +1,14 @@
1
1
  %#--
2
- %# Copyright 2009 Suraj N. Kurapati
3
- %# See the LICENSE file for details.
2
+ %# Copyright protects this work.
3
+ %# See LICENSE file for details.
4
4
  %#++
5
5
 
6
+
6
7
  %|chapter "Setup"
8
+
9
+
7
10
  %|section "Requirements"
11
+
8
12
  Your system needs the following software to run <%= $project %>.
9
13
 
10
14
  | Software | Description | Notes |
@@ -12,7 +16,9 @@
12
16
  | [Ruby](http://ruby-lang.org) | Ruby language interpreter | Version 1.8.6, 1.8.7, and 1.9.1 have been tested successfully. |
13
17
  | [RubyGems](http://rubygems.org) | Ruby packaging system | Version 1.3.1 or newer is required. |
14
18
 
19
+
15
20
  %|section "Installation"
21
+
16
22
  You can install <%= $project %> by running this command:
17
23
 
18
24
  gem install <%= $program %>
@@ -27,3 +33,46 @@
27
33
 
28
34
  If you do not see such output, you may
29
35
  <%= xref "License", "ask the author(s)" %> for help.
36
+
37
+
38
+ %|section "Version numbers"
39
+
40
+ <%= $project %> releases are numbered in *major.minor.patch*
41
+ form according to the [RubyGems rational versioning
42
+ policy](http://www.rubygems.org/read/chapter/7), which
43
+ can be summarized thus:
44
+
45
+ <table markdown="1">
46
+ <thead>
47
+ <tr>
48
+ <td rowspan="2">What increased in the version number?</td>
49
+ <td colspan="3">The increase indicates that the release:</td>
50
+ </tr>
51
+ <tr>
52
+ <th>Is backward compatible?</th>
53
+ <th>Has new features?</th>
54
+ <th>Has bug fixes?</th>
55
+ </tr>
56
+ </thead>
57
+ <tbody>
58
+ <tr>
59
+ <th>major</th>
60
+ <td style="background-color: #FFE4E1;">No</td>
61
+ <td>Yes</td>
62
+ <td>Yes</td>
63
+ </tr>
64
+ <tr>
65
+ <th>minor</th>
66
+ <td>Yes</td>
67
+ <td>Yes</td>
68
+ <td>Yes</td>
69
+ </tr>
70
+ <tr>
71
+ <th>patch</th>
72
+ <td>Yes</td>
73
+ <td style="background-color: #FFE4E1;">No</td>
74
+ <td>Yes</td>
75
+ </tr>
76
+ </tbody>
77
+ </table>
78
+
@@ -1,10 +1,14 @@
1
1
  %#--
2
- %# Copyright 2009 Suraj N. Kurapati
3
- %# See the LICENSE file for details.
2
+ %# Copyright protects this work.
3
+ %# See LICENSE file for details.
4
4
  %#++
5
5
 
6
+
6
7
  %|chapter "Usage"
8
+
9
+
7
10
  %|section "Command-line interface"
11
+
8
12
  When you run this command:
9
13
 
10
14
  <%= $program %> --help
@@ -13,7 +17,9 @@
13
17
 
14
18
  <pre><%= verbatim `ruby bin/#{$program} --help` %></pre>
15
19
 
20
+
16
21
  %|section "Ruby library interface"
22
+
17
23
  Begin by loading <%= $project %> into Ruby:
18
24
 
19
25
  <code>
@@ -44,7 +50,9 @@
44
50
 
45
51
  See the [API documentation](<%= api_url %>) for details and examples.
46
52
 
53
+
47
54
  %|section "eRuby template directives", "Directives"
55
+
48
56
  eRuby templates are plain-text documents that contain special processing instructions known as **directives**. Directives may be expressed using either **standard** or **shorthand** notation:
49
57
 
50
58
  | Notation | Directive | Head | Operation | Body | Tail |
@@ -58,7 +66,9 @@
58
66
 
59
67
  In any case, directives are atomic constructs; they may not be nested.
60
68
 
69
+
61
70
  %|section "Operations"
71
+
62
72
  The first character that follows the head of a directive is known as an **operation**. Operations specify how the directive should be processed:
63
73
 
64
74
  | Operation | Effect | Example |
@@ -105,12 +115,16 @@
105
115
  end
106
116
  %>
107
117
 
118
+
108
119
  %|example "An empty template"
120
+
109
121
  Begin with an empty template:
110
122
 
111
- <%= template_example.call "", {} %>
123
+ %= template_example.call "", {}
124
+
112
125
 
113
126
  %|example "Comment directives"
127
+
114
128
  Add comment directives:
115
129
 
116
130
  <%=
@@ -122,7 +136,9 @@
122
136
  }, :shorthand => true
123
137
  %>
124
138
 
139
+
125
140
  %|example "Escaped directives"
141
+
126
142
  Add escaped directives:
127
143
 
128
144
  <%=
@@ -134,7 +150,9 @@
134
150
  }, :shorthand => true
135
151
  %>
136
152
 
153
+
137
154
  %|example "Vocal directives"
155
+
138
156
  Add vocal directives, which produce output:
139
157
 
140
158
  <%=
@@ -145,7 +163,9 @@
145
163
  }, :shorthand => true
146
164
  %>
147
165
 
166
+
148
167
  %|example "Silent directives"
168
+
149
169
  Add silent directives, which do not produce output:
150
170
 
151
171
  <%=
@@ -159,7 +179,9 @@
159
179
  }, :shorthand => true
160
180
  %>
161
181
 
182
+
162
183
  %|example "Block directives"
184
+
163
185
  Add some Ruby blocks:
164
186
 
165
187
  <%=
@@ -181,7 +203,9 @@
181
203
  }, :shorthand => true
182
204
  %>
183
205
 
206
+
184
207
  %|example "Infer block endings"
208
+
185
209
  Omit <tt><%= standard_directive[' end'] %></tt> directives from the template:
186
210
 
187
211
  <%=
@@ -200,7 +224,9 @@
200
224
  }, :shorthand => true, :infer_end => true
201
225
  %>
202
226
 
227
+
203
228
  %|example "Raw file inclusion"
229
+
204
230
  When <tt>doc/example.txt</tt> contains:
205
231
 
206
232
  <pre><%< "example.txt" %></pre>
@@ -218,7 +244,9 @@
218
244
  }, :shorthand => true, :source_file => __FILE__
219
245
  %>
220
246
 
247
+
221
248
  %|example "Template file inclusion"
249
+
222
250
  When <tt>doc/example.erb</tt> contains:
223
251
 
224
252
  <code lang="rhtml"><%< "example.erb" %></code>
@@ -236,7 +264,9 @@
236
264
  }, :shorthand => true, :source_file => __FILE__
237
265
  %>
238
266
 
267
+
239
268
  %|example "Dynamic template evaluation"
269
+
240
270
  <%=
241
271
  example = %{~ "#{shorthand_directive['= 2 + 2']}"}
242
272
 
@@ -247,3 +277,4 @@
247
277
 
248
278
  }, :shorthand => true
249
279
  %>
280
+
@@ -1,6 +1,6 @@
1
1
  #--
2
- # Copyright 2009 Suraj N. Kurapati
3
- # See the LICENSE file for details.
2
+ # Copyright protects this work.
3
+ # See LICENSE file for details.
4
4
  #++
5
5
 
6
6
  require 'rubygems'
@@ -8,9 +8,9 @@ gem 'inochi', '~> 1'
8
8
  require 'inochi'
9
9
 
10
10
  Inochi.init :Ember,
11
- :version => '0.0.0',
12
- :release => '2009-05-02',
13
- :website => 'http://snk.tuxfamily.org/lib/ember',
11
+ :version => '0.0.1',
12
+ :release => '2009-10-03',
13
+ :website => 'http://snk.tuxfamily.org/lib/ember/',
14
14
  :tagline => 'eRuby template processor'
15
15
 
16
16
  require 'ember/template'
@@ -1,6 +1,6 @@
1
1
  #--
2
- # Copyright 2009 Suraj N. Kurapati
3
- # See the LICENSE file for details.
2
+ # Copyright protects this work.
3
+ # See LICENSE file for details.
4
4
  #++
5
5
 
6
6
  require 'pathname'
@@ -11,9 +11,10 @@ module Ember
11
11
  # Builds a processor that evaluates eRuby directives
12
12
  # in the given input according to the given options.
13
13
  #
14
- # This processor transforms the given input into an
15
- # executable Ruby program (provided by the #to_s() method)
16
- # which is then executed by the #render() method on demand.
14
+ # This processor transforms the given input
15
+ # into an executable Ruby program (provided
16
+ # by the #program() method) which is then
17
+ # executed by the #render() method on demand.
17
18
  #
18
19
  # eRuby directives that contribute to the output of
19
20
  # the given template are called "vocal" directives.
@@ -62,6 +63,7 @@ module Ember
62
63
  #
63
64
  def initialize input, options = {}
64
65
  @options = options
66
+ @render_context_id = object_id
65
67
  @compile = compile(input.to_s)
66
68
  end
67
69
 
@@ -73,14 +75,22 @@ module Ember
73
75
  @compile
74
76
  end
75
77
 
78
+ @@contexts = {}
79
+
76
80
  ##
77
81
  # Returns the result of executing the Ruby program for this template
78
- # (provided by the #to_s() method) inside the given context binding.
82
+ # (provided by the #program() method) inside the given context binding.
79
83
  #
80
- def render(context = TOPLEVEL_BINDING)
81
- eval @compile, context,
84
+ def render context = TOPLEVEL_BINDING, parent_context_id = nil
85
+ context ||= @@contexts[parent_context_id] # inherit parent context
86
+ @@contexts[@render_context_id] = context # provide to children
87
+
88
+ result = eval @compile, context,
82
89
  (@options[:source_file] || :SOURCE).to_s,
83
90
  (@options[:source_line] || 1).to_i
91
+
92
+ @@contexts.delete @render_context_id # free the memory
93
+ result
84
94
  end
85
95
 
86
96
  class << self
@@ -323,7 +333,7 @@ module Ember
323
333
 
324
334
  # content + after_spacing
325
335
  content_line.gsub! '<%%', '<%' # unescape escaped directives
326
- @program.text content_line
336
+ @program.emit_text content_line
327
337
 
328
338
  # after_newline
329
339
  @program.new_line if content_line =~ /\n\z/
@@ -359,28 +369,28 @@ module Ember
359
369
  arguments =~ BLOCK_END_REGEXP ||
360
370
  arguments =~ BLOCK_CONTINUE_REGEXP
361
371
 
362
- @program.text unindent(before_spacing) if is_vocal
372
+ @program.emit_text unindent(before_spacing) if is_vocal
363
373
 
364
374
  # directive
365
375
  template_class_name = '::Ember::Template'
366
376
  nested_template_args = "(#{arguments}), #{@options.inspect}"
367
377
 
368
378
  nest_template_with = lambda do |meth|
369
- @program.code "#{template_class_name}.#{meth}(#{
379
+ @program.emit_code "#{template_class_name}.#{meth}(#{
370
380
  nested_template_args
371
- }.merge!(:continue_result => true)).render(binding)"
381
+ }.merge!(:continue_result => true)).render(nil, #{@render_context_id.inspect})"
372
382
  end
373
383
 
374
384
  case operation
375
385
  when OPERATION_EVAL_EXPRESSION
376
- @program.expr arguments
386
+ @program.emit_expr arguments
377
387
 
378
388
  when OPERATION_COMMENT_LINE
379
- @program.code directive.gsub(/\S/, ' ')
389
+ @program.emit_code directive.gsub(/\S/, ' ')
380
390
 
381
391
  when OPERATION_BEGIN_LAMBDA
382
392
  arguments =~ /(\bdo\b)?\s*(\|[^\|]*\|)?\s*\z/
383
- @program.code "#{$`} #{$1 || 'do'} #{$2}"
393
+ @program.emit_code "#{$`} #{$1 || 'do'} #{$2}"
384
394
 
385
395
  p :begin => directive if $DEBUG
386
396
  open_block.call
@@ -392,10 +402,10 @@ module Ember
392
402
  nest_template_with[:load_file]
393
403
 
394
404
  when OPERATION_INSERT_PLAIN_FILE
395
- @program.expr "#{template_class_name}.read_file(#{nested_template_args})"
405
+ @program.emit_expr "#{template_class_name}.read_file(#{nested_template_args})"
396
406
 
397
407
  else
398
- @program.code arguments
408
+ @program.emit_code arguments
399
409
 
400
410
  unless arguments =~ /\n/ # don't bother parsing multi-line directives
401
411
  case arguments
@@ -417,14 +427,14 @@ module Ember
417
427
  end
418
428
 
419
429
  # after_spacing
420
- @program.text after_spacing if is_vocal || after_newline.empty?
430
+ @program.emit_text after_spacing if is_vocal || after_newline.empty?
421
431
 
422
432
  # after_newline
423
- @program.text after_newline if is_vocal
433
+ @program.emit_text after_newline if is_vocal
424
434
  @program.new_line unless after_newline.empty?
425
435
  end
426
436
 
427
- class Program
437
+ class Program #:nodoc:
428
438
  ##
429
439
  # Transforms this program into Ruby code which uses
430
440
  # the given variable name as the evaluation buffer.
@@ -465,7 +475,7 @@ module Ember
465
475
  # Schedules the given text to be inserted verbatim
466
476
  # into the evaluation buffer when this program is run.
467
477
  #
468
- def text value
478
+ def emit_text value
469
479
  # don't bother emitting empty strings
470
480
  return if value.empty?
471
481
 
@@ -481,7 +491,7 @@ module Ember
481
491
  # Schedules the given Ruby code to be
482
492
  # evaluated when this program is run.
483
493
  #
484
- def code value
494
+ def emit_code value
485
495
  statement :code, value
486
496
  end
487
497
 
@@ -489,7 +499,7 @@ module Ember
489
499
  # Schedules the given Ruby code to be evaluated and inserted
490
500
  # into the evaluation buffer when this program is run.
491
501
  #
492
- def expr value
502
+ def emit_expr value
493
503
  statement :expr, value
494
504
  end
495
505