cft 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +57 -0
  3. data/lib/cft.rb +51 -0
  4. metadata +97 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Alex MacCaw
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,57 @@
1
+ Ruby CFT
2
+ ========
3
+
4
+ Ruby CFT is a bridge to the official
5
+ [CFT](https://github.com/sstephenson/cft) compiler for embedded
6
+ CoffeeScript templates.
7
+
8
+ ```ruby
9
+ require "cft"
10
+
11
+ CFT.compile(File.read("template.cft"))
12
+ # => "function(...) {...}"
13
+
14
+ context = CFT.context_for("Hello <%= @name %>")
15
+ context.call("render", :name => "Sam")
16
+ # => "Hello Sam"
17
+
18
+ CFT.render("Hello <%= @name %>", :name => "world")
19
+ # => "Hello world"
20
+ ```
21
+
22
+ Installation
23
+ ------------
24
+
25
+ $ gem install cft
26
+
27
+
28
+ Dependencies
29
+ ------------
30
+
31
+ This library depends on the `cft-source` gem which is updated any time
32
+ a new version of CFT is released. (The `cft-source` gem's version
33
+ number is synced with each official CFT release.) This way you can
34
+ build against different versions of CFT by requiring the correct
35
+ version of the `cft-source` gem.
36
+
37
+ In addition, you can use this library with unreleased versions of CFT
38
+ by setting the `CFT_SOURCE_PATH` environment variable:
39
+
40
+ export CFT_SOURCE_PATH=/path/to/cft/dist/cft.js
41
+
42
+
43
+ ### Ruby CoffeeScript
44
+
45
+ The [Ruby CoffeeScript](https://github.com/josh/ruby-coffee-script)
46
+ library is used to load the CoffeeScript compiler source. See the
47
+ [README](https://github.com/josh/ruby-coffee-script/blob/master/README.md)
48
+ for information on loading a specific version of the CoffeeScript
49
+ compiler.
50
+
51
+ ### ExecJS
52
+
53
+ The [ExecJS](https://github.com/sstephenson/execjs) library is used to
54
+ automatically choose the best JavaScript engine for your
55
+ platform. Check out its
56
+ [README](https://github.com/sstephenson/execjs/blob/master/README.md)
57
+ for a complete list of supported engines.
@@ -0,0 +1,51 @@
1
+ require "coffee_script"
2
+ require "cft/source"
3
+ require "execjs"
4
+
5
+ module CFT
6
+ module Source
7
+ def self.path
8
+ @path ||= ENV["CFT_SOURCE_PATH"] || bundled_path
9
+ end
10
+
11
+ def self.path=(path)
12
+ @contents = @version = @context = nil
13
+ @path = path
14
+ end
15
+
16
+ def self.contents
17
+ @contents ||= File.read(path)
18
+ end
19
+
20
+ def self.combined_contents
21
+ [CoffeeScript::Source.contents, contents].join(";\n")
22
+ end
23
+
24
+ def self.version
25
+ @version ||= contents[/CFT Compiler v(.*?)\s/, 1]
26
+ end
27
+
28
+ def self.context
29
+ @context ||= ExecJS.compile(combined_contents)
30
+ end
31
+ end
32
+
33
+ class << self
34
+ def version
35
+ Source.version
36
+ end
37
+
38
+ def compile(template)
39
+ template = template.read if template.respond_to?(:read)
40
+ Source.context.call("cft.precompile", template)
41
+ end
42
+
43
+ def context_for(template)
44
+ ExecJS.compile("var render = #{compile(template)}")
45
+ end
46
+
47
+ def render(template, locals = {})
48
+ context_for(template).call("render", locals)
49
+ end
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cft
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex MacCaw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: coffee-script
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: cft-source
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: execjs
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! ' Ruby CFT is a bridge to the official JavaScript CFT compiler.
63
+
64
+ '
65
+ email: alex@alexmaccaw.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - lib/cft.rb
71
+ - LICENSE
72
+ - README.md
73
+ homepage: https://github.com/maccman/ruby-cft
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.24
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Ruby CFT Compiler (Embedded CoffeeScript templates)
97
+ test_files: []