Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input and Options update #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 60 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,90 @@
# gulp-ruby-haml
# gulp-ziey-ruby-haml

This is a [gulp](http://gulpjs.com/) plugin that will use the `haml` command
line script to compile your Haml files into HTML. You need both Ruby and Haml
installed to use this. Try `gem install haml`. If you use
[Bundler](http://bundler.io/), add `gem 'haml'` to your Gemfile and run
`bundle install`.
## Update( 2014-07-04 )
There is an Push Request in Apr 23th in [gulp-ruby-haml](https://github.com/mongorian-chop/gulp-ruby-haml)
and nither be accessed nor be close.

Well, let's change it to gulp-ziey-ruby-haml...

## Update( 2014-07-03 from [zemzheng](https://github.com/zemzheng) )

* Options

Same as haml -h:

-s, --stdin Read input from standard input instead of an input file
--trace Show a full traceback on error
--unix-newlines Use Unix-style newlines in written files.
-c, --check Just check syntax, don't evaluate.
-t, --style NAME Output style. Can be indented (default) or ugly.
-f, --format NAME Output format. Can be html5 (default), xhtml, or html4.
-e, --escape-html Escape HTML characters (like ampersands and angle brackets) by default.
--no-escape-attrs Don't escape HTML characters (like ampersands and angle brackets) in attributes.
-q, --double-quote-attributes Set attribute wrapper to double-quotes (default is single).
--cdata Always add CDATA sections to javascript and css blocks.
--autoclose LIST Comma separated list of elements to be automatically self-closed.
--suppress-eval Don't evaluate Ruby scripts.
-r, --require FILE Same as 'ruby -r'.
-I, --load-path PATH Same as 'ruby -I'.
-E ex[:in] Specify the default external and internal character encodings.
-d, --debug Print out the precompiled Ruby source.
-p, --parse Print out Haml parse tree.
-?, -h, --help Show this message
-v, --version Print version

* Stream Accessable, so you can use:

gulp.src( '*' )
.pipe( other() )
.pipe( haml() )
...


## Options

Pass `{doubleQuote: true}` to use `"` around HTML attributes instead of `'`.
This uses the `-q`/`--double-quote-attributes` option with `haml`.

## gulpfile.js example

var gulp = require('gulp');
var watch = require('gulp-watch');
var haml = require('gulp-ruby-haml');

// Compile Haml into HTML
gulp.task('haml', function() {
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(haml()).
pipe(gulp.dest('./public'));
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(haml()).
pipe(gulp.dest('./public'));
});

// Compile Haml into HTML with double quotes around attributes
// Same as haml -q
gulp.task('haml-double-quote', function() {
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(haml({doubleQuote: true})).
pipe(gulp.dest('./public'));
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(
haml({
'-q' : true,
'--no-escape-attrs' : true
})
).
pipe(gulp.dest('./public'));
});

// Watch for changes in Haml files
gulp.task('haml-watch', function() {
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(watch()).
pipe(haml()).
pipe(gulp.dest('./public'));
gulp.src('./app/assets/haml/**/*.haml', {read: false}).
pipe(watch()).
pipe(haml()).
pipe(gulp.dest('./public'));
});


## Thanks

This largely came from [gulp-ruby-sass](https://github.com/sindresorhus/gulp-ruby-sass) by Sindre Sorhus.
158 changes: 108 additions & 50 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,126 @@
var spawn = require('win-spawn');
var gutil = require('gulp-util');
var Buffer = require('buffer').Buffer;
var spawn = require('win-spawn');
var gutil = require('gulp-util');
var Buffer = require('buffer').Buffer;
var PluginError = gutil.PluginError;
var clone = require('clone');
var through = require('through2');
var through = require('through2');
var os = require( 'os' );
var fs = require( 'fs' );
var path = require( 'path' );

const PLUGIN_NAME = 'gulp-ruby-haml';

module.exports = function (opt) {
function modifyFile(file, enc, callback) {
if (file.isNull()) {
return callback(null, file);
}

if (file.isStream()) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return callback(null, file);
}

opt = opt || {};
var options = {};
options.outExtension = opt.outExtension || '.html';
options.doubleQuote = opt.doubleQuote || false;

var str = file.contents.toString('utf8');
var args = ['haml'];
if (options.doubleQuote) {
args.push('-q');
}
args.push(file.path);
var cp = spawn(args.shift(), args);
var haml = function( args, file, self, callback ){
gutil.log(
'Haml Input',
gutil.colors.yellow( file.path || file.contents.toString() )
);
var cp = spawn( 'haml ' + args.join( ' ' ) );
var tmp_file_path = args[ args.length -1 ];

var self = this;
cp.on('error', function (err) {
self.emit('error', new PluginError(PLUGIN_NAME, err));
return callback(null, file);
self.emit('error', new PluginError(PLUGIN_NAME, err));
return callback(null, file);
});

var haml_data = '';
cp.stdout.on('data', function (data) { haml_data += data.toString(); });

var errors = '';

cp.stderr.setEncoding('utf8');
cp.stderr.on('data', function (data) { errors += data.toString(); });
cp.stdout.setEncoding('utf8');

cp.stdout.on('data', function (data) { haml_data += data.toString(); });
cp.stderr.on('data', function (data) {
var str = data.toString()
gutil.log(
gutil.colors.bgRed( 'Haml Error' ),
JSON.stringify( args )
);
gutil.log(
gutil.colors.bgRed( str )
);
errors += str;
});

cp.on('close', function (code) {
if (errors) {
self.emit('error', new PluginError(PLUGIN_NAME, errors));
return callback(null, null);
}
if (errors) {
self.emit('error', new PluginError(PLUGIN_NAME, errors));
return callback(null, null);
}
fs.unlink( tmp_file_path );

if (code > 0) {
self.emit('error', new PluginError(PLUGIN_NAME,
'Exited with error code ' + code));
return callback(null, null);
}
if (code > 0) {
self.emit(
'error',
new PluginError(PLUGIN_NAME, 'Exited with error code ' + code)
);
return callback(null, null);
}

var newFile = clone(file);
newFile.path = gutil.replaceExtension(file.path, options.outExtension);
newFile.contents = new Buffer(haml_data);
return callback(null, newFile);
file.contents = new Buffer(haml_data);
return callback(null, file);
});
}
};

var makeArgsByOptions = function( options ){
var key, val, result = [];
for( key in options ){
val = options[ key ];
if( false === val ){
continue
} else if( true === val ){
val = undefined;
}
result.push( key, val )
}
return result;
}
var extend = function( obj1, obj2 ){
var key;
for( key in obj2 ){
obj1[ key ] = obj2[ key ];
}
return obj1;
}
module.exports = function( options ) {
function modifyFile(file, enc, callback) {
if (file.isNull()) { return callback(null, file); }

if (file.isStream()) {
this.emit(
'error',
new PluginError(PLUGIN_NAME, 'Streaming not supported')
);
return callback(null, file);
}

var args = makeArgsByOptions( extend(
{ '-E' : 'utf-8' },
options || {}
) );


var self = this;

if( file.contents ){
var file_path = path.join(
os.tmpdir(), path.basename( file.path ) + ( +new Date )
);
args.push( file_path );
fs.writeFile( file_path, file.contents.toString( 'utf8' ), function( err ){
if( err ){
self.emit( 'error', err );
return callback( null, file );
}
haml( args, file, self, callback );
} );

} else if( file.path ){
args.push( file.path );
haml( args, file, self, callback );
}


}

return through.obj(modifyFile);
return through.obj(modifyFile);
};
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"name": "gulp-ruby-haml",
"version": "0.0.3",
"name": "gulp-ziey-ruby-haml",
"version": "0.0.5",
"description": "Compile Haml to HTML with Ruby Haml",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/moneypenny/gulp-ruby-haml.git"
},
"author": {
"name": "Sarah Vessels",
"email": "cheshire137@gmail.com",
"url": "http://www.sarah-vessels.com"
"author": {
"name" : "Zem Zheng",
"email" : "zemzheng@gmail.com",
"url" : "https://github.com/zemzheng"
},
"contributors" : [
{
"name": "Sarah Vessels",
"email": "[email protected]",
"url": "http://www.sarah-vessels.com"
}

],
"engines": {
"node": ">=0.10.0"
},
Expand All @@ -33,8 +41,7 @@
"dependencies": {
"gulp-util": "~2.2.0",
"through2": "~0.4.0",
"win-spawn": "~2.0.0",
"clone": "~0.1.11"
"win-spawn": "~2.0.0"
},
"devDependencies": {
"gulp": "~3.5.2",
Expand Down
3 changes: 0 additions & 3 deletions test/fixtures/basic.haml

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/invalid.haml

This file was deleted.

2 changes: 2 additions & 0 deletions test/input.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%div#id.class
%a( href="#" ) innerHTML
Loading