From ebf4d8e3faf22fc4ad1f91ac4d284d69d04c52d1 Mon Sep 17 00:00:00 2001 From: Gustavo Vilela Vargas Date: Fri, 22 Jul 2016 16:22:33 -0300 Subject: [PATCH 1/2] Added option to specify variable name in output code. --- README.md | 8 +++++++- lib/index.js | 20 +++++++++++++++----- src/index.coffee | 11 ++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4d123b8..7883c0b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ paths = gulp.task "peg:compile", -> gulp .src( paths.scripts.peg ) - .pipe( peg( ).on( "error", gutil.log ) ) + .pipe( peg( {exportVar: ''} ) ) + .on( "error", gutil.log ) .pipe( gulp.dest( paths.build ) ) ``` @@ -39,3 +40,8 @@ Plugin redirects passed options directly to PEG, so read [its documentation](htt This option is inspired by [grunt-peg](https://github.com/dvberkel/grunt-peg) plugin, and defines variable to which the generated parser will be assigned in the output file. Default value is `module.exports`. +`exportVar` | Behavior +---------- | --------- +`null` | **default** Will set variable to `module.exports` +function(filename){} | should return the desired variable name +`''` (empty string) | convenience value to set variable to *filename*parser diff --git a/lib/index.js b/lib/index.js index 11800e4..187fe99 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +// Generated by CoffeeScript 1.10.0 var PEG, gutil, processFile, through, util; PEG = require("pegjs"); @@ -9,10 +10,19 @@ gutil = require("gulp-util"); through = require("through2"); processFile = function(file, opts) { - var grammar, parser, source; + var grammar, parser, source, variable; grammar = file.contents.toString("utf8"); parser = PEG.buildParser(grammar, opts); - source = util.format("%s = %s;", opts.exportVar, parser); + if (typeof opts.exportVar === 'function') { + variable = opts.exportVar(file.relative); + } else { + if (opts.exportVar !== '') { + variable = opts.exportVar; + } else { + variable = file.relative.split('.')[0] + 'parser'; + } + } + source = util.format("%s = %s;", variable, parser); file.path = gutil.replaceExtension(file.path, ".js"); file.contents = new Buffer(source); return file; @@ -29,15 +39,15 @@ module.exports = function(opts) { opts.output = "source"; } return through.obj(function(file, enc, cb) { - var e; + var e, error; if (file.isStream()) { return this.emit("error", gutil.PluginError("gulp-peg", "Streams are not supported!")); } else if (file.isBuffer()) { try { this.push(processFile(file, opts)); return cb(); - } catch (_error) { - e = _error; + } catch (error) { + e = error; this.emit("error", e); return cb(); } diff --git a/src/index.coffee b/src/index.coffee index f7aceaf..15acff7 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -7,7 +7,16 @@ through = require "through2" processFile = ( file, opts ) -> grammar = file.contents.toString "utf8" parser = PEG.buildParser grammar, opts - source = util.format "%s = %s;", opts.exportVar, parser + + if typeof opts.exportVar is 'function' + variable = opts.exportVar file.relative + else + if opts.exportVar isnt '' + variable = opts.exportVar + else + variable = file.relative.split('.')[0] + 'parser' + + source = util.format "%s = %s;", variable, parser file.path = gutil.replaceExtension file.path, ".js" file.contents = new Buffer source From 6776224bc09d3b946925892f01826f657e19d507 Mon Sep 17 00:00:00 2001 From: Gustavo Vargas Date: Fri, 22 Jul 2016 16:24:01 -0300 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7883c0b..0a82a9d 100644 --- a/README.md +++ b/README.md @@ -44,4 +44,4 @@ This option is inspired by [grunt-peg](https://github.com/dvberkel/grunt-peg) pl ---------- | --------- `null` | **default** Will set variable to `module.exports` function(filename){} | should return the desired variable name -`''` (empty string) | convenience value to set variable to *filename*parser +`''` (empty string) | convenience value to set variable to '*filename*parser'