Created
May 7, 2011 08:38
-
-
Save nzifnab/960329 to your computer and use it in GitHub Desktop.
Monkeypatch of Jammit to pre-compile using Haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Jammit | |
class Compressor | |
# Compiles a single JST file by writing out a javascript that adds | |
# template properties to a top-level template namespace object. Adds a | |
# JST-compilation function to the top of the package, unless you've | |
# specified your own preferred function, or turned it off. | |
# JST templates are named with the basename of their file. | |
# We are monkey-patching it so that we get Haml compilation *before* it hits the client. | |
def compile_jst(paths) | |
namespace = Jammit.template_namespace | |
paths = paths.grep(Jammit.template_extension_matcher).sort | |
base_path = find_base_path(paths) | |
compiled = paths.map do |path| | |
contents = read_binary_file(path) | |
#contents = contents.gsub(/\r?\n/, "\\n").gsub("'", '\\\\\'') | |
contents = Haml::Engine.new(contents).render.gsub(/\r?\n/, "\\n").gsub("'", '\\\\\'') | |
name = template_name(path, base_path) | |
"#{namespace}['#{name}'] = #{Jammit.template_function}('#{contents}');" | |
end | |
compiler = Jammit.include_jst_script ? read_binary_file(DEFAULT_JST_SCRIPT) : ''; | |
setup_namespace = "#{namespace} = #{namespace} || {};" | |
[JST_START, setup_namespace, compiler, compiled, JST_END].flatten.join("\n") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment