underscore-template 0.0.1

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/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,50 @@
1
+ Underscore templates compiler for Ruby
2
+ ====================================================
3
+
4
+ Imagine, you've got a rails project using
5
+ [Backbone.js](http://documentcloud.github.com/backbone) and just want to use
6
+ the [Underscore.js](http://documentcloud.github.com/underscore/)'s
7
+ [`_.template` function](http://documentcloud.github.com/underscore/#template)
8
+ without having an anonymous function like
9
+ [ejs](http://github.com/sstephenson/ruby-ejs) does.
10
+ `underscore-template` allows you to use
11
+ [`sprockets`](http://github.com/sstephenson/sprockets)'s'
12
+ [`JST`](https://github.com/sstephenson/sprockets#javascript-templating-with-ejs-and-eco)
13
+ templating directly with `_.template` function
14
+
15
+ Just add this to your `Gemfile` :
16
+
17
+ gem 'underscore-template'
18
+
19
+ <!-- templates/hello.jst._ -->
20
+ <div>Hello, <span><%= name %></span>!</div>
21
+
22
+ // application.js
23
+ //= require templates/hello
24
+ $("#hello").html(JST["templates/hello"]({ name: "Sam" }));
25
+
26
+ Invoke the function in a JavaScript environment to produce a string
27
+ value. You can pass an optional object specifying local variables for
28
+ template evaluation.
29
+
30
+ The underscore template tag syntax is as follows:
31
+
32
+ * `<% ... %>` silently evaluates the statement inside the tags.
33
+ * `<%= ... %>` evaluates the expression inside the tags and inserts
34
+ its string value into the template output.
35
+ * `<%- ... %>` behaves like `<%= ... %>` but HTML-escapes its output.
36
+
37
+ If you have the [ExecJS](https://github.com/sstephenson/execjs/)
38
+ library and a suitable JavaScript runtime installed, you can pass a
39
+ template and an optional hash of local variables to `UnderscoreTemplate.evaluate`:
40
+
41
+ Underscore::Template.evaluate("Hello <%= name %>", :name => "world")
42
+ # => "Hello world"
43
+
44
+ -----
45
+
46
+ &copy; 2012 Jean-Sébastien Ney
47
+
48
+ (actually most of credits goes to [@sstephenson](http://github.com/sstephenson))
49
+
50
+ Released under the MIT license
@@ -0,0 +1,4 @@
1
+ module Underscore; end
2
+
3
+ require 'underscore-template/engine'
4
+ require 'underscore-template/sprockets'
@@ -0,0 +1,70 @@
1
+ # UnderscoreTemplate (Embedded JavaScript) template compiler for Ruby
2
+ # This is a port of Underscore.js' `_.template` function:
3
+ # http://documentcloud.github.com/underscore/
4
+
5
+ module Underscore::Engine
6
+ JS_UNESCAPES = {
7
+ '\\' => '\\',
8
+ "'" => "'",
9
+ "\"" => "\"",
10
+ 'r' => "\r",
11
+ 'n' => "\n",
12
+ 't' => "\t",
13
+ 'u2028' => "\u2028",
14
+ 'u2029' => "\u2029"
15
+ }
16
+ JS_ESCAPES = JS_UNESCAPES.invert
17
+ JS_UNESCAPE_PATTERN = /\\(#{Regexp.union(JS_UNESCAPES.keys)})/
18
+ JS_ESCAPE_PATTERN = Regexp.union(JS_ESCAPES.keys)
19
+ EVALUATION_PATTERN = /<%([\s\S]+?)%>/
20
+ INTERPOLATION_PATTERN = /<%=([\s\S]+?)%>/
21
+ ESCAPE_PATTERN = /<%-([\s\S]+?)%>/
22
+
23
+ class << self
24
+ attr_accessor :underscore_available
25
+
26
+ # Compiles an UnderscoreTemplate template to a JavaScript function. The compiled
27
+ # function takes an optional argument, an object specifying local
28
+ # variables in the template.
29
+ #
30
+ # UnderscoreTemplate.compile("Hello <%= name %>")
31
+ # # => "_.template('Hello <%= name %>')"
32
+ #
33
+ def compile(source, options = {})
34
+ source = source.dup
35
+ js_escape!(source)
36
+ "_.template(\"#{source}\")"
37
+ end
38
+
39
+ # Evaluates an UnderscoreTemplate template with the given local variables and
40
+ # compiler options. You will need the ExecJS
41
+ # (https://github.com/sstephenson/execjs/) library and a
42
+ # JavaScript runtime available.
43
+ #
44
+ # UnderscoreTemplate.evaluate("Hello <%= name %>", :name => "world")
45
+ # # => "Hello world"
46
+ #
47
+ def evaluate(template, locals = {}, options = {})
48
+ underscore_context.call(compile(template, options), locals)
49
+ end
50
+
51
+ protected
52
+ def js_escape!(source)
53
+ source.gsub!(JS_ESCAPE_PATTERN) { |match| '\\' + JS_ESCAPES[match] }
54
+ source
55
+ end
56
+
57
+ def js_unescape!(source)
58
+ source.gsub!(JS_UNESCAPE_PATTERN) { |match| JS_UNESCAPES[match[1..-1]] }
59
+ source
60
+ end
61
+
62
+ def underscore_context
63
+ @@underscore_context ||= begin
64
+ require 'execjs'
65
+ ExecJS.compile(File.read(File.join(File.dirname(__FILE__), '..', '..', 'ext', 'underscore.js')))
66
+ end
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,9 @@
1
+ require 'sprockets'
2
+
3
+ module Underscore
4
+ # Processing
5
+ autoload :Template, "underscore-template/template"
6
+
7
+ # JST engines
8
+ ::Sprockets.register_engine '._', Template
9
+ end
@@ -0,0 +1,33 @@
1
+ require 'sprockets'
2
+ require 'tilt'
3
+
4
+ module Underscore
5
+ # Tilt engine class for the Underscore compiler.
6
+ class Template < Tilt::Template
7
+ # Check to see if Underscore is loaded
8
+ def self.engine_initialized?
9
+ defined? ::Underscore::Engine
10
+ end
11
+
12
+ # Autoload underscore-template library. If the library isn't loaded, Tilt will produce
13
+ # a thread safetly warning. If you intend to use `._` files, you
14
+ # should explicitly require it.
15
+ def initialize_engine
16
+ require_template_library 'underscore-template'
17
+ end
18
+
19
+ def prepare
20
+ end
21
+
22
+ # Compile template data with Underscore compiler.
23
+ #
24
+ # Returns a JS function definition String. The result should be
25
+ # assigned to a JS variable.
26
+ #
27
+ # # => "_.template(...)"
28
+ #
29
+ def evaluate(scope, locals, &block)
30
+ Underscore::Engine.compile(data)
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: underscore-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jean-Sébastien Ney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sprockets
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.1.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: execjs
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.4'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.4'
46
+ description: Compile and evaluate underscore templates from Ruby.
47
+ email:
48
+ - jeansebastien.ney@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - LICENSE
55
+ - lib/underscore-template/engine.rb
56
+ - lib/underscore-template/sprockets.rb
57
+ - lib/underscore-template/template.rb
58
+ - lib/underscore-template.rb
59
+ homepage: https://github.com/jney/ruby-underscore-template/
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.24
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: UnderscoreTemplate template compiler
83
+ test_files: []