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

Bugfix. OS independent path detection #3

Open
wants to merge 9 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
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# gulp-dot-precompiler
# gulp-dot-precompiler2

Better [Gulp](https://github.com/gulpjs/gulp) plugin for precompilation of [doT](https://github.com/olado/doT) templates. Forked from [titarenko/gulp-dotify](https://github.com/titarenko/gulp-dotify) but with more control and much more logical.
Better [Gulp](https://github.com/gulpjs/gulp) plugin for precompilation of [doT](https://github.com/olado/doT) templates. Forked from [kentliau/gulp-dot-precompiler](https://github.com/kentliau/gulp-dot-precompiler) with more control and bugfixes.

## Status

[![Build Status](https://secure.travis-ci.org/kentliau/gulp-dot-precompiler.png?branch=master)](https://travis-ci.org/kentliau/gulp-dot-precompiler)
[![NPM version](https://badge.fury.io/js/gulp-dot-precompiler.png)](http://badge.fury.io/js/gulp-dot-precompiler)
[![Coverage Status](https://coveralls.io/repos/kentliau/gulp-dot-precompiler/badge.png)](https://coveralls.io/r/kentliau/gulp-dot-precompiler)
[![Dependecy Status](https://david-dm.org/kentliau/gulp-dot-precompiler.png)](https://david-dm.org/kentliau/gulp-dot-precompiler.png)


[![NPM](https://nodei.co/npm/gulp-dot-precompiler.png?downloads=true&stars=true)](https://nodei.co/npm/gulp-dot-precompiler/)
[![NPM](https://nodei.co/npm/gulp-dot-precompiler2.png?downloads=true&stars=true)](https://nodei.co/npm/gulp-dot-precompiler2/)


## Options
Expand Down Expand Up @@ -80,7 +74,7 @@ app/views/layout.dot
Then, running this code:

```js
var dot = require('gulp-dot-precompiler'),
var dot = require('gulp-dot-precompiler2'),
concat = require('gulp-concat'), // npm install gulp-concat --save
header = require('gulp-header'); // npm install gulp-header --save

Expand All @@ -104,12 +98,6 @@ render['layout'] = function ...
```


##Todo

- [] rename all the options to be more self-descriptive
- [x] add dot delimiter options @caseyWebb
- [] allow loadfile() using object oriented way of parameter, just like laravel

## License

MIT
199 changes: 107 additions & 92 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,124 @@
var through = require('through2');
var dot = require('dot');
var gutil = require('gulp-util');
var _ = require('lodash');
var dot = require('dot');
var path = require('path');
var PluginError = gutil.PluginError;
var fs = require('fs');
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var defs = {};

const PLUGIN_NAME = 'gulp-dot-precompiler';
const PLUGIN_NAME = 'gulp-dot-precompiler2';

function getTemplateName(root, name, extension, separator) {
var parts = name.split(path.sep);
if(root.length !== 0 )
{
parts.unshift(root);
}
if(extension.length !== 0)
{
parts[parts.length-1] = parts[parts.length-1] + extension;
}
return parts.join(separator);
var parts = name.split(path.sep);

if (root.length !== 0) {
parts.unshift(root);
}

if (extension.length !== 0) {
parts[parts.length - 1] = parts[parts.length - 1] + extension;
}

return parts.join(separator);
}

function getTemplateCode(content,templateSettings,defs) {
var compiled;
try {
compiled = dot.template(content,templateSettings,defs).toString();
} catch(err){
console.log(err);
return err;
}
function getTemplateCode(content, templateSettings, defs) {
var compiled;

try {
compiled = dot.template(content, templateSettings, defs).toString();
} catch (err) {
console.log(err);
return err;
}

return compiled;
return compiled;
}

function readStream(stream, done) {
var buffer = '';
stream.on('data', function (chunk) {
buffer += chunk;
}).on('end', function () {
done(null, buffer);
}).on('error', function (error) {
done(error);
});
var buffer = '';

stream.on('data', function (chunk) {
buffer += chunk;
}).on('end', function () {
done(null, buffer);
}).on('error', function (error) {
done(error);
});
}

function gulpDotify(options) {
options = options || {};
_.defaults(options, {
root: '',
separator: '.',
extension: '',
dictionary: 'render',

//doT.js setting
templateSettings: {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
defineParams: /^\s*([\w$]+):([\s\S]+)/,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: 'data',
strip: true,
append: true,
selfcontained: false
}
});

var stream = through.obj(function (file, enc, callback) {
var complete = function (error, contents) {
if (error) {
throw new PluginError(PLUGIN_NAME, error);
}

defs.loadfile = function(include_path) {
current_path = (file.path).substr(0, (file.path).lastIndexOf('/')+1 );
return fs.readFileSync(current_path + include_path);
};

var relative_path = file.relative;
var trimmed_ext = relative_path.substr(0, relative_path.lastIndexOf('.')) || relative_path;

var name = getTemplateName(options.root, trimmed_ext, options.extension, options.separator);
var code = getTemplateCode(contents,options.templateSettings,defs);
if(typeof code !== "string")
{
this.emit('error', new PluginError(PLUGIN_NAME, code));
}
file.contents = new Buffer([options.dictionary, '["', name, '"] = ', code, ';'].join(''));

this.push(file);
return callback();
}.bind(this);

if (file.isBuffer()) {
complete(null, file.contents.toString());
} else if (file.isStream()) {
readStream(file.contents, complete);
}
});
return stream;
function gulpDotCompile(options, templateSettings) {
options = options || {};
templateSettings = templateSettings || {};

_.defaults(options, {
root: '',
separator: '.',
extension: '',
dictionary: 'render',
namedTemplates: true,

//doT.js setting
templateSettings: _.defaults(templateSettings, {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
defineParams: /^\s*([\w$]+):([\s\S]+)/,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: 'data',
strip: true,
append: true,
selfcontained: false
})
});

var stream = through.obj(function (file, enc, callback) {
var complete = function (error, contents) {
if (error) {
throw new PluginError(PLUGIN_NAME, error);
}

defs.loadfile = function (include_path) {
current_path = (file.path).substr(0, (file.path).lastIndexOf(path.sep) + 1);
return fs.readFileSync(current_path + include_path);
};

var relative_path = file.relative;
var trimmed_ext = relative_path.substr(0, relative_path.lastIndexOf('.')) || relative_path;

var name = getTemplateName(options.root, trimmed_ext, options.extension, options.separator);
var code = getTemplateCode(contents, options.templateSettings, defs);

if (typeof code !== "string") {
this.emit('error', new PluginError(PLUGIN_NAME, code));
}

var prefix = '';

if (options.namedTemplates === true) {
prefix = '["', name, '"]';
}

file.contents = new Buffer([options.dictionary, prefix + ' = ', code, ';'].join(''));

this.push(file);

return callback();
}.bind(this);

if (file.isBuffer()) {
complete(null, file.contents.toString());
} else if (file.isStream()) {
readStream(file.contents, complete);
}
});

return stream;
};

module.exports = gulpDotify;
module.exports = gulpDotCompile;
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-dot-precompiler",
"version": "0.3.0",
"name": "gulp-dot-precompiler2",
"version": "0.3.1",
"description": "Gulp plugins for precompilation of doT template engine.",
"main": "index.js",
"directories": {
Expand All @@ -11,7 +11,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/kentliau/gulp-dot-precompiler.git"
"url": "https://github.com/rndm2/gulp-dot-precompiler2.git"
},
"keywords": [
"doT",
Expand All @@ -25,9 +25,9 @@
"author": "Kent Liau",
"license": "MIT",
"bugs": {
"url": "https://github.com/kentliau/gulp-dot-precompiler/issues"
"url": "https://github.com/rndm2/gulp-dot-precompiler2/issues"
},
"homepage": "https://github.com/kentliau/gulp-dot-precompiler",
"homepage": "https://github.com/rndm2/gulp-dot-precompiler2",
"dependencies": {
"lodash": "~2.4.1",
"dot": "~1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion test/integration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var dot = require('../');
var gutil = require('gulp-util');

describe('gulp-dot-precompiler', function () {
describe('gulp-dot-precompiler2', function () {
it('should process files with default options just as expected :)', function (done) {
var stream = dot();

Expand Down