Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Created November 6, 2012 19:17
Show Gist options
  • Save zhangyuan/4026842 to your computer and use it in GitHub Desktop.
Save zhangyuan/4026842 to your computer and use it in GitHub Desktop.
Generate html pages from Slim templates
require 'fileutils'
task :default => [:generate]
# put Slim templates under templates/ directory. run `rake generate` to generate html pages into public/ directory.
desc 'Generate html pages from Slim templates'
task :generate do
base_path = File.dirname(__FILE__)
templates_path = File.join(base_path, 'templates')
public_dir = File.join(base_path, 'public')
Dir.glob("#{templates_path}/**/*.slim").each do |path|
output_path = File.join(public_dir, path[templates_path.length..-6])
FileUtils.mkdir_p(File.dirname(output_path))
File.open(output_path, 'w') do |file|
file.puts(Slim::Template.new(path,
:pretty => true,
:sections => true) { File.open(path, 'r').read }.render)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment