Skip to content

Instantly share code, notes, and snippets.

@sukima
Forked from phaer/coffeescript_converter.rb
Created June 13, 2012 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sukima/2925325 to your computer and use it in GitHub Desktop.
Save sukima/2925325 to your computer and use it in GitHub Desktop.
A trivial CoffeeScript.org -> Javascript plugin for OLDER OctoPress. Put this file in 'plugins/' and write a YAML header to your .coffee files (i.e. "---\nlayout:nil\n---\n")
module Jekyll
require 'coffee-script'
class CoffeeScriptConverter < Converter
safe true
priority :normal
def matches(ext)
ext =~ /coffee/i
end
def output_ext(ext)
".js"
end
def convert(content)
begin
content = CoffeeScript.compile content
# OctoPress will pass all content though a RubyPants filter.
# RubyPants will conver quotes to smart quotes.
# RubyPants will ignore any convertions when content is surrounded by HTML <script> tags
# This adds HTML <script> tags to the output to prevent RubyPants from processing the JavaScript code.
<<EOS
// <script> To prevent OctoPress filtering through RubyPants ([See Gist][1])
#{content}
// [1]: https://gist.github.com/2925325
// </script>
EOS
rescue StandardError => e
puts "CoffeeScript error:" + e.message
end
end
end
end
@sukima
Copy link
Author

sukima commented Jun 14, 2012

HTML comments will fail if you have a '>' character in your source.

// <!-- ...
alert("barfoo");
if (blah > 0) {
    alert(&#8220;foobar&#8221;);
}
// &#8211;>

Use <script> tags instead. Be careful as the same rules apply as they do for embeded JavaScript inside HTML documents. You can not use the string </script> or you'll turn on RubyPants processing again. Instead escape this with "<"+"/"+"script>" (normal CoffeeScript string concatenation will not work here you must explicitly use the + operator to separate the characters and prevent parsing).

EDIT: Source patched to use <script> tags.

@sukima
Copy link
Author

sukima commented Jun 14, 2012

This Hack is no longer required for latest OctoPress. Use this gist instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment