forked from akoenig/gulp-spellcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (53 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* gulp-spellcheck
*
* Copyright(c) 2014 André König <[email protected]>
* MIT Licensed
*
*/
/**
* @author André König <[email protected]>
*
*/
'use strict';
var util = require('util');
var through = require('through2');
var aspell = require('aspell');
var gutil = require('gulp-util');
var PLUGIN_NAME = 'gulp-spellcheck';
module.exports = function (options) {
options = options || {};
options.replacement = options.replacement || '%s (suggestions: %s)';
options.language = (options.language)? util.format('--lang=%s', options.language) : '';
aspell.args.push(options.language);
function check (file, enc, callback) {
/*jshint validthis:true */
var self = this;
if(!file.contents)
{
return callback();
}
var contents = file.contents.toString('utf-8');
// Remove all line breaks and add a circumflex in order to disable 'pipe mode'.
// see: http://aspell.net/man-html/Through-A-Pipe.html
aspell('^' + contents.replace(/\r?\n/g, ''))
.on('error', function onError (err) {
err = err.toString('utf-8');
return self.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
})
.on('result', function onResult (result) {
if ('misspelling' === result.type) {
contents = contents.replace(result.word, util.format(options.replacement, result.word, result.alternatives.join(', ')));
}
})
.on('end', function () {
file.contents = new Buffer(contents);
self.push(file);
return callback();
});
}
function finalize (callback) {
return callback();
}
return through.obj(check, finalize);
};