ejs 1.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.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +32 -0
  3. data/lib/ejs.rb +75 -0
  4. metadata +84 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Sam Stephenson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ EJS (Embedded JavaScript) template compiler for Ruby
2
+ ====================================================
3
+
4
+ EJS templates embed JavaScript code inside `<% ... %>` tags, much like
5
+ ERB. This library is a port of
6
+ [Underscore.js](http://documentcloud.github.com/underscore/)'s
7
+ [`_.template`
8
+ function](http://documentcloud.github.com/underscore/#template) to
9
+ Ruby.
10
+
11
+ Pass an EJS template to `EJS.compile` to generate a JavaScript
12
+ function:
13
+
14
+ EJS.compile("Hello <%= name %>")
15
+ # => "function(obj){...}"
16
+
17
+ Invoke the function in a JavaScript environment to produce a string
18
+ value. You can pass an optional object specifying local variables for
19
+ template evaluation.
20
+
21
+ If you have the [ExecJS](https://github.com/sstephenson/execjs/)
22
+ library and a suitable JavaScript runtime installed, you can pass a
23
+ template and an optional hash of local variables to `EJS.evaluate`:
24
+
25
+ EJS.evaluate("Hello <%= name %>", :name => "world")
26
+ # => "Hello world"
27
+
28
+ -----
29
+
30
+ &copy; 2011 Sam Stephenson
31
+
32
+ Released under the MIT license
@@ -0,0 +1,75 @@
1
+ # EJS (Embedded JavaScript) template compiler for Ruby
2
+ # (c) 2011 Sam Stephenson
3
+ #
4
+ # This is a port of Underscore.js' `_.template` function:
5
+ # http://documentcloud.github.com/underscore/
6
+
7
+ module EJS
8
+ class << self
9
+ attr_accessor :evaluation_pattern
10
+ attr_accessor :interpolation_pattern
11
+
12
+ # Compiles an EJS template to a JavaScript function. The compiled
13
+ # function takes an optional argument, an object specifying local
14
+ # variables in the template. You can optionally pass the
15
+ # `:evaluation_pattern` and `:interpolation_pattern` options to
16
+ # `compile` if you want to specify a different tag syntax for the
17
+ # template.
18
+ #
19
+ # EJS.compile("Hello <%= name %>")
20
+ # # => "function(obj){...}"
21
+ #
22
+ def compile(source, options = {})
23
+ source = source.dup
24
+
25
+ escape_quotes!(source)
26
+ replace_interpolation_tags!(source, options)
27
+ replace_evaluation_tags!(source, options)
28
+ escape_whitespace!(source)
29
+
30
+ "function(obj){var __p=[],print=function(){__p.push.apply(__p,arguments);};" +
31
+ "with(obj||{}){__p.push('#{source}');}return __p.join('');}"
32
+ end
33
+
34
+ # Evaluates an EJS template with the given local variables and
35
+ # compiler options. You will need the ExecJS
36
+ # (https://github.com/sstephenson/execjs/) library and a
37
+ # JavaScript runtime available.
38
+ #
39
+ # EJS.evaluate("Hello <%= name %>", :name => "world")
40
+ # # => "Hello world"
41
+ #
42
+ def evaluate(template, locals = {}, options = {})
43
+ require "execjs"
44
+ context = ExecJS.compile("var evaluate = #{compile(template, options)}")
45
+ context.call("evaluate", locals)
46
+ end
47
+
48
+ protected
49
+ def escape_quotes!(source)
50
+ source.gsub!(/\\/) { '\\\\' }
51
+ source.gsub!(/'/) { "\\'" }
52
+ end
53
+
54
+ def replace_evaluation_tags!(source, options)
55
+ source.gsub!(options[:evaluation_pattern] || evaluation_pattern) do
56
+ "');" + $1.gsub(/\\'/, "'").gsub(/[\r\n\t]/, ' ') + "__p.push('"
57
+ end
58
+ end
59
+
60
+ def replace_interpolation_tags!(source, options)
61
+ source.gsub!(options[:interpolation_pattern] || interpolation_pattern) do
62
+ "'," + $1.gsub(/\\'/, "'") + ",'"
63
+ end
64
+ end
65
+
66
+ def escape_whitespace!(source)
67
+ source.gsub!(/\r/, '\\r')
68
+ source.gsub!(/\n/, '\\n')
69
+ source.gsub!(/\t/, '\\t')
70
+ end
71
+ end
72
+
73
+ self.evaluation_pattern = /<%([\s\S]+?)%>/
74
+ self.interpolation_pattern = /<%=([\s\S]+?)%>/
75
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ejs
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Sam Stephenson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-17 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: execjs
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ - 4
33
+ version: "0.4"
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: Compile and evaluate EJS (Embedded JavaScript) templates from Ruby.
37
+ email:
38
+ - sstephenson@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README.md
47
+ - LICENSE
48
+ - lib/ejs.rb
49
+ has_rdoc: true
50
+ homepage: https://github.com/sstephenson/ruby-ejs/
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.5.0
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: EJS (Embedded JavaScript) template compiler
83
+ test_files: []
84
+