dotjs_sprockets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea75be79e483ce16dbf8f103172dafd2d80d9809
4
+ data.tar.gz: 4b0bfea0a2fc5c6a80d1ff5eb5e1241057c5e6cf
5
+ SHA512:
6
+ metadata.gz: 13d8f3f92bd3d73bab36f423749246d24ef1024a601c3a5125125c574f10074cfbe1dd72819624c011ddddedd7a9c0ec68d92ca927ec43b782f4f196212d52f9
7
+ data.tar.gz: 699412f42c05219bd043668a2b3ead4697a9b86d3fec6eb65286920f45c69bd4cf97267b42dbb1ad1463b372e20d149c973e43be2e90e7c19a46f22ffdd9beda
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dotjs_sprockets.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Roberto Dip
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # DotjsSprockets
2
+
3
+ [![Code Climate](https://codeclimate.com/github/roperzh/dotjs_sprockets.png)](https://codeclimate.com/github/roperzh/dotjs_sprockets)
4
+ [![Inline docs](http://inch-ci.org/github/roperzh/dotjs_sprockets.png?branch=master)](http://inch-ci.org/github/roperzh/dotjs_sprockets)
5
+
6
+ [doT.js](https://github.com/olado/doT) templates for any application using [sprockets](https://github.com/sstephenson/sprockets).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "dotjs-sprockets"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ To get your template loaded and accessible client-side via the JST variable, you must follow:
25
+
26
+ - The template file must be under one of the Sprockets load paths ( usually `assets/javascripts` or `assets/templates` )
27
+ - The template must have the following extension: `.jst.djs`
28
+
29
+ That's all!
30
+
31
+ ***Example***
32
+
33
+ ```
34
+ app/
35
+ └── assets/
36
+ └── javascripts/
37
+ └── my_template.jst.djs
38
+ ```
39
+
40
+ In this case the template will be accessible in the client via:
41
+
42
+ ```javascript
43
+ // The compiler function
44
+ JST["my_template"] //=> function() ...
45
+
46
+ //The compiled template
47
+ JST["my_template"]() //=> <h2>Hello!</h2> ...
48
+ ```
49
+
50
+ ## Roadmap
51
+
52
+ - Provide an easy way to change the `djs` extension
53
+ - Check support and compatibility for nested templates
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/[my-github-username]/dotjs-sprockets/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dotjs_sprockets/constants'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dotjs_sprockets"
8
+ spec.version = DotjsSprockets::VERSION
9
+ spec.authors = ["Roberto Dip"]
10
+ spec.email = ["dip.jesusr@gmail.com"]
11
+ spec.summary = %q{doT.js templates for sprockets.}
12
+ spec.description = %q{doT.js templates for sprockets powered by ExecJS.}
13
+ spec.homepage = "https://github.com/roperzh/dotjs_sprockets"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+
24
+ spec.add_runtime_dependency "sprockets", "~> 2"
25
+ spec.add_runtime_dependency "execjs", "~> 2"
26
+ end
@@ -0,0 +1,22 @@
1
+ # Configuration constants, all of these are accessible from outside
2
+ #
3
+ # @since 0.1.0
4
+ module DotjsSprockets
5
+ # Local gem assets
6
+ #
7
+ # Version 0.1.0 includes doT.js v1.0.1
8
+ ASSETS_PATH = "../../../vendor/assets/javascripts/dotjs_sprockets"
9
+
10
+ # Extension for the templates, sprockets will find and precompile all files
11
+ # with DotjsSprockets::EXTENSION
12
+ #
13
+ # This can be changed from the outside:
14
+ #
15
+ # DotjsSprockets::EXTENSION = ".dotjs"
16
+ #
17
+ # @since 0.1.0
18
+ EXTENSION = ".djs"
19
+
20
+ # Current version
21
+ VERSION = "0.1.0"
22
+ end
@@ -0,0 +1,58 @@
1
+ require "execjs"
2
+
3
+ module DotjsSprockets
4
+
5
+ # The engine class is in charge to handle the template compilation
6
+ # via ExecJS, provides a clean interface to be used by the TiltDot class
7
+ #
8
+ # @since 0.1.0
9
+ class Engine
10
+ class << self
11
+
12
+ # Given a path to a template it returns the compiled version
13
+ #
14
+ # @param full_path [String] the template full path
15
+ #
16
+ # @return [String] JavaScript function that may be directly executed client-side
17
+ #
18
+ # @example Compiling a Template
19
+ # DotjsSprockets::Engine.precompile("/my/template.jst.djs")
20
+ # # => "function()...."
21
+ #
22
+ # @since 0.1.0
23
+ def precompile(full_path)
24
+ template = open(full_path).read.chomp
25
+ context.eval("doT.compile('#{template}').toString()")
26
+ end
27
+
28
+ private
29
+
30
+ # Stores a ExecJS context instance
31
+ #
32
+ # @return [ExecJS::ExternalRuntime::Context]
33
+ #
34
+ # @since 0.1.0
35
+ def context
36
+ @context ||= ExecJS.compile(source)
37
+ end
38
+
39
+ # Stores the full path of the library source (doT.js)
40
+ #
41
+ # @return [String] full path of the doT.js library
42
+ #
43
+ # @since 0.1.0
44
+ def path
45
+ @path ||= File.expand_path(DotjsSprockets::ASSETS_PATH, __FILE__)
46
+ end
47
+
48
+ # Stores the contents of the library source (doT.js)
49
+ #
50
+ # @return [String] full content of the doT.js library
51
+ #
52
+ # @since 0.1.0
53
+ def source
54
+ @source ||= open("#{path}/doT.js").read
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,37 @@
1
+ require "tilt"
2
+
3
+ module DotjsSprockets
4
+ # New tilt template declaration, from the tilt docs:
5
+ # Base class for template implementations Subclasses must implement
6
+ # the #prepare method and one of the #evaluate or #precompiled_template
7
+ # methods.
8
+ #
9
+ # More info here: https://github.com/rtomayko/tilt/blob/master/lib/tilt/template.rb
10
+ #
11
+ # @since 0.1.0
12
+ class TiltDot < Tilt::Template
13
+ # Define the mime type of the template
14
+ #
15
+ # @since 0.1.0
16
+ def self.default_mime_type
17
+ "application/javascript"
18
+ end
19
+
20
+ # Required method definition in order to implement the Tilt::Template
21
+ # interface
22
+ #
23
+ # @since 0.1.0
24
+ def prepare
25
+ end
26
+
27
+ # Execute the compiled template and return the result string
28
+ #
29
+ # @return [String] the compiled template
30
+ #
31
+ # @since 0.1.0
32
+ def evaluate(scope, locals, &block)
33
+ template_name = scope.pathname
34
+ DotjsSprockets::Engine.precompile(template_name)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ require "dotjs_sprockets/constants"
2
+ require "dotjs_sprockets/engine"
3
+ require "dotjs_sprockets/tilt_dot"
4
+
5
+ Sprockets.register_engine ::DotjsSprockets::EXTENSION, DotjsSprockets::TiltDot
@@ -0,0 +1,136 @@
1
+ // doT.js
2
+ // 2011, Laura Doktorova, https://github.com/olado/doT
3
+ // Licensed under the MIT license.
4
+
5
+ (function() {
6
+ "use strict";
7
+
8
+ var doT = {
9
+ version: '1.0.1',
10
+ templateSettings: {
11
+ evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
12
+ interpolate: /\{\{=([\s\S]+?)\}\}/g,
13
+ encode: /\{\{!([\s\S]+?)\}\}/g,
14
+ use: /\{\{#([\s\S]+?)\}\}/g,
15
+ useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
16
+ define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
17
+ defineParams:/^\s*([\w$]+):([\s\S]+)/,
18
+ conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
19
+ iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
20
+ varname: 'it',
21
+ strip: true,
22
+ append: true,
23
+ selfcontained: false
24
+ },
25
+ template: undefined, //fn, compile template
26
+ compile: undefined //fn, for express
27
+ }, global;
28
+
29
+ if (typeof module !== 'undefined' && module.exports) {
30
+ module.exports = doT;
31
+ } else if (typeof define === 'function' && define.amd) {
32
+ define(function(){return doT;});
33
+ } else {
34
+ global = (function(){ return this || (0,eval)('this'); }());
35
+ global.doT = doT;
36
+ }
37
+
38
+ function encodeHTMLSource() {
39
+ var encodeHTMLRules = { "&": "&#38;", "<": "&#60;", ">": "&#62;", '"': '&#34;', "'": '&#39;', "/": '&#47;' },
40
+ matchHTML = /&(?!#?\w+;)|<|>|"|'|\//g;
41
+ return function() {
42
+ return this ? this.replace(matchHTML, function(m) {return encodeHTMLRules[m] || m;}) : this;
43
+ };
44
+ }
45
+ String.prototype.encodeHTML = encodeHTMLSource();
46
+
47
+ var startend = {
48
+ append: { start: "'+(", end: ")+'", endencode: "||'').toString().encodeHTML()+'" },
49
+ split: { start: "';out+=(", end: ");out+='", endencode: "||'').toString().encodeHTML();out+='"}
50
+ }, skip = /$^/;
51
+
52
+ function resolveDefs(c, block, def) {
53
+ return ((typeof block === 'string') ? block : block.toString())
54
+ .replace(c.define || skip, function(m, code, assign, value) {
55
+ if (code.indexOf('def.') === 0) {
56
+ code = code.substring(4);
57
+ }
58
+ if (!(code in def)) {
59
+ if (assign === ':') {
60
+ if (c.defineParams) value.replace(c.defineParams, function(m, param, v) {
61
+ def[code] = {arg: param, text: v};
62
+ });
63
+ if (!(code in def)) def[code]= value;
64
+ } else {
65
+ new Function("def", "def['"+code+"']=" + value)(def);
66
+ }
67
+ }
68
+ return '';
69
+ })
70
+ .replace(c.use || skip, function(m, code) {
71
+ if (c.useParams) code = code.replace(c.useParams, function(m, s, d, param) {
72
+ if (def[d] && def[d].arg && param) {
73
+ var rw = (d+":"+param).replace(/'|\\/g, '_');
74
+ def.__exp = def.__exp || {};
75
+ def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
76
+ return s + "def.__exp['"+rw+"']";
77
+ }
78
+ });
79
+ var v = new Function("def", "return " + code)(def);
80
+ return v ? resolveDefs(c, v, def) : v;
81
+ });
82
+ }
83
+
84
+ function unescape(code) {
85
+ return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, ' ');
86
+ }
87
+
88
+ doT.template = function(tmpl, c, def) {
89
+ c = c || doT.templateSettings;
90
+ var cse = c.append ? startend.append : startend.split, needhtmlencode, sid = 0, indv,
91
+ str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;
92
+
93
+ str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g,' ')
94
+ .replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,''): str)
95
+ .replace(/'|\\/g, '\\$&')
96
+ .replace(c.interpolate || skip, function(m, code) {
97
+ return cse.start + unescape(code) + cse.end;
98
+ })
99
+ .replace(c.encode || skip, function(m, code) {
100
+ needhtmlencode = true;
101
+ return cse.start + unescape(code) + cse.endencode;
102
+ })
103
+ .replace(c.conditional || skip, function(m, elsecase, code) {
104
+ return elsecase ?
105
+ (code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
106
+ (code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
107
+ })
108
+ .replace(c.iterate || skip, function(m, iterate, vname, iname) {
109
+ if (!iterate) return "';} } out+='";
110
+ sid+=1; indv=iname || "i"+sid; iterate=unescape(iterate);
111
+ return "';var arr"+sid+"="+iterate+";if(arr"+sid+"){var "+vname+","+indv+"=-1,l"+sid+"=arr"+sid+".length-1;while("+indv+"<l"+sid+"){"
112
+ +vname+"=arr"+sid+"["+indv+"+=1];out+='";
113
+ })
114
+ .replace(c.evaluate || skip, function(m, code) {
115
+ return "';" + unescape(code) + "out+='";
116
+ })
117
+ + "';return out;")
118
+ .replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/\r/g, '\\r')
119
+ .replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, '')
120
+ .replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');
121
+
122
+ if (needhtmlencode && c.selfcontained) {
123
+ str = "String.prototype.encodeHTML=(" + encodeHTMLSource.toString() + "());" + str;
124
+ }
125
+ try {
126
+ return new Function(c.varname, str);
127
+ } catch (e) {
128
+ if (typeof console !== 'undefined') console.log("Could not create a template function: " + str);
129
+ throw e;
130
+ }
131
+ };
132
+
133
+ doT.compile = function(tmpl, def) {
134
+ return doT.template(tmpl, null, def);
135
+ };
136
+ }());
@@ -0,0 +1 @@
1
+ //= require dotjs_sprockets/doT
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotjs_sprockets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Dip
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sprockets
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: execjs
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ description: doT.js templates for sprockets powered by ExecJS.
70
+ email:
71
+ - dip.jesusr@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - dotjs_sprockets.gemspec
82
+ - lib/dotjs_sprockets.rb
83
+ - lib/dotjs_sprockets/constants.rb
84
+ - lib/dotjs_sprockets/engine.rb
85
+ - lib/dotjs_sprockets/tilt_dot.rb
86
+ - vendor/assets/javascripts/dotjs_sprockets/doT.js
87
+ - vendor/assets/javascripts/dotjs_sprockets/index.js
88
+ homepage: https://github.com/roperzh/dotjs_sprockets
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: doT.js templates for sprockets.
112
+ test_files: []