How can I auto compile my HAML files into HTML files in a tiny project that doesn't run on RoR?

北战南征 提交于 2019-12-04 01:44:59
Jannis

Thank you both @Jacob and @Jonathan, I ultimately ended up using neither of your approaches in favour of using middleman, hence the answer to my own question.

For those reading this topic having a similar question in mind, the reason I like middleman so much is that it effectively combines my entire workflow into 1 mini-server app. Using mm-ini project_name and then mm-server in the directory I instantly have access to Compass, HAML and SASS all with the option of simply outputting it to plain html at any given time.

Here is more info about middleman : http://middlemanapp.com/

Staticmatic and Nanoc also do HAML but as far as I could find out they do not 'out of the box' support Compass (SASS) compilation, which for some might be an upside but for me wasn't.

Again, thanks for your answers, here is however the answer that I ultimately chose to follow.

If you have Ruby installed you could use the watchr gem.

With the help of a little nice script that I found here you can start a process that recognizes any changes to your haml file.

Beneath you can find my customized watchr.rb

def compile_haml
  %x[haml index.haml index.html]
end

def do_growl(message)
  growlnotify = `which growlnotify`.chomp
  title = "Watchr Message"
  passed = message.include?('0 failures, 0 errors')
  image = passed ? "~/.watchr_images/passed.png" : "~/.watchr_images/failed.png"
  severity = passed ? "-1" : "1"
  options = "-w -n Watchr --image '#{File.expand_path(image)}'"
  options << " -m '#{message}' '#{title}' -p #{severity}"
  system %(#{growlnotify} #{options} &)
end

do_growl "Watching folders and waiting for changes..."

watch(".*\.haml$") { |x|
  compile_haml
  do_growl "Compiled HAML!"
}

If you do not have growl installed just leave that part away

I've found StaticMatic to be really good for building static web sites in HAML.

Maybee a bit more manual that you'd like, but you could always install the fs-events gem and do something along the lines of

require 'rb-fsevent'
require "open3"

include Open3

fsevent = FSEvent.new
fsevent.watch Dir.pwd do |directories|
  puts "Detected change inside: #{directories.inspect}"
  popen3('haml',
         '..parameters..',
         '..parameters..') do |stdin, stdout, stderr|
    stdout.read.split("\n").each do |line|
      puts line
    end
  end
end
fsevent.run

using values in the directoriesobject to call the hamlexecutable on changed files.

Although you've apparently found what you were looking for I'll still post another approach because middleman might not be the perfect solution for everyone. My approach uses Rake. I've written a simple rakefile including a 'watch' task that recompiles my sass (or compass) and haml files whenever a file changes. Plus it reloads the browser preview :) (I don't know if middleman can do that).

The rakefile is on github: https://gist.github.com/1635301/

Codekit is what I use, it's fantastic and handles SASS, Compass, HAML, as well as many other things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!