Skip to content

Commit

Permalink
Update to haml-coffee 0.5.5, document and spec the basename option.
Browse files Browse the repository at this point in the history
  • Loading branch information
netzpirat committed Dec 16, 2011
1 parent 3597921 commit 4001578
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 16 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ config.hamlcoffee.format = 'xhtml'

### Template namespace

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

**Example:**

A template `app/assets/templates/header.hamlc` with the given content:
By default all Haml Coffee templates are registered under the `JST` namespace. A template
`app/assets/templates/header.hamlc` with the given content:

```haml
%header
Expand Down Expand Up @@ -208,7 +205,7 @@ And you must make sure `MyApp` exists before any template is loaded.
#### Template name

The name under which the template can be addressed in the namespace depends not only from the filename, but also on
the directory name.
the directory name by default.

The following examples assumes a configured namespace `window.JST` and the asset template directory
`app/assets/templates`:
Expand All @@ -217,6 +214,25 @@ The following examples assumes a configured namespace `window.JST` and the asset
* `app/assets/templates/users/new.hamlc` will become `JST['users/new']`
* `app/assets/templates/shared/form/address.hamlc` will become `JST['shared/form/address']`

### Basename

If you don't want to have your directory names under which your template is located to be contained in the JST name,
you can configure Haml Coffee n your `config/application.rb` to strip off the path to the file name and only use the
basename as JST name:

```ruby
config.hamlcoffee.basename = true
```

With this setting enabled the following naming rule applies:

* `app/assets/templates/login.hamlc` will become `JST['login']`
* `app/assets/templates/users/new.hamlc` will become `JST['new']`
* `app/assets/templates/shared/form/address.hamlc` will become `JST['address']`

This setting has only an effect when you're using Haml Coffee to generate the JST and not when using the Sprockets
JST processor.

### Escaping

All generated output by running CoffeeScript in your template is being escaped, but you can disable escaping of either
Expand Down
2 changes: 2 additions & 0 deletions lib/haml_coffee_assets/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Engine < Rails::Engine
:format => 'html5',
:namespace => 'window.JST',
:uglify => false,
:basename => false,
:escapeHtml => true,
:escapeAttributes => true,
:cleanValue => true,
Expand Down Expand Up @@ -39,6 +40,7 @@ class Engine < Rails::Engine
config.namespace = options[:namespace]
config.format = options[:format]
config.uglify = options[:uglify]
config.basename = options[:basename]
config.escapeHtml = options[:escapeHtml]
config.escapeAttributes = options[:escapeAttributes]
config.cleanValue = options[:cleanValue]
Expand Down
7 changes: 6 additions & 1 deletion lib/haml_coffee_assets/haml_coffee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ module HamlCoffee
mattr_accessor :uglify
self.uglify = false

# Ignore path when generate the JST
#
mattr_accessor :basename
self.basename = false

# Escape template code output
#
mattr_accessor :escapeHtml
Expand Down Expand Up @@ -87,7 +92,7 @@ def configure
# @return [String] the compiled template in JavaScript
#
def compile(name, source, jst = true)
runtime.call('HamlCoffeeAssets.compile', name, source, jst, HamlCoffee.namespace, HamlCoffee.format, HamlCoffee.uglify,
runtime.call('HamlCoffeeAssets.compile', name, source, jst, HamlCoffee.namespace, HamlCoffee.format, HamlCoffee.uglify, HamlCoffee.basename,
HamlCoffee.escapeHtml, HamlCoffee.escapeAttributes, HamlCoffee.cleanValue,
HamlCoffee.customHtmlEscape, HamlCoffee.customCleanValue,
HamlCoffee.customPreserve, HamlCoffee.customFindAndPreserve,
Expand Down
4 changes: 3 additions & 1 deletion lib/haml_coffee_assets/haml_coffee_assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var HamlCoffeeAssets = (function(){
* @param jst [Boolean] if a JST template should be generated
* @param format [String] output HTML format
* @param uglify [Boolean] skip HTML indention
* @param basename [Boolean] ignore path when generate JST
* @param escapeHtml [Boolean] whether to escape HTML output by default or not
* @param escapeAttributes [Boolean] whether to escape HTML attributes output by default or not
* @param customHtmlEscape [String] the name of the function to escape the output
Expand All @@ -33,7 +34,7 @@ var HamlCoffeeAssets = (function(){
* @param selfCloseTags [String] comma separated list of tags to self-closing tags
* @param context [String] the name of the function to merge contexts
*/
compile: function(name, source, jst, namespace, format, uglify,
compile: function(name, source, jst, namespace, format, uglify, basename,
escapeHtml, escapeAttributes, cleanValue,
customHtmlEscape, customCleanValue, customPreserve, customFindAndPreserve,
preserveTags, selfCloseTags,
Expand All @@ -42,6 +43,7 @@ var HamlCoffeeAssets = (function(){
var compiler = new Compiler({
format: format,
uglify: uglify,
basename: basename,
escapeHtml: escapeHtml,
escapeAttributes: escapeAttributes,
cleanValue: cleanValue,
Expand Down
19 changes: 11 additions & 8 deletions lib/js/haml-coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname,
module.exports = HamlCoffee = (function() {

function HamlCoffee(options) {
var _base, _base2, _base3, _base4, _base5, _base6, _base7, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7;
var _base, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
this.options = options != null ? options : {};
if ((_ref = (_base = this.options).escapeHtml) == null) {
_base.escapeHtml = true;
Expand All @@ -354,14 +354,17 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname,
_base3.cleanValue = true;
}
if ((_ref4 = (_base4 = this.options).uglify) == null) _base4.uglify = false;
if ((_ref5 = (_base5 = this.options).format) == null) {
_base5.format = 'html5';
if ((_ref5 = (_base5 = this.options).basename) == null) {
_base5.basename = false;
}
if ((_ref6 = (_base6 = this.options).preserveTags) == null) {
_base6.preserveTags = 'pre,textarea';
if ((_ref6 = (_base6 = this.options).format) == null) {
_base6.format = 'html5';
}
if ((_ref7 = (_base7 = this.options).selfCloseTags) == null) {
_base7.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base';
if ((_ref7 = (_base7 = this.options).preserveTags) == null) {
_base7.preserveTags = 'pre,textarea';
}
if ((_ref8 = (_base8 = this.options).selfCloseTags) == null) {
_base8.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base';
}
}

Expand Down Expand Up @@ -535,7 +538,7 @@ require.define("/haml-coffee.js", function (require, module, exports, __dirname,
if (namespace == null) namespace = 'window.HAML';
template = '';
segments = ("" + namespace + "." + templateName).replace(/(\s|-)+/g, '_').split(/\./);
templateName = this.options.basename ? segments.pop() : segments.pop().split(/\/|\\/).pop();
templateName = this.options.basename ? segments.pop().split(/\/|\\/).pop() : segments.pop();
namespace = segments.shift();
if (segments.length !== 0) {
for (_i = 0, _len = segments.length; _i < _len; _i++) {
Expand Down
42 changes: 42 additions & 0 deletions spec/haml_coffee_assets/haml_coffee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
HamlCoffeeAssets::HamlCoffee.namespace = 'window.JST'
HamlCoffeeAssets::HamlCoffee.format = 'html5'
HamlCoffeeAssets::HamlCoffee.uglify = false
HamlCoffeeAssets::HamlCoffee.basename = false
HamlCoffeeAssets::HamlCoffee.preserveTags = 'textarea,pre'
HamlCoffeeAssets::HamlCoffee.selfCloseTags = 'meta,img,link,br,hr,input,area,param,col,base'
HamlCoffeeAssets::HamlCoffee.escapeHtml = true
Expand Down Expand Up @@ -447,6 +448,47 @@
end
end

context 'basename configuration' do
it 'does not strip the path by default' do
subject.compile('path/to/file', "%p Basename").should eql <<-TEMPLATE
(function() {
var _ref;
if ((_ref = window.JST) == null) {
window.JST = {};
}
window.JST['path/to/file'] = function(context) {
return (function() {
var $o;
$o = [];
$o.push("<p>Basename</p>");
return $o.join("\\n");
}).call(context);
};
}).call(this);
TEMPLATE
end

it 'does strip the path' do
subject.basename = true
subject.compile('path/to/file', "%p Basename").should eql <<-TEMPLATE
(function() {
var _ref;
if ((_ref = window.JST) == null) {
window.JST = {};
}
window.JST['file'] = function(context) {
return (function() {
var $o;
$o = [];
$o.push("<p>Basename</p>");
return $o.join("\\n");
}).call(context);
};
}).call(this);
TEMPLATE
end
end

context 'whitespace tag list configuration' do
it 'uses textarea and pre by default' do
subject.compile('ws', "%textarea= 'Test\\nMe'\n%pre= 'Test\\nMe'\n%p= 'Test\\nMe'\n").should eql <<-TEMPLATE
Expand Down

0 comments on commit 4001578

Please sign in to comment.