Skip to content

Commit 9190b9b

Browse files
committed
use babel
1 parent ce93637 commit 9190b9b

11 files changed

Lines changed: 198 additions & 78 deletions

File tree

.eslintrc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"ecmaFeatures": {
3+
"modules": true,
4+
"module": true
5+
},
26
"rules": {
37
"no-unused-expressions": [0],
48
"no-underscore-dangle": [0],
@@ -10,11 +14,13 @@
1014
"no-loop-func": [0],
1115
"key-spacing": [0],
1216
"strict": [0],
17+
"no-var": [2],
1318
"indent": [2],
1419
"quotes": [2, "single", "avoid-escape"],
1520
},
1621
"env": {
1722
"mocha": true,
18-
"node": true
23+
"node": true,
24+
"es6": true
1925
}
2026
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ npm-debug.log
2525
.idea/
2626
.project
2727
.svn
28+
29+
### Project
30+
31+
build/

.jscsrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
".git/**",
55
"node_modules/**"
66
],
7-
"fileExtensions": [".js", ".bemtree", ".bemhtml"]
7+
"fileExtensions": [".js"],
8+
"esnext": true
89
}

.npmignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.gitignore
2+
3+
node_modules/
4+
5+
build/
6+
7+
test/
8+
.travis.yml
9+
npm-debug.log
10+
11+
gulpfile.babel.js

gulpfile.babel.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import gulp from 'gulp';
2+
import path from 'path';
3+
import fs from 'fs-extra';
4+
5+
gulp.task('clean', (done) => {
6+
fs.remove(path.join(__dirname, 'poshtml.js'), () => {
7+
fs.remove(path.join(__dirname, 'build'), done);
8+
});
9+
});
10+
11+
// Build
12+
13+
gulp.task('build:lib', ['clean'], () => {
14+
let babel = require('gulp-babel');
15+
return gulp.src('lib/*.js')
16+
.pipe(babel({ loose: 'all' }))
17+
.pipe(gulp.dest('build/lib'));
18+
});
19+
20+
gulp.task('build:docs', ['clean'], () => {
21+
let ignore = require('fs').readFileSync('.npmignore').toString()
22+
.trim().split(/\n+/)
23+
.concat(['.npmignore', 'index.js', 'package.json'])
24+
.map( i => '!' + i );
25+
return gulp.src(['*'].concat(ignore))
26+
.pipe(gulp.dest('build'));
27+
});
28+
29+
gulp.task('build:package', ['clean'], () => {
30+
let editor = require('gulp-json-editor');
31+
gulp.src('./package.json')
32+
.pipe(editor( (p) => {
33+
p.main = 'lib/posthtml';
34+
p.devDependencies.babel = p.dependencies.babel;
35+
delete p.dependencies.babel;
36+
return p;
37+
}))
38+
.pipe(gulp.dest('build'));
39+
});
40+
41+
gulp.task('build', ['build:lib', 'build:docs', 'build:package']);
42+
43+
// Lint
44+
45+
gulp.task('lint', function() {
46+
let eslint = require('gulp-eslint');
47+
48+
return gulp.src(['*.js', 'test/*.js'])
49+
.pipe(eslint())
50+
.pipe(eslint.format())
51+
.pipe(eslint.failAfterError());
52+
});
53+
54+
55+
gulp.task('test', function() {
56+
let mocha = require('gulp-mocha');
57+
return gulp.src('test/*.js', { read : false }).pipe(mocha());
58+
});
59+
60+
// Common
61+
gulp.task('default', ['test', 'lint']);

gulpfile.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

index.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1 @@
1-
var Promise = require('bluebird'),
2-
bh = new (require('bh').BH)(),
3-
buildBemJson = require('html2bemjson');
4-
5-
bh.setOptions({
6-
// add SVG short tags
7-
8-
// FIXME(@voischev): @veribigman это что?
9-
shortTags : ['rect']
10-
});
11-
12-
module.exports = function posthtml() {
13-
14-
return new (function() {
15-
16-
this.parse = function(html) {
17-
return [buildBemJson.convert(html)];
18-
};
19-
20-
this.plugins = [];
21-
22-
this.use = function(plugin) {
23-
this.plugins.push(plugin);
24-
25-
return this;
26-
};
27-
28-
this.process = function(html) {
29-
var _this = this;
30-
31-
return new Promise(function(resolve) {
32-
33-
var tree = _this.parse(html);
34-
35-
_this.plugins.forEach(function(plugin) {
36-
tree = plugin(tree);
37-
});
38-
39-
var bemjson = bh.processBemJson(tree);
40-
41-
resolve(bh.apply(bemjson));
42-
});
43-
};
44-
})();
45-
};
1+
module.exports = require('./lib/posthtml');

lib/posthtml.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import buildBemJson from 'html2bemjson';
2+
3+
var tmpl = new (require('bh').BH);
4+
5+
class Posthtml {
6+
7+
constructor(options) {
8+
9+
let opt = options || {},
10+
{ tmplOptions } = opt,
11+
plugins = [];
12+
13+
/**
14+
* Get options
15+
* @return {Object} options or {}
16+
*/
17+
this.getOptions = function() {
18+
return opt;
19+
};
20+
21+
/**
22+
* Parse html to json tree
23+
* @param {String} html htmltree
24+
* @return {String} json jsontree
25+
*/
26+
this.parse = function(html) {
27+
return [buildBemJson.convert(html)];
28+
};
29+
30+
/**
31+
* use plagins
32+
* @param {function} plugin posthtml().use(plugin());
33+
*/
34+
this.use = function(plugin) {
35+
plugins.push(plugin);
36+
return this;
37+
};
38+
39+
/**
40+
* @param {String} html htmltree
41+
* @return {[type]} [description]
42+
*/
43+
this.process = function(html) {
44+
45+
let _this = this;
46+
47+
tmplOptions && tmpl.setOptions(tmplOptions);
48+
49+
return new Promise(function(resolve) {
50+
51+
let tree = _this.parse(html);
52+
53+
plugins.forEach(function(plugin) {
54+
tree = plugin(tree);
55+
});
56+
57+
let json = tmpl.processBemJson(tree);
58+
59+
resolve(tmpl.apply(json));
60+
});
61+
};
62+
}
63+
}
64+
65+
export default function posthtml(options) {
66+
return new Posthtml(options);
67+
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
"description": "HTML post processor",
55
"keywords": [],
66
"dependencies": {
7+
"babel": "^5.6.14",
78
"bh": "git://github.com/bem/bh.git#f2e8c6dd7a2e5b57300ab9eea6710823cc38711c",
8-
"bluebird": "^2.9.27",
99
"html2bemjson": "^1.1.0"
1010
},
1111
"peerDependencies": {},
1212
"devDependencies": {
1313
"chai": "^3.0.0",
14+
"fs-extra": "^0.20.1",
1415
"gulp": "^3.9.0",
16+
"gulp-babel": "^5.1.0",
1517
"gulp-eslint": "^0.14.0",
18+
"gulp-json-editor": "^2.2.1",
1619
"gulp-mocha": "^2.1.2",
1720
"jscs": "^1.11.2",
1821
"jscs-protein": "https://github.com/theprotein/jscs-protein.git",

test/set-options.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import posthtml from '../index.js';
2+
import { expect } from 'chai';
3+
4+
var beforeHTML = '<div class="button"><rect/><div class="button__text">Text</div></div>',
5+
options = { tmplOptions : { shortTags : ['rect'] } };
6+
7+
8+
function test(html, done) {
9+
posthtml(options).process(html).then(result => {
10+
expect(beforeHTML).to.eql(result);
11+
done();
12+
}).catch(error => done(error));
13+
}
14+
15+
describe('Set options', () => {
16+
17+
it('html eqval', done => {
18+
test(beforeHTML, done);
19+
});
20+
21+
it('minifer \\n', done => {
22+
test('<div class="button">\n<rect/>\n<div class="button__text">Text</div>\n</div>', done);
23+
});
24+
25+
it('Get options', done => {
26+
expect(posthtml().getOptions()).to.eql({});
27+
expect(posthtml({ op: 1 }).getOptions().op).to.eql(1);
28+
done();
29+
})
30+
31+
});
32+

0 commit comments

Comments
 (0)