Skip to content

Instantly share code, notes, and snippets.

@leepfrog
Last active December 10, 2015 16:58
Show Gist options
  • Select an option

  • Save leepfrog/4464513 to your computer and use it in GitHub Desktop.

Select an option

Save leepfrog/4464513 to your computer and use it in GitHub Desktop.
Class extension to convert hamlbars to handlebars.
class File
def to_str
path
end
end
class Hamlbars::Converter
def self.process_directory(directory)
dir = Dir.open(directory)
entries = dir.entries
# Shift off . and ..
entries.delete(".")
entries.delete("..")
# For each file in this directory that ends in hbs, convert it to .hbs
# if it's a directory, call the same method
entries.each do |entry|
fullPath = "#{directory}/#{entry}"
if File.directory?(fullPath)
process_directory(fullPath)
elsif File.file?(fullPath)
process_file(fullPath)
end
end
end
def self.process_file(filePath)
if filePath.match(/(.+).hamlbars/)
fileName = $1
readFile = File.open(filePath, "r")
compiledHbs = Hamlbars::Template.new(readFile, :format => :xhtml).render
writeFile = File.open("#{fileName}.hbs", 'w')
writeFile.write(compiledHbs)
writeFile.close
readFile.close
end
end
end

Class extension to convert hamlbars to handlebars.

  • This requires Hamlbars 2.0
  • Use at your own risk
  • Writes an .hbs file for every .hamlbars file
  • Does not handle exceptions / invalid files
  • Blindly deletes existing files

Usage:

Hamlbars::Converter.process_directory(pathName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment