Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Nov 7, 2011
0 parents commit 11b2527
Show file tree
Hide file tree
Showing 21 changed files with 1,657 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pkg/*
doc/*
*.gem
*.rbc
.*.swp
*.bak
.bundle
.yardoc
.rbx
.rvmrc
Gemfile.lock

## MAC OS
.DS_Store
.Trashes
.com.apple.timemachine.supported
.fseventsd
Desktop DB
Desktop DF
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
rvm:
- 1.8.7
- 1.9.2
- ree
- jruby
branches:
only:
- master
notifications:
recipients:
- michi@netzpiraten.ch
10 changes: 10 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--title 'Haml CoffeeScript Assets Documentation'
--readme README.md
--markup markdown
--markup-provider redcarpet
--private
--protected
--output-dir ./doc
lib/**/*.rb
-
LICENSE
13 changes: 13 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# coding: UTF-8

source "http://rubygems.org"

gemspec

platform :ruby do
gem 'therubyracer', '~> 0.9.8'
end

platform :jruby do
gem 'therubyrhino', '~> 1.72.8'
end
6 changes: 6 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding: UTF-8

guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{ m[1] }_spec.rb" }
end
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011 Michael Kessler <michi@netzpiraten.ch>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
260 changes: 260 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
# HamlCoffeeAssets [![Build Status](https://secure.travis-ci.org/netzpirat/haml_coffee_assets.png)](http://travis-ci.org/netzpirat/haml_coffee_assets)

HamlCoffeeAssets compiles [Haml CoffeeScript](https://github.com/9elements/haml-coffee) templates in the Rails 3.1 asset
pipeline.

Tested on MRI Ruby 1.8.7, 1.9.2, REE and the latest versions of JRuby.

## HamlCoffee

HamlCoffee allows you to write inline [CoffeeScript](http://jashkenas.github.com/coffee-script/) in your
[HAML](http://haml-lang.com/) template:

```haml
#cart
%h2= I18n.t('js.cart.title')
- if @cart.length == 0
%p.empty= I18n.t('js.cart.empty')
- else
%ul
- for item in @cart
%li
.item
= item.name
%a{ :href => "/cart/item/remove/#{ item.id }" }
= I18n.t('js.cart.item.remove')
```

## Installation

The simplest way to install Guard is to use [Bundler](http://gembundler.com/).
Add `haml_coffee_assets` and `execjs` to your `Gemfile`:

```ruby
group :assets do
gem 'haml_coffee_assets'
gem 'execjs'
end
```

And require the `haml_coffee_assets.js` in your `application.js.coffee`

```coffeescript
#= require haml_coffee_assets
```

### JavaScript runtimes

HamlCoffeeAssets uses [ExecJS](https://github.com/sstephenson/execjs) to pick the best runtime to evaluate the
CoffeeScript and generate the JavaScript template.

* With CRuby you want to use a V8 JavaScript Engine or Mozilla SpiderMonkey.
* With JRuby you want to use the Mozilla Rhino.
* On Mac OS X you want to use Apple JavaScriptCore.
* On Linux or as a node.js developer you want to use Node.js (V8).
* On Windows you want to use Microsoft Windows Script Host.

The following sections gives you a short overview of the available JavaScript runtimes and how to install it.

### Node.js (V8)

You can install [node.js](http://nodejs.org/) and use its V8 engine. On OS X you may want to install it with
[Homebrew](http://mxcl.github.com/homebrew/), on Linux with your package manager and on Windows you have to download and
install the [executable](http://www.nodejs.org/#download).

### V8 JavaScript Engine

To use the [V8 JavaScript Engine](http://code.google.com/p/v8/), simple add `therubyracer` to your `Gemfile`.
The Ruby Racer acts as a bridge between Ruby and the V8 engine, that will be automatically installed by the Ruby Racer.

```ruby
group :development do
gem 'therubyracer'
end
```

Another alternative is [Mustang](https://github.com/nu7hatch/mustang), a Ruby proxy library for the awesome Google V8
JavaScript engine. Just add `mustang` to your `Gemfile`:

```ruby
group :development do
gem 'mustang'
end
```

### Mozilla SpiderMonkey

To use [Mozilla SpiderMonkey](https://developer.mozilla.org/en/SpiderMonkey), simple add `johnson` to your `Gemfile`.
Johnson embeds the Mozilla SpiderMonkey JavaScript runtime as a C extension.

```ruby
group :development do
gem 'johnson'
end
```

### Mozilla Rhino

If you're using JRuby, you can embed the [Mozilla Rhino](http://www.mozilla.org/rhino/) runtime by adding `therubyrhino`
to your `Gemfile`:

```ruby
group :development do
gem 'therubyrhino'
end
```

### Apple JavaScriptCore

[JavaScriptCore](http://developer.apple.com/library/mac/#documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/index.html)
is Safari's Nitro JavaScript Engine and only usable on Mac OS X. You don't have to install anything, because
JavaScriptCore is already packaged with Mac OS X.

### Microsoft Windows Script Host

[Microsoft Windows Script Host](http://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx) is available on any Microsoft
Windows operating systems.

## Usage

You should place all your HamlCoffee templates in the `app/assets/templates` directory and include all templates in
your `application.js.coffee`:

```coffeescript
#= require_tree ../templates
```

Now you can start to add your HamlCoffee templates to your template directory. Make sure all your templates have a
`.hamlc` extension to be recognized by HamlCoffeeAssets.

**Note:** HamlCoffee already generates a JavaScript Template, so there is not need to pass it to the `JST` Sprocket
processor by using `.jst.hamlc` as extension, and if you do, the HamlCoffee templates will not work.

## Configuration

### Template namespace

By default all HamlCoffee templates are registered under the `JST` namespace.

Example:

A template located in `app/assets/templates/header.hamlc` with the given content:

```haml
%header
%h2= title
```

will be accessible in your browser as `JST.header`. You can now render the precompiled template with:

```javascript
JST.header.render({ title: 'Hello HamlCoffee' })
```

If you prefer another namespace, you can set it in your `application.rb`:

```ruby
config.hamlcoffee.namespace = 'HAML'
```

### Escaping

By default your code block in your HamlCoffee template will be escaped through the `HAML.escape` function that is
provided in the `haml_coffee_assets.js`.

You can set another escaping function in your `application.rb`:

```ruby
config.hamlcoffee.escape = 'App.myEscape'
```

or disable escaping completely:

```ruby
config.hamlcoffee.escape = false
```

Your custom escape function must take the unescaped text as parameter and returns the escape function.
The following example implements only ampersand escaping:

```coffeescript
App.myEscape = (text) -> text.replace(/&/g, '&amp;')
```

### Global Context

HamlCoffeeAssets allows you to configure a global context function that gets merged into the local template context for
each template.

There is a example implementation provided in the `haml_coffee_assets.js` that uses the `extend` function
from these frameworks:

* jQuery
* Underscore.js
* Prototype
* MooTools
* Zepto.js

If you use one of these, than you can simply override `HAML.globals` and return the global HAML context object:

```coffeescript
HAML.globals = ->
{
authenticated: App.isAuthenticated()
isAdmin: App.currentUser.hasRole('admin')
}
```

If you like to use your own implementation, simply configure your context function in your `application.rb`:

```ruby
config.hamlcoffee.context = `App.globalTemplateContext`
```

or disable the global context completely:

```ruby
config.hamlcoffee.context = false
```

Your custom context function must take the local context as parameter and returns the merged context function.
The following example implementation used the _.underscore extend function to merge an inline defined global
context:

```coffeescript
App.globalTemplateContext = (locals) -> _.extend({}, {
authenticated: App.isAuthenticated()
}, locals)
```

## Acknowledgement

* [Jeremy Ashkenas](http://twitter.com/#!/jashkenas) for CoffeeScript, that little language that compiles into
JavaScript.
* The people at [9elements](https://github.com/9elements) who gave us
[haml-coffee](https://github.com/9elements/haml-coffee), an elegant JavaScript template solution.

## License

(The MIT License)

Copyright (c) 2011 Michael Kessler

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# coding: UTF-8

require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

namespace(:spec) do
desc 'Run all specs on multiple ruby versions (requires rvm)'
task(:portability) do
travis_config_file = File.expand_path("../.travis.yml", __FILE__)
begin
travis_options ||= YAML::load_file(travis_config_file)
rescue => ex
puts "Travis config file '#{ travis_config_file }' could not be found: #{ ex.message }"
return
end

travis_options['rvm'].each do |version|
system <<-BASH
bash -c 'source ~/.rvm/scripts/rvm;
rvm #{ version };
ruby_version_string_size=`ruby -v | wc -m`
echo;
for ((c=1; c<$ruby_version_string_size; c++)); do echo -n "="; done
echo;
echo "`ruby -v`";
for ((c=1; c<$ruby_version_string_size; c++)); do echo -n "="; done
echo;
RBXOPT="-Xrbc.db" bundle install;
RBXOPT="-Xrbc.db" bundle exec rspec spec -f doc 2>&1;'
BASH
end
end
end
Loading

0 comments on commit 11b2527

Please sign in to comment.