Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Literate CoffeeScript support #173

Open
wants to merge 5 commits into from

4 participants

@skalee

Literate CoffeeScript has been introduced in version 1.5.0 of CoffeeScript.

@judofyr
Collaborator

Thanks for the pull request, but this is going to have to wait until 2.0.

@skalee

OK then…

So for anyone who can't wait to have Literate CoffeeScript in Rails' AssetPipeline:

  1. Require tilt in :assets group in Gemfile explicitly pointing to my repository
  2. Add following initializer:
module Sprockets
  register_engine '.litcoffee', Tilt::CoffeeScriptTemplate::Literate
end
@wulftone

Just so other people know specifically what to put in their Gemfile:

group :assets do
  gem 'tilt', github: 'skalee/tilt', branch: 'literate-coffee-script-support'
end

Edited: To reflect the branch info as @skalee mentions below.

@wulftone wulftone referenced this pull request in sstephenson/sprockets
Closed

Support for litcoffee, Literate coffee-script? #420

skalee added some commits
@skalee skalee Cleanup CoffeeScript tests
Make them DRY before introducing tests for literate CoffeeScript
handler, extract template renderer class to instance variable.
f71e617
@skalee skalee Most of CoffeeScript tests extracted to module.
They will be shared with literate CoffeeScript tests.
bdd46ab
@skalee skalee Literate CoffeeScript renderer. f0db94c
@skalee skalee Literate CoffeeScript in TEMPLATES & README. 9acb11d
@skalee skalee Development dependency for literate CoffeeScript. f3b4556
@skalee

@wulftone Because I'm going to rebase that branch from time to time, you might want to refer to branch, not specific commit:

group :assets do
  gem 'tilt', github: 'skalee/tilt', branch: 'literate-coffee-script-support'
end
@nullobject

@skalee @judofyr Ping! Is this any closer to getting merged into master? Anything I could help with?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2013
  1. @skalee

    Cleanup CoffeeScript tests

    skalee authored
    Make them DRY before introducing tests for literate CoffeeScript
    handler, extract template renderer class to instance variable.
  2. @skalee

    Most of CoffeeScript tests extracted to module.

    skalee authored
    They will be shared with literate CoffeeScript tests.
  3. @skalee

    Literate CoffeeScript renderer.

    skalee authored
  4. @skalee
  5. @skalee
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -44,6 +44,7 @@ Support for these template engines is included with the package:
Markaby .mab markaby
Nokogiri .nokogiri nokogiri
CoffeeScript .coffee coffee-script (+ javascript)
+ CoffeeScript (literate) .litcoffee coffee-script (>= 1.5.0) + javascript
Creole (Wiki markup) .wiki, .creole creole
WikiCloth (Wiki markup) .wiki, .mediawiki, .mw wikicloth
Yajl .yajl yajl-ruby
View
1  TEMPLATES.md
@@ -31,6 +31,7 @@ Tilt also includes support for CSS processors like [LessCSS][lesscss] and
* Sass - `Tilt::SassTemplate`
* Scss - `Tilt::ScssTemplate`
* CoffeeScript - `Tilt::CoffeeScriptTemplate`
+ * Literate CoffeeScript - `Tilt::CoffeeScriptTemplate::Literate`
* [Textile](#redcloth) - `Tilt::RedClothTemplate`
* Creole - `Tilt::CreoleTemplate`
* [RDoc](#rdoc) - `Tilt::RDocTemplate`
View
3  lib/tilt.rb
@@ -157,7 +157,8 @@ def clear
register CSVTemplate, 'rcsv'
require 'tilt/coffee'
- register CoffeeScriptTemplate, 'coffee'
+ register CoffeeScriptTemplate, 'coffee'
+ register CoffeeScriptTemplate::Literate, 'litcoffee'
require 'tilt/nokogiri'
register NokogiriTemplate, 'nokogiri'
View
11 lib/tilt/coffee.rb
@@ -28,6 +28,10 @@ def self.default_no_wrap=(value)
@@default_bare = value
end
+ def self.literate?
+ false
+ end
+
def self.engine_initialized?
defined? ::CoffeeScript
end
@@ -40,6 +44,7 @@ def prepare
if !options.key?(:bare) and !options.key?(:no_wrap)
options[:bare] = self.class.default_bare
end
+ options[:literate] ||= self.class.literate?
end
def evaluate(scope, locals, &block)
@@ -49,6 +54,12 @@ def evaluate(scope, locals, &block)
def allows_script?
false
end
+
+ class Literate < CoffeeScriptTemplate
+ def self.literate?
+ true
+ end
+ end
end
end
View
201 test/tilt_coffeescripttemplate_test.rb
@@ -4,108 +4,145 @@
begin
require 'coffee_script'
- class CoffeeScriptTemplateTest < Test::Unit::TestCase
-
- unless method_defined?(:assert_not_match)
- # assert_not_match is missing on 1.8.7, which uses assert_no_match
- def assert_not_match(a, b)
- unless a.kind_of?(Regexp)
- a = Regexp.new(Regexp.escape(a))
+ module CoffeeScriptTests
+ def self.included(mod)
+ mod.class_eval do
+ unless method_defined?(:assert_not_match)
+ # assert_not_match is missing on 1.8.7, which uses assert_no_match
+ def assert_not_match(a, b)
+ unless a.kind_of?(Regexp)
+ a = Regexp.new(Regexp.escape(a))
+ end
+ assert_no_match(a,b)
+ end
end
- assert_no_match(a,b)
- end
- end
- test "is registered for '.coffee' files" do
- assert_equal Tilt::CoffeeScriptTemplate, Tilt['test.coffee']
- end
+ test "bare is disabled by default" do
+ assert_equal false, @renderer.default_bare
+ end
- test "bare is disabled by default" do
- assert_equal false, Tilt::CoffeeScriptTemplate.default_bare
- end
+ test "compiles and evaluates the template on #render" do
+ template = @renderer.new { |t| @code_without_variables }
+ assert_match "puts('Hello, World!');", template.render
+ end
- test "compiles and evaluates the template on #render" do
- template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
- assert_match "puts('Hello, World!');", template.render
- end
+ test "can be rendered more than once" do
+ template = @renderer.new { |t| @code_without_variables }
+ 3.times { assert_match "puts('Hello, World!');", template.render }
+ end
- test "can be rendered more than once" do
- template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
- 3.times { assert_match "puts('Hello, World!');", template.render }
- end
+ test "disabling coffee-script wrapper" do
+ template = @renderer.new { @code_with_variables }
+ assert_match "(function() {", template.render
+ assert_match "puts(\"Hello, \" + name);\n", template.render
- test "disabling coffee-script wrapper" do
- str = 'name = "Josh"; puts "Hello #{name}"'
+ template = @renderer.new(:bare => true) { @code_with_variables }
+ assert_not_match "(function() {", template.render
+ assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render
- template = Tilt::CoffeeScriptTemplate.new { str }
- assert_match "(function() {", template.render
- assert_match "puts(\"Hello \" + name);\n", template.render
+ template2 = @renderer.new(:no_wrap => true) { @code_with_variables}
+ assert_not_match "(function() {", template.render
+ assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render
+ end
- template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
- assert_not_match "(function() {", template.render
- assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
+ context "wrapper globally enabled" do
+ setup do
+ @bare = @renderer.default_bare
+ @renderer.default_bare = false
+ end
+
+ teardown do
+ @renderer.default_bare = @bare
+ end
+
+ test "no options" do
+ template = @renderer.new { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_match "(function() {", template.render
+ end
+
+ test "overridden by :bare" do
+ template = @renderer.new(:bare => true) { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_not_match "(function() {", template.render
+ end
+
+ test "overridden by :no_wrap" do
+ template = @renderer.new(:no_wrap => true) { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_not_match "(function() {", template.render
+ end
+ end
- template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
- assert_not_match "(function() {", template.render
- assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
+ context "wrapper globally disabled" do
+ setup do
+ @bare = @renderer.default_bare
+ @renderer.default_bare = true
+ end
+
+ teardown do
+ @renderer.default_bare = @bare
+ end
+
+ test "no options" do
+ template = @renderer.new { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_not_match "(function() {", template.render
+ end
+
+ test "overridden by :bare" do
+ template = @renderer.new(:bare => false) { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_match "(function() {", template.render
+ end
+
+ test "overridden by :no_wrap" do
+ template = @renderer.new(:no_wrap => false) { |t| @code_with_variables }
+ assert_match "puts(\"Hello, \" + name);", template.render
+ assert_match "(function() {", template.render
+ end
+ end
+ end
end
+ end
- context "wrapper globally enabled" do
- setup do
- @bare = Tilt::CoffeeScriptTemplate.default_bare
- Tilt::CoffeeScriptTemplate.default_bare = false
- end
+ class CoffeeScriptTemplateTest < Test::Unit::TestCase
+ setup do
+ @code_without_variables = "puts 'Hello, World!'\n"
+ @code_with_variables = 'name = "Josh"; puts "Hello, #{name}"'
+ @renderer = Tilt::CoffeeScriptTemplate
+ end
- teardown do
- Tilt::CoffeeScriptTemplate.default_bare = @bare
- end
+ include CoffeeScriptTests
- test "no options" do
- template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_match "(function() {", template.render
- end
+ test "is registered for '.coffee' files" do
+ assert_equal @renderer, Tilt['test.coffee']
+ end
+ end
- test "overridden by :bare" do
- template = Tilt::CoffeeScriptTemplate.new(:bare => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_not_match "(function() {", template.render
- end
+ class LiterateCoffeeScriptTemplateTest < Test::Unit::TestCase
+ setup do
+ @code_without_variables = <<EOLIT
+This is some comment.
- test "overridden by :no_wrap" do
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_not_match "(function() {", template.render
- end
- end
+ puts 'Hello, World!'
- context "wrapper globally disabled" do
- setup do
- @bare = Tilt::CoffeeScriptTemplate.default_bare
- Tilt::CoffeeScriptTemplate.default_bare = true
- end
+This is a comment too.
+EOLIT
+ @code_with_variables = <<EOLIT
+This is some comment.
- teardown do
- Tilt::CoffeeScriptTemplate.default_bare = @bare
- end
+ name = "Josh"; puts "Hello, \#{name}"
- test "no options" do
- template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_not_match "(function() {", template.render
- end
+This is a comment too.
+EOLIT
+ @renderer = Tilt::CoffeeScriptTemplate::Literate
+ end
- test "overridden by :bare" do
- template = Tilt::CoffeeScriptTemplate.new(:bare => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_match "(function() {", template.render
- end
+ include CoffeeScriptTests
- test "overridden by :no_wrap" do
- template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
- assert_match "puts(\"Hello, \" + name);", template.render
- assert_match "(function() {", template.render
- end
+ test "is registered for '.litcoffee' files" do
+ assert_equal @renderer, Tilt['test.litcoffee']
end
end
View
1  tilt.gemspec
@@ -94,6 +94,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'bluecloth'
s.add_development_dependency 'builder'
s.add_development_dependency 'coffee-script'
+ s.add_development_dependency 'coffee-script-source', '>= 1.5.0'
s.add_development_dependency 'contest'
s.add_development_dependency 'creole'
s.add_development_dependency 'erubis'
Something went wrong with that request. Please try again.