Skip to content

Instantly share code, notes, and snippets.

@nicohvi
Created November 7, 2014 08:48
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 nicohvi/ede1afbfb964d80723e4 to your computer and use it in GitHub Desktop.
Save nicohvi/ede1afbfb964d80723e4 to your computer and use it in GitHub Desktop.
Ruby script for compiling haml and coffeescript
require 'haml'
require 'coffee-script'
@root = Dir.pwd
@coffee = "#{@root}/_coffee"
@haml = "#{@root}/_haml"
def compile_coffeescript
target, javascript = "#{@root}/public/application.js", '' # 1
Dir.glob("#{@coffee}/*.coffee") do |coffeescript| # 2
javascript += File.open(coffeescript, 'r') do |file|
CoffeeScript.compile file.read # 3
end
end
File.open(target, 'w') { |file| file.write(javascript) } # 4
end
def compile_haml(file) # 1
file_name = "#{File.basename(file, '.haml')}.html" # 2
html = File.open(file, 'r') { |file| Haml::Engine.new(file.read).render } # 3
path = "#{@root}/views/#{file_name}"
File.open(path, 'w') { |file| file.write(html) } # 4
end
# Do the actual compilations
compile_coffeescript
Dir.glob("#{@haml}/.*haml").each { |file| compile_haml(haml) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment