Created
September 5, 2011 07:57
-
-
Save tlync/1194357 to your computer and use it in GitHub Desktop.
Haml to html in realtime
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
// usege: node haml2html <file> | |
var sys = require('util'), | |
fs = require('fs'), | |
http = require('http'), | |
exec = require('child_process').exec | |
var argv = process.argv, | |
target = argv[2] | |
if(!target){ | |
console.error('usage: ' + argv.join(' ') + ' <filename>') | |
process.exit(1) | |
} | |
monitor(target, function(cur, prev){ | |
haml2html(target) | |
}); | |
/** | |
* Monitor a file changes | |
* @param {String} file path of html file | |
* @param {Function} callback callback when file was changed | |
*/ | |
function monitor(file, callback) { | |
fs.stat(file, function(e, stat){ | |
if(e){ throw e } | |
if(!stat.isFile()) { | |
console.error(file + ' is not file.') | |
process.exit(1) | |
} | |
fs.watchFile(file, { interval: 500 }, callback) | |
}) | |
} | |
/** | |
* Convert to haml to html using ruby haml | |
* NOTE: gem install haml | |
* @param {String} file target html file path | |
*/ | |
function haml2html(file) { | |
var output = target.replace('.haml', '.html'), | |
cmd = 'haml -f html5 --trace ' + file + ' ' + output | |
exec(cmd, function(e, stdout, stderr) { | |
if(e){ console.error(e) } | |
console.log('updated ', output) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment