reco 0.1.1 → 0.1.2

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.
data/README.md CHANGED
@@ -6,3 +6,16 @@ Eco is a wonderful javascript template system by [Sam Stephenson](http://twitter
6
6
  Reco let you compile Eco templates into Javascript through Ruby like this:
7
7
 
8
8
  javascript = Reco.compile File.read('some_template')
9
+
10
+ With Rails 3.1 you can serve Eco templates i.e. like this:
11
+
12
+ // app/assets/javascripts/templates.js.erb
13
+ window.templates = {};
14
+ <% for template_name in [:user, :post] do %>
15
+ <% template_path = File.join Rails.root, 'app', 'assets', 'templates', "#{template_name}.eco" %>
16
+ <%= Reco.compile File.read(template_path), identifier: "window.templates.#{template_name}" %>
17
+ <% end %>
18
+
19
+ Javascript that are run after templates.js.erb will now be able to do this:
20
+
21
+ window.templates.user({ name: 'Rasmus' }); // returns the rendered HTML
@@ -7,15 +7,15 @@ module Reco
7
7
  end
8
8
 
9
9
  def self.compile(source, options = {})
10
- compiled_javascript = CoffeeScript.compile preprocess(source), noWrap: true
10
+ compiled_javascript = CoffeeScript.compile preprocess(source), :noWrap => true
11
11
  identifier = options[:identifier] || 'module.exports'
12
12
  identifier = "var #{identifier}" unless identifier.include? '.'
13
13
 
14
- wrapper % { compiled_javascript: compiled_javascript, identifier: identifier }
14
+ wrapper % [identifier, compiled_javascript]
15
15
  end
16
16
 
17
17
  def self.wrapper
18
- File.read File.join(File.dirname(__FILE__), 'wrapper.js')
18
+ @wrapper ||= File.read File.join(File.dirname(__FILE__), 'wrapper.js')
19
19
  end
20
20
 
21
21
  end
@@ -2,8 +2,9 @@ require 'strscan'
2
2
 
3
3
  class Reco::Scanner
4
4
  MODE_PATTERNS = {
5
- data: /(.*?)(<%%|<%(([=-])?)|\n|$)/,
6
- code: /(.*?)(((:|(->|=>))\s*)?%>|\n|$)/
5
+ :data => /(.*?)(<%%|<%\s*(\#)|<%(([=-])?)|\n|$)/,
6
+ :code => /(.*?)((((:|(->|=>))\s*))?%>|\n|$)/,
7
+ :comment => /(.*?)(%>|\n|$)/
7
8
  }
8
9
  DEDENTABLE_PATTERN = /^(end|when|else|catch|finally)(?:\W|$)/
9
10
 
@@ -30,7 +31,15 @@ class Reco::Scanner
30
31
  callback.call @mode == :data ? ["print_string", flush] : ["fail", "unexpected end of template"]
31
32
  else
32
33
  advance
33
- @mode == :data ? scan_data(callback) : scan_code(callback)
34
+
35
+ case @mode
36
+ when :data
37
+ scan_data callback
38
+ when :code
39
+ scan_code callback
40
+ when :comment
41
+ scan_comment callback
42
+ end
34
43
  end
35
44
  end
36
45
 
@@ -38,8 +47,9 @@ class Reco::Scanner
38
47
  @scanner.scan_until MODE_PATTERNS[@mode]
39
48
  @buffer += @scanner[1]
40
49
  @tail = @scanner[2]
41
- @directive = @scanner[4]
42
- @arrow = @scanner[5]
50
+ @comment = @scanner[3]
51
+ @directive = @scanner[5]
52
+ @arrow = @scanner[6]
43
53
  end
44
54
 
45
55
  def scan_data(callback)
@@ -50,9 +60,14 @@ class Reco::Scanner
50
60
  @buffer += @tail
51
61
  scan callback
52
62
  elsif @tail
53
- @mode = :code
54
63
  callback.call ["print_string", flush]
55
- callback.call ["begin_code", print: !!@directive, safe: @directive == '-']
64
+
65
+ if @comment
66
+ @mode = :comment
67
+ else
68
+ @mode = :code
69
+ callback.call ["begin_code", {:print => !!@directive, :safe => @directive == '-'}]
70
+ end
56
71
  end
57
72
  end
58
73
 
@@ -70,6 +85,15 @@ class Reco::Scanner
70
85
  end
71
86
  end
72
87
 
88
+ def scan_comment(callback)
89
+ if @tail == "\n"
90
+ callback.call ['fail', 'unexpected newline in code block']
91
+ elsif @tail
92
+ @mode = :data
93
+ @buffer = ''
94
+ end
95
+ end
96
+
73
97
  def is_dedentable?(code)
74
98
  code.match DEDENTABLE_PATTERN
75
99
  end
@@ -1,4 +1,4 @@
1
- %{identifier} = function(__obj) {
1
+ %s = function(__obj) {
2
2
  if (!__obj) __obj = {};
3
3
  var __out = [], __capture = function(callback) {
4
4
  var out = __out, result;
@@ -36,7 +36,7 @@
36
36
  };
37
37
  }
38
38
  (function() {
39
- %{compiled_javascript}
39
+ %s
40
40
  }).call(__obj);
41
41
  __obj.safe = __objSafe, __obj.escape = __escape;
42
42
  return __out.join('');
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: reco
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Rasmus R\xC3\xB8nn Nielsen"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-24 00:00:00 +02:00
13
+ date: 2011-06-05 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -24,6 +24,17 @@ dependencies:
24
24
  version: "2.0"
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
27
38
  description: Reco let you compile Eco templates into Javascript through Ruby.
28
39
  email: rasmusrnielsen@gmail.com
29
40
  executables: []