haml-underscore-template 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Christopher Rueber
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.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Haml Underscore Template Compiler
2
+
3
+ This project is an addendum to the Underscore Template Compiler built by Jean-Sebastien Ney. You can follow my fork
4
+ link back to his project if you are interested in the original code. This code will no longer be following that repository.
5
+
6
+ # Purpose
7
+
8
+ Say that you're using **BackboneJS**, **UnderscoreJS**, and **jQuery**. A lot of the time you end up putting templates
9
+ directly in to the HTML, and then go after them utilizing the jQuery `$('#template-for-things').html()` functionality,
10
+ rather than having them directly included within your asset pipeline. That is not an insignificant operation for the browser
11
+ to have to go running after, every time a page boots up. This functionality already exists in sprockets implemented as
12
+ EJS and the JST function from the browser side. Additionally, if you're using haml and have already built a lot
13
+ of your templates in that matter, it is a non-trivial task to convert those all over to HTML. And really, why would you want to?
14
+ HAML is a lot easier to write than HTML, and it's a lot easier to reason about. Thus, this project.
15
+
16
+ [Backbone.js](http://documentcloud.github.com/backbone) |
17
+ [Underscore.js](http://documentcloud.github.com/underscore/) |
18
+ [ejs](http://github.com/sstephenson/ruby-ejs) |
19
+ [sprockets](http://github.com/sstephenson/sprockets) |
20
+ [JST](https://github.com/sstephenson/sprockets#javascript-templating-with-ejs-and-eco) |
21
+
22
+ Just add this to your `Gemfile` :
23
+
24
+ gem 'underscore-template'
25
+
26
+ Example :
27
+
28
+ <!-- templates/hello.jst._ -->
29
+ .hello
30
+ Hello
31
+ %span <%= name %>!
32
+
33
+ // application.js
34
+ //= require templates/hello
35
+ $("#hello").html(JST["templates/hello"]({ name: "Sam" }));
36
+
37
+ Invoke the function in a JavaScript environment to produce a string
38
+ value. You can pass an optional object specifying local variables for
39
+ template evaluation.
40
+
41
+ The underscore template tag syntax is as follows:
42
+
43
+ * `<% ... %>` silently evaluates the statement inside the tags.
44
+ * `<%= ... %>` evaluates the expression inside the tags and inserts
45
+ its string value into the template output.
46
+ * `<%- ... %>` behaves like `<%= ... %>` but HTML-escapes its output.
47
+
48
+ If you have the [ExecJS](https://github.com/sstephenson/execjs/)
49
+ library and a suitable JavaScript runtime installed, you can pass a
50
+ template and an optional hash of local variables to `UnderscoreTemplate.evaluate`:
51
+
52
+ Underscore::Template.evaluate("Hello <%= name %>", :name => "world")
53
+ # => "Hello world"
54
+
55
+ -----
56
+
57
+ &copy; 2012 Christopher Rueber
58
+
59
+ (most code originally written by [Jean-Sebastien Ney](http://github.com/jney/ruby-underscore-template), and a lot of framework code by [@sstephenson](http://github.com/sstephenson))
60
+
61
+ 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,75 @@
1
+ require 'haml'
2
+ require 'execjs'
3
+
4
+ # UnderscoreTemplate (Embedded JavaScript) template compiler for Ruby
5
+ # This is a port of Underscore.js' `_.template` function:
6
+ # http://documentcloud.github.com/underscore/
7
+
8
+ module Underscore::Engine
9
+ JS_UNESCAPES = {
10
+ '\\' => '\\',
11
+ "'" => "'",
12
+ "\"" => "\"",
13
+ 'r' => "\r",
14
+ 'n' => "\n",
15
+ 't' => "\t",
16
+ 'u2028' => "\u2028",
17
+ 'u2029' => "\u2029"
18
+ }
19
+ JS_ESCAPES = JS_UNESCAPES.invert
20
+ JS_UNESCAPE_PATTERN = /\\(#{Regexp.union(JS_UNESCAPES.keys)})/
21
+ JS_ESCAPE_PATTERN = Regexp.union(JS_ESCAPES.keys)
22
+ EVALUATION_PATTERN = /<%([\s\S]+?)%>/
23
+ INTERPOLATION_PATTERN = /<%=([\s\S]+?)%>/
24
+ ESCAPE_PATTERN = /<%-([\s\S]+?)%>/
25
+
26
+ class << self
27
+ attr_accessor :underscore_available
28
+
29
+ # Compiles an UnderscoreTemplate template to a JavaScript function. The compiled
30
+ # function takes an optional argument, an object specifying local
31
+ # variables in the template.
32
+ #
33
+ # UnderscoreTemplate.compile("Hello <%= name %>")
34
+ # # => "_.template('Hello <%= name %>')"
35
+ #
36
+ def compile(source, options = {})
37
+ source = ::Haml::Engine.new(source.dup).render
38
+ js_escape!(source)
39
+ "_.template(\"#{source}\")"
40
+ end
41
+
42
+ # Evaluates an UnderscoreTemplate template with the given local variables and
43
+ # compiler options. You will need the ExecJS
44
+ # (https://github.com/sstephenson/execjs/) library and a
45
+ # JavaScript runtime available.
46
+ #
47
+ # UnderscoreTemplate.evaluate("Hello <%= name %>", :name => "world")
48
+ # # => "Hello world"
49
+ #
50
+ def evaluate(template, locals = {}, options = {})
51
+ template_to_compile = compile(template, options)
52
+
53
+ underscore_context.call(template_to_compile, locals)
54
+ end
55
+
56
+ protected
57
+ def js_escape!(source)
58
+ source.gsub!(JS_ESCAPE_PATTERN) { |match| '\\' + JS_ESCAPES[match] }
59
+ source
60
+ end
61
+
62
+ def js_unescape!(source)
63
+ source.gsub!(JS_UNESCAPE_PATTERN) { |match| JS_UNESCAPES[match[1..-1]] }
64
+ source
65
+ end
66
+
67
+ def underscore_context
68
+ @@underscore_context ||= begin
69
+ underscore = File.read(File.join(File.dirname(__FILE__), '..', '..', 'ext', 'underscore.js'))
70
+ ExecJS.compile(underscore)
71
+ end
72
+ end
73
+ end
74
+
75
+ 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,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haml-underscore-template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Rueber
9
+ - Jean-Sébastien Ney
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-05-30 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sprockets
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 2.1.3
31
+ - !ruby/object:Gem::Dependency
32
+ name: haml
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: execjs
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.4.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 1.4.0
63
+ - !ruby/object:Gem::Dependency
64
+ name: json
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ version: 1.7.7
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ~>
77
+ - !ruby/object:Gem::Version
78
+ version: 1.7.7
79
+ description: Compile and evaluate underscore templates from Ruby.
80
+ email:
81
+ - crueber@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - README.md
87
+ - LICENSE
88
+ - lib/underscore-template/engine.rb
89
+ - lib/underscore-template/sprockets.rb
90
+ - lib/underscore-template/template.rb
91
+ - lib/underscore-template.rb
92
+ homepage: https://github.com/crueber/haml-underscore-template/
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.25
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Haml+Underscore Template compiler
116
+ test_files: []