Skip to content
This repository

Literate CoffeeScript support #173

Open
wants to merge 5 commits into from

4 participants

Sebastian Skałacki Magnus Holm Trevor Bortins Josh Bassett
Sebastian Skałacki

Literate CoffeeScript has been introduced in version 1.5.0 of CoffeeScript.

Magnus Holm
Collaborator

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

Sebastian Skałacki

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
Trevor Bortins

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.

Trevor Bortins wulftone referenced this pull request in sstephenson/sprockets March 14, 2013
Closed

Support for litcoffee, Literate coffee-script? #420

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

@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
Josh Bassett

@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

Showing 5 unique commits by 1 author.

Mar 20, 2013
Sebastian Skałacki Cleanup CoffeeScript tests
Make them DRY before introducing tests for literate CoffeeScript
handler, extract template renderer class to instance variable.
f71e617
Sebastian Skałacki Most of CoffeeScript tests extracted to module.
They will be shared with literate CoffeeScript tests.
bdd46ab
Sebastian Skałacki Literate CoffeeScript renderer. f0db94c
Sebastian Skałacki Literate CoffeeScript in TEMPLATES & README. 9acb11d
Sebastian Skałacki Development dependency for literate CoffeeScript. f3b4556
This page is out of date. Refresh to see the latest.
1  README.md
Source Rendered
@@ -44,6 +44,7 @@ Support for these template engines is included with the package:
44 44
     Markaby                    .mab                    markaby
45 45
     Nokogiri                   .nokogiri               nokogiri
46 46
     CoffeeScript               .coffee                 coffee-script (+ javascript)
  47
+    CoffeeScript (literate)    .litcoffee              coffee-script (>= 1.5.0) + javascript
47 48
     Creole (Wiki markup)       .wiki, .creole          creole
48 49
     WikiCloth (Wiki markup)    .wiki, .mediawiki, .mw  wikicloth
49 50
     Yajl                       .yajl                   yajl-ruby
1  TEMPLATES.md
Source Rendered
@@ -31,6 +31,7 @@ Tilt also includes support for CSS processors like [LessCSS][lesscss] and
31 31
  * Sass - `Tilt::SassTemplate`
32 32
  * Scss - `Tilt::ScssTemplate`
33 33
  * CoffeeScript - `Tilt::CoffeeScriptTemplate`
  34
+ * Literate CoffeeScript - `Tilt::CoffeeScriptTemplate::Literate`
34 35
  * [Textile](#redcloth) - `Tilt::RedClothTemplate`
35 36
  * Creole - `Tilt::CreoleTemplate`
36 37
  * [RDoc](#rdoc) - `Tilt::RDocTemplate`
3  lib/tilt.rb
@@ -157,7 +157,8 @@ def clear
157 157
   register CSVTemplate, 'rcsv'
158 158
 
159 159
   require 'tilt/coffee'
160  
-  register CoffeeScriptTemplate, 'coffee'
  160
+  register CoffeeScriptTemplate,            'coffee'
  161
+  register CoffeeScriptTemplate::Literate,  'litcoffee'
161 162
 
162 163
   require 'tilt/nokogiri'
163 164
   register NokogiriTemplate, 'nokogiri'
11  lib/tilt/coffee.rb
@@ -28,6 +28,10 @@ def self.default_no_wrap=(value)
28 28
       @@default_bare = value
29 29
     end
30 30
 
  31
+    def self.literate?
  32
+      false
  33
+    end
  34
+
31 35
     def self.engine_initialized?
32 36
       defined? ::CoffeeScript
33 37
     end
@@ -40,6 +44,7 @@ def prepare
40 44
       if !options.key?(:bare) and !options.key?(:no_wrap)
41 45
         options[:bare] = self.class.default_bare
42 46
       end
  47
+      options[:literate] ||= self.class.literate?
43 48
     end
44 49
 
45 50
     def evaluate(scope, locals, &block)
@@ -49,6 +54,12 @@ def evaluate(scope, locals, &block)
49 54
     def allows_script?
50 55
       false
51 56
     end
  57
+
  58
+    class Literate < CoffeeScriptTemplate
  59
+      def self.literate?
  60
+        true
  61
+      end
  62
+    end
52 63
   end
53 64
 end
54 65
 
201  test/tilt_coffeescripttemplate_test.rb
@@ -4,108 +4,145 @@
4 4
 begin
5 5
   require 'coffee_script'
6 6
 
7  
-  class CoffeeScriptTemplateTest < Test::Unit::TestCase
8  
-
9  
-    unless method_defined?(:assert_not_match)
10  
-      # assert_not_match is missing on 1.8.7, which uses assert_no_match
11  
-      def assert_not_match(a, b)
12  
-        unless a.kind_of?(Regexp)
13  
-          a = Regexp.new(Regexp.escape(a))
  7
+  module CoffeeScriptTests
  8
+    def self.included(mod)
  9
+      mod.class_eval do
  10
+        unless method_defined?(:assert_not_match)
  11
+          # assert_not_match is missing on 1.8.7, which uses assert_no_match
  12
+          def assert_not_match(a, b)
  13
+            unless a.kind_of?(Regexp)
  14
+              a = Regexp.new(Regexp.escape(a))
  15
+            end
  16
+            assert_no_match(a,b)
  17
+          end
14 18
         end
15  
-        assert_no_match(a,b)
16  
-      end
17  
-    end
18 19
 
19  
-    test "is registered for '.coffee' files" do
20  
-      assert_equal Tilt::CoffeeScriptTemplate, Tilt['test.coffee']
21  
-    end
  20
+        test "bare is disabled by default" do
  21
+          assert_equal false, @renderer.default_bare
  22
+        end
22 23
 
23  
-    test "bare is disabled by default" do
24  
-      assert_equal false, Tilt::CoffeeScriptTemplate.default_bare
25  
-    end
  24
+        test "compiles and evaluates the template on #render" do
  25
+          template = @renderer.new { |t| @code_without_variables }
  26
+          assert_match "puts('Hello, World!');", template.render
  27
+        end
26 28
 
27  
-    test "compiles and evaluates the template on #render" do
28  
-      template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
29  
-      assert_match "puts('Hello, World!');", template.render
30  
-    end
  29
+        test "can be rendered more than once" do
  30
+          template = @renderer.new { |t| @code_without_variables }
  31
+          3.times { assert_match "puts('Hello, World!');", template.render }
  32
+        end
31 33
 
32  
-    test "can be rendered more than once" do
33  
-      template = Tilt::CoffeeScriptTemplate.new { |t| "puts 'Hello, World!'\n" }
34  
-      3.times { assert_match "puts('Hello, World!');", template.render }
35  
-    end
  34
+        test "disabling coffee-script wrapper" do
  35
+          template = @renderer.new { @code_with_variables }
  36
+          assert_match "(function() {", template.render
  37
+          assert_match "puts(\"Hello, \" + name);\n", template.render
36 38
 
37  
-    test "disabling coffee-script wrapper" do
38  
-      str = 'name = "Josh"; puts "Hello #{name}"'
  39
+          template = @renderer.new(:bare => true) { @code_with_variables }
  40
+          assert_not_match "(function() {", template.render
  41
+          assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render
39 42
 
40  
-      template = Tilt::CoffeeScriptTemplate.new { str }
41  
-      assert_match "(function() {", template.render
42  
-      assert_match "puts(\"Hello \" + name);\n", template.render
  43
+          template2 = @renderer.new(:no_wrap => true) { @code_with_variables}
  44
+          assert_not_match "(function() {", template.render
  45
+          assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello, \" + name);\n", template.render
  46
+        end
43 47
 
44  
-      template = Tilt::CoffeeScriptTemplate.new(:bare => true) { str }
45  
-      assert_not_match "(function() {", template.render
46  
-      assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
  48
+        context "wrapper globally enabled" do
  49
+          setup do
  50
+            @bare = @renderer.default_bare
  51
+            @renderer.default_bare = false
  52
+          end
  53
+
  54
+          teardown do
  55
+            @renderer.default_bare = @bare
  56
+          end
  57
+
  58
+          test "no options" do
  59
+            template = @renderer.new { |t| @code_with_variables }
  60
+            assert_match "puts(\"Hello, \" + name);", template.render
  61
+            assert_match "(function() {", template.render
  62
+          end
  63
+
  64
+          test "overridden by :bare" do
  65
+            template = @renderer.new(:bare => true) { |t| @code_with_variables }
  66
+            assert_match "puts(\"Hello, \" + name);", template.render
  67
+            assert_not_match "(function() {", template.render
  68
+          end
  69
+
  70
+          test "overridden by :no_wrap" do
  71
+            template = @renderer.new(:no_wrap => true) { |t| @code_with_variables }
  72
+            assert_match "puts(\"Hello, \" + name);", template.render
  73
+            assert_not_match "(function() {", template.render
  74
+          end
  75
+        end
47 76
 
48  
-      template2 = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { str}
49  
-      assert_not_match "(function() {", template.render
50  
-      assert_equal "var name;\n\nname = \"Josh\";\n\nputs(\"Hello \" + name);\n", template.render
  77
+        context "wrapper globally disabled" do
  78
+          setup do
  79
+            @bare = @renderer.default_bare
  80
+            @renderer.default_bare = true
  81
+          end
  82
+
  83
+          teardown do
  84
+            @renderer.default_bare = @bare
  85
+          end
  86
+
  87
+          test "no options" do
  88
+            template = @renderer.new { |t| @code_with_variables }
  89
+            assert_match "puts(\"Hello, \" + name);", template.render
  90
+            assert_not_match "(function() {", template.render
  91
+          end
  92
+
  93
+          test "overridden by :bare" do
  94
+            template = @renderer.new(:bare => false) { |t| @code_with_variables }
  95
+            assert_match "puts(\"Hello, \" + name);", template.render
  96
+            assert_match "(function() {", template.render
  97
+          end
  98
+
  99
+          test "overridden by :no_wrap" do
  100
+            template = @renderer.new(:no_wrap => false) { |t| @code_with_variables }
  101
+            assert_match "puts(\"Hello, \" + name);", template.render
  102
+            assert_match "(function() {", template.render
  103
+          end
  104
+        end
  105
+      end
51 106
     end
  107
+  end
52 108
 
53  
-    context "wrapper globally enabled" do
54  
-      setup do
55  
-        @bare = Tilt::CoffeeScriptTemplate.default_bare
56  
-        Tilt::CoffeeScriptTemplate.default_bare = false
57  
-      end
  109
+  class CoffeeScriptTemplateTest < Test::Unit::TestCase
  110
+    setup do
  111
+      @code_without_variables = "puts 'Hello, World!'\n"
  112
+      @code_with_variables = 'name = "Josh"; puts "Hello, #{name}"'
  113
+      @renderer = Tilt::CoffeeScriptTemplate
  114
+    end
58 115
 
59  
-      teardown do
60  
-        Tilt::CoffeeScriptTemplate.default_bare = @bare
61  
-      end
  116
+    include CoffeeScriptTests
62 117
 
63  
-      test "no options" do
64  
-        template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
65  
-        assert_match "puts(\"Hello, \" + name);", template.render
66  
-        assert_match "(function() {", template.render
67  
-      end
  118
+    test "is registered for '.coffee' files" do
  119
+      assert_equal @renderer, Tilt['test.coffee']
  120
+    end
  121
+  end
68 122
 
69  
-      test "overridden by :bare" do
70  
-        template = Tilt::CoffeeScriptTemplate.new(:bare => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
71  
-        assert_match "puts(\"Hello, \" + name);", template.render
72  
-        assert_not_match "(function() {", template.render
73  
-      end
  123
+  class LiterateCoffeeScriptTemplateTest < Test::Unit::TestCase
  124
+    setup do
  125
+      @code_without_variables = <<EOLIT
  126
+This is some comment.
74 127
 
75  
-      test "overridden by :no_wrap" do
76  
-        template = Tilt::CoffeeScriptTemplate.new(:no_wrap => true) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
77  
-        assert_match "puts(\"Hello, \" + name);", template.render
78  
-        assert_not_match "(function() {", template.render
79  
-      end
80  
-    end
  128
+    puts 'Hello, World!'
81 129
 
82  
-    context "wrapper globally disabled" do
83  
-      setup do
84  
-        @bare = Tilt::CoffeeScriptTemplate.default_bare
85  
-        Tilt::CoffeeScriptTemplate.default_bare = true
86  
-      end
  130
+This is a comment too.
  131
+EOLIT
  132
+      @code_with_variables = <<EOLIT
  133
+This is some comment.
87 134
 
88  
-      teardown do
89  
-        Tilt::CoffeeScriptTemplate.default_bare = @bare
90  
-      end
  135
+    name = "Josh"; puts "Hello, \#{name}"
91 136
 
92  
-      test "no options" do
93  
-        template = Tilt::CoffeeScriptTemplate.new { |t| 'name = "Josh"; puts "Hello, #{name}"' }
94  
-        assert_match "puts(\"Hello, \" + name);", template.render
95  
-        assert_not_match "(function() {", template.render
96  
-      end
  137
+This is a comment too.
  138
+EOLIT
  139
+      @renderer = Tilt::CoffeeScriptTemplate::Literate
  140
+    end
97 141
 
98  
-      test "overridden by :bare" do
99  
-        template = Tilt::CoffeeScriptTemplate.new(:bare => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
100  
-        assert_match "puts(\"Hello, \" + name);", template.render
101  
-        assert_match "(function() {", template.render
102  
-      end
  142
+  include CoffeeScriptTests
103 143
 
104  
-      test "overridden by :no_wrap" do
105  
-        template = Tilt::CoffeeScriptTemplate.new(:no_wrap => false) { |t| 'name = "Josh"; puts "Hello, #{name}"' }
106  
-        assert_match "puts(\"Hello, \" + name);", template.render
107  
-        assert_match "(function() {", template.render
108  
-      end
  144
+    test "is registered for '.litcoffee' files" do
  145
+      assert_equal @renderer, Tilt['test.litcoffee']
109 146
     end
110 147
   end
111 148
 
1  tilt.gemspec
@@ -94,6 +94,7 @@ Gem::Specification.new do |s|
94 94
   s.add_development_dependency 'bluecloth'
95 95
   s.add_development_dependency 'builder'
96 96
   s.add_development_dependency 'coffee-script'
  97
+  s.add_development_dependency 'coffee-script-source', '>= 1.5.0'
97 98
   s.add_development_dependency 'contest'
98 99
   s.add_development_dependency 'creole'
99 100
   s.add_development_dependency 'erubis'
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.