Skip to content

Instantly share code, notes, and snippets.

@DamonOehlman
Created March 10, 2011 05:47
Show Gist options
  • Save DamonOehlman/863632 to your computer and use it in GitHub Desktop.
Save DamonOehlman/863632 to your computer and use it in GitHub Desktop.
Rake task that will combine a number of HTML (or Handlebars) files into an object literal in a single JS file.
task :resources => [] do
resources = {}
basepath = 'src/resources/'
files = FileList.new
.include("%s**/*.html" % basepath)
.include("%s**/*.handlebars" % basepath)
.sub(basepath, '')
files.each do |src|
File.open(basepath + src, 'r') do |resfile|
resid = src.gsub(/.(html|handlebars)/i, '')
# update the resource in the resources list
resources[resid] = resfile.read().gsub(/[\n\r\t]/, '').gsub(/\'/, '"')
end
end
File.open("src/resources.js", "w") do |resjs|
counter = 0
resjs.write("var htmlResources = {\n")
resources.each do |k, v|
if (counter > 0)
resjs.write(",\n")
end
resjs.write(" '%s': '%s'" % [k, v])
counter += 1
end
resjs.write("\n};\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment