ejs 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +9 -2
  2. data/lib/ejs.rb +19 -3
  3. metadata +27 -52
data/README.md CHANGED
@@ -6,7 +6,7 @@ ERB. This library is a port of
6
6
  [Underscore.js](http://documentcloud.github.com/underscore/)'s
7
7
  [`_.template`
8
8
  function](http://documentcloud.github.com/underscore/#template) to
9
- Ruby.
9
+ Ruby, and strives to maintain the same syntax and semantics.
10
10
 
11
11
  Pass an EJS template to `EJS.compile` to generate a JavaScript
12
12
  function:
@@ -18,6 +18,13 @@ Invoke the function in a JavaScript environment to produce a string
18
18
  value. You can pass an optional object specifying local variables for
19
19
  template evaluation.
20
20
 
21
+ The EJS tag syntax is as follows:
22
+
23
+ * `<% ... %>` silently evaluates the statement inside the tags.
24
+ * `<%= ... %>` evaluates the expression inside the tags and inserts
25
+ its string value into the template output.
26
+ * `<%- ... %>` behaves like `<%= ... %>` but HTML-escapes its output.
27
+
21
28
  If you have the [ExecJS](https://github.com/sstephenson/execjs/)
22
29
  library and a suitable JavaScript runtime installed, you can pass a
23
30
  template and an optional hash of local variables to `EJS.evaluate`:
@@ -27,6 +34,6 @@ template and an optional hash of local variables to `EJS.evaluate`:
27
34
 
28
35
  -----
29
36
 
30
- &copy; 2011 Sam Stephenson
37
+ &copy; 2012 Sam Stephenson
31
38
 
32
39
  Released under the MIT license
data/lib/ejs.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  # EJS (Embedded JavaScript) template compiler for Ruby
2
- # (c) 2011 Sam Stephenson
3
- #
4
2
  # This is a port of Underscore.js' `_.template` function:
5
3
  # http://documentcloud.github.com/underscore/
6
4
 
@@ -8,6 +6,7 @@ module EJS
8
6
  class << self
9
7
  attr_accessor :evaluation_pattern
10
8
  attr_accessor :interpolation_pattern
9
+ attr_accessor :escape_pattern
11
10
 
12
11
  # Compiles an EJS template to a JavaScript function. The compiled
13
12
  # function takes an optional argument, an object specifying local
@@ -23,6 +22,7 @@ module EJS
23
22
  source = source.dup
24
23
 
25
24
  escape_quotes!(source)
25
+ replace_escape_tags!(source, options)
26
26
  replace_interpolation_tags!(source, options)
27
27
  replace_evaluation_tags!(source, options)
28
28
  escape_whitespace!(source)
@@ -51,9 +51,15 @@ module EJS
51
51
  source.gsub!(/'/) { "\\'" }
52
52
  end
53
53
 
54
+ def replace_escape_tags!(source, options)
55
+ source.gsub!(options[:escape_pattern] || escape_pattern) do
56
+ "',(''+" + $1.gsub(/\\'/, "'") + ")#{escape_function},'"
57
+ end
58
+ end
59
+
54
60
  def replace_evaluation_tags!(source, options)
55
61
  source.gsub!(options[:evaluation_pattern] || evaluation_pattern) do
56
- "');" + $1.gsub(/\\'/, "'").gsub(/[\r\n\t]/, ' ') + "__p.push('"
62
+ "');" + $1.gsub(/\\'/, "'").gsub(/[\r\n\t]/, ' ') + "; __p.push('"
57
63
  end
58
64
  end
59
65
 
@@ -68,8 +74,18 @@ module EJS
68
74
  source.gsub!(/\n/, '\\n')
69
75
  source.gsub!(/\t/, '\\t')
70
76
  end
77
+
78
+ def escape_function
79
+ ".replace(/&/g, '&amp;')" +
80
+ ".replace(/</g, '&lt;')" +
81
+ ".replace(/>/g, '&gt;')" +
82
+ ".replace(/\"/g, '&quot;')" +
83
+ ".replace(/'/g, '&#x27;')" +
84
+ ".replace(/\\//g,'&#x2F;')"
85
+ end
71
86
  end
72
87
 
73
88
  self.evaluation_pattern = /<%([\s\S]+?)%>/
74
89
  self.interpolation_pattern = /<%=([\s\S]+?)%>/
90
+ self.escape_pattern = /<%-([\s\S]+?)%>/
75
91
  end
metadata CHANGED
@@ -1,84 +1,59 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ejs
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Sam Stephenson
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-05-17 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-06-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: execjs
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70242739871960 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
18
+ requirements:
27
19
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- - 4
33
- version: "0.4"
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
34
22
  type: :development
35
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *70242739871960
36
25
  description: Compile and evaluate EJS (Embedded JavaScript) templates from Ruby.
37
- email:
26
+ email:
38
27
  - sstephenson@gmail.com
39
28
  executables: []
40
-
41
29
  extensions: []
42
-
43
30
  extra_rdoc_files: []
44
-
45
- files:
31
+ files:
46
32
  - README.md
47
33
  - LICENSE
48
34
  - lib/ejs.rb
49
- has_rdoc: true
50
35
  homepage: https://github.com/sstephenson/ruby-ejs/
51
36
  licenses: []
52
-
53
37
  post_install_message:
54
38
  rdoc_options: []
55
-
56
- require_paths:
39
+ require_paths:
57
40
  - lib
58
- required_ruby_version: !ruby/object:Gem::Requirement
41
+ required_ruby_version: !ruby/object:Gem::Requirement
59
42
  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
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
48
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
76
53
  requirements: []
77
-
78
54
  rubyforge_project:
79
- rubygems_version: 1.5.0
55
+ rubygems_version: 1.8.11
80
56
  signing_key:
81
57
  specification_version: 3
82
58
  summary: EJS (Embedded JavaScript) template compiler
83
59
  test_files: []
84
-