coffeekup_rails 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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +44 -0
- data/Rakefile +1 -0
- data/coffeekup.gemspec +20 -0
- data/lib/coffeekup/configuration.rb +31 -0
- data/lib/coffeekup/errors.rb +15 -0
- data/lib/coffeekup/version.rb +3 -0
- data/lib/coffeekup_rails.rb +69 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#CoffeeKup Rails
|
2
|
+
CoffeeKup Rails is an asset pipeline engine/preprocessor for [coffeekup](http://coffeekup.org/) template files.
|
3
|
+
Simply place your coffeekup templates in a configured asset directory with a .js.ck extension and they will be automatically available as precompiled js templates.
|
4
|
+
##Example
|
5
|
+
Say your client side views live in `/app/assets/javascripts/views` (default).
|
6
|
+
|
7
|
+
Given `/app/assets/javascripts/views/shared/hello.js.ck`:
|
8
|
+
|
9
|
+
h1 "Hello #{@name}."
|
10
|
+
|
11
|
+
CoffeeKup will automatically compile the coffeescript source to a coffeekup javascript template under a desired
|
12
|
+
global js object (defaults to `window.templates`). Template names are period delimited by directory structure:
|
13
|
+
|
14
|
+
From the javascript console:
|
15
|
+
|
16
|
+
templates['shared.hello']({name: 'chris'})
|
17
|
+
=> "<h1>Hello chris.</h1>"
|
18
|
+
|
19
|
+
This happens upon every page load when your .ck files change thanks to sprockets and the asset pipeline just as you would expect for a .coffee file.
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
1. First grab [node.js](http://nodejs.org/#download) and [npm](https://github.com/isaacs/npm)
|
23
|
+
2. `npm install coffeekup -g`
|
24
|
+
3. `gem 'coffeekup_rails', :git => "git://github.com/chrismccord/coffeekup_rails.git"` and `bundle`
|
25
|
+
|
26
|
+
Will be pushed to rubygems soon.
|
27
|
+
|
28
|
+
### Initializer
|
29
|
+
Configuration optional (defaults are redundantly set here); however, `Coffeekup.register_engine` must be included.
|
30
|
+
|
31
|
+
app/config/initializers/coffeekup.rb:
|
32
|
+
|
33
|
+
Coffeekup.configure do |config|
|
34
|
+
config.namespace = 'templates'
|
35
|
+
config.extension = 'ck'
|
36
|
+
end
|
37
|
+
|
38
|
+
Coffeekup.register_engine
|
39
|
+
|
40
|
+
## Configuration
|
41
|
+
Coffeekup.configure do |config|
|
42
|
+
config.namespace = 'name_of_global_js_object' # default 'templates'
|
43
|
+
config.extension = 'coffeekup' # default 'ck'
|
44
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/coffeekup.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "coffeekup/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "coffeekup_rails"
|
7
|
+
s.version = Coffeekup::VERSION
|
8
|
+
s.authors = ["Chris McCord"]
|
9
|
+
s.email = ["chris@chrismccord.com"]
|
10
|
+
s.homepage = "https://github.com/chrismccord/coffeekup_rails"
|
11
|
+
s.summary = %q{Asset pipeline engine/preprocessor for CoffeeKup template files}
|
12
|
+
s.description = %q{CoffeeKup Rails is an asset pipeline engine/preprocessor for CoffeeKup template files.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "coffeekup_rails"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Coffeekup
|
2
|
+
module Configuration
|
3
|
+
|
4
|
+
# Global frontend object name holding the templates
|
5
|
+
DEFAULT_NAMESPACE = 'templates'
|
6
|
+
# Base view directory where .ck templates will live
|
7
|
+
DEFAULT_BASE_DIRECTORY = "app/assets/javascripts/views"
|
8
|
+
# Extensions to use for Coffeekup files
|
9
|
+
DEFAULT_EXTENSION = "ck"
|
10
|
+
|
11
|
+
attr_accessor :namespace,
|
12
|
+
:base_directory,
|
13
|
+
:extension,
|
14
|
+
:files
|
15
|
+
|
16
|
+
# set all configuration options to their default values
|
17
|
+
def self.extended(base)
|
18
|
+
base.reset
|
19
|
+
end
|
20
|
+
|
21
|
+
def configure
|
22
|
+
yield self
|
23
|
+
end
|
24
|
+
|
25
|
+
def reset
|
26
|
+
self.namespace = DEFAULT_NAMESPACE
|
27
|
+
self.base_directory = DEFAULT_BASE_DIRECTORY
|
28
|
+
self.extension = DEFAULT_EXTENSION
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require "coffeekup/version"
|
2
|
+
require "coffeekup/errors"
|
3
|
+
require "coffeekup/configuration"
|
4
|
+
require 'open3'
|
5
|
+
require 'tilt'
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
module Coffeekup
|
9
|
+
extend Configuration
|
10
|
+
|
11
|
+
def self.register_engine
|
12
|
+
Rails.application.assets.register_engine ".#{self.extension}", Coffeekup::Template
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# Takes coffeescript coffeekup file (.js.ck) and returns compiled js template.
|
17
|
+
# JS templates are loaded into window.#{self.namespace} object, ie:
|
18
|
+
# /app/assets/javascripts/views/shared/index.js.ck => templates['shared.index']({})
|
19
|
+
#
|
20
|
+
# Templates are searched under self.base_directory (defaults /app/assets/javascripts/views) and
|
21
|
+
# must be a path that the asset pipeline recognizes in order to have sprockets load the files.
|
22
|
+
#
|
23
|
+
# params:
|
24
|
+
# source input file path
|
25
|
+
# options
|
26
|
+
# namespace optional js template namespace to use instead of default
|
27
|
+
#
|
28
|
+
# returns:
|
29
|
+
# contents of compiled coffeekup js template
|
30
|
+
#
|
31
|
+
def self.compile!(source, options = {})
|
32
|
+
nspace = options[:namespace] || self.namespace
|
33
|
+
base_dir = self.base_directory
|
34
|
+
base_dir += "/" if base_dir[-1] != "/"
|
35
|
+
filename = source.split(base_dir).last.gsub("/", ".").gsub('.js', '')
|
36
|
+
@files = {
|
37
|
+
:tmp => "#{Rails.root}/tmp/#{filename}",
|
38
|
+
:compiled_tmp => "#{Rails.root}/tmp/#{filename}".gsub(self.extension, 'js')
|
39
|
+
}
|
40
|
+
FileUtils.cp(source, @files[:tmp])
|
41
|
+
i, o, stderr = Open3.popen3("coffeekup --js --format --namespace '#{nspace}' '#{@files[:tmp]}'")
|
42
|
+
errors = stderr.read
|
43
|
+
raise CompilationError, "#{source}\n\n#{errors}" if not errors.blank?
|
44
|
+
|
45
|
+
File.open(@files[:compiled_tmp]).read
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def self.cleanup
|
50
|
+
@files.each{|f| File.delete(f[1]) rescue nil }
|
51
|
+
end
|
52
|
+
|
53
|
+
class Template < Tilt::Template
|
54
|
+
|
55
|
+
def prepare
|
56
|
+
end
|
57
|
+
|
58
|
+
def evaluate(context, locals, &block)
|
59
|
+
begin
|
60
|
+
compiled_output = Coffeekup.compile!(@file)
|
61
|
+
ensure
|
62
|
+
Coffeekup.cleanup
|
63
|
+
end
|
64
|
+
|
65
|
+
compiled_output
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coffeekup_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Chris McCord
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-08 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: CoffeeKup Rails is an asset pipeline engine/preprocessor for CoffeeKup template files.
|
23
|
+
email:
|
24
|
+
- chris@chrismccord.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- coffeekup.gemspec
|
37
|
+
- lib/coffeekup/configuration.rb
|
38
|
+
- lib/coffeekup/errors.rb
|
39
|
+
- lib/coffeekup/version.rb
|
40
|
+
- lib/coffeekup_rails.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: https://github.com/chrismccord/coffeekup_rails
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
hash: 3
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: coffeekup_rails
|
71
|
+
rubygems_version: 1.5.0
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Asset pipeline engine/preprocessor for CoffeeKup template files
|
75
|
+
test_files: []
|
76
|
+
|