From 0c642050965d7e43565f016c9b06e06e1bb661d1 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Sun, 30 Jan 2022 16:10:21 +0100 Subject: [PATCH] feat: Allow passing options to nomnoml-cli --- .gitignore | 1 + Gruntfile.js | 10 ++++++++- README.md | 9 ++++++++- tasks/nomnoml.js | 48 ++++++++++++++++++++++++-------------------- test/nomnoml_test.js | 9 +++++++-- 5 files changed, 51 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 5488188..9407539 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ coverage node_modules npm-debug.log test/piracy.png +test/piracy.svg diff --git a/Gruntfile.js b/Gruntfile.js index 9c0cd7f..cafe401 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -15,10 +15,18 @@ module.exports = function (grunt) { }, nomnoml: { - task: { + png: { files: { 'test/piracy.png': ['test/piracy.nomnoml'] } + }, + svg: { + options: { + format: 'svg' + }, + files: { + 'test/piracy.svg': ['test/piracy.nomnoml'] + } } }, diff --git a/README.md b/README.md index 3e285c2..41b3efb 100644 --- a/README.md +++ b/README.md @@ -52,16 +52,22 @@ grunt.initConfig({ } }, all: { + options: { format: 'svg' }, src: ['doc/images/*.nomnoml'] dest: 'dist/doc/images' } } }); ``` + The configuration consists of key-value pairs with the output image path as a key pointing to the nomnoml input file. If you specify more source files by wildcards, the destination should be a directory; the source file -extension wil lbe replaced by "png" in the output file name. +extension will be replaced by "png" or other one depending on the format +in the output file name. + +The `options` can contain one or more options supported by the [nomnoml-cli API]. +For example, `format` can be `png` (default), `jpg`, `svg` or `pdf`. Then, load the plugin: @@ -103,3 +109,4 @@ Licensed under the MIT license. [Getting Gtarted]: https://github.com/gruntjs/grunt/wiki/Getting-started [nomnoml]: http://www.nomnoml.com/ [nomnoml-cli]: https://github.com/prantlf/nomnoml-cli +[nomnoml-cli API]: https://github.com/prantlf/nomnoml-cli#programmatic-usage diff --git a/tasks/nomnoml.js b/tasks/nomnoml.js index 637be76..192508b 100644 --- a/tasks/nomnoml.js +++ b/tasks/nomnoml.js @@ -12,13 +12,13 @@ const generateDiagram = require('nomnoml-cli'); const { join, parse, dirname } = require('path'); module.exports = function (grunt) { - function processDiagram(fileSrc, fileDest) { + function processDiagram(fileSrc, fileDest, options) { try { grunt.log.subhead('Processing diagram "' + fileSrc + '"'); - return generateDiagram({ + return generateDiagram(Object.assign({ inputFile: fileSrc, output: fileDest - }); + }, options)); } catch (error) { grunt.log.error(error); grunt.fail.warn('Processing diagram "' + fileSrc + '" failed\n'); @@ -26,25 +26,29 @@ module.exports = function (grunt) { } grunt.registerMultiTask('nomnoml', "Generate images from nomnoml diagram sources", function () { - var done = this.async(), - promises = this.files.map(function (file) { - // If multiple source files are specified, the destination - // path should point to a directory - var single = file.orig.src.length === 1 && - !file.orig.src.some(function (src) { - return src.indexOf('*') >= 0 || src.indexOf('?') >= 0; - }), - promises = file.src.map(function (src) { - // If the destination is a directory, use the source file name - // with the '.png' extension - const dest = single ? file.dest : join(file.dest, parse(src).name + '.png'); - const dir = dirname(dest); - grunt.file.mkdir(dir); - return processDiagram(src, dest); - }); - return Promise.all(promises); + const done = this.async(); + const options = this.options({}); + let { format = 'png' } = options; + format = format.toLowerCase(); + const promises = this.files.map(function (file) { + // If multiple source files are specified, the destination + // path should point to a directory + const single = file.orig.src.length === 1 && + !file.orig.src.some(function (src) { + return src.indexOf('*') >= 0 || src.indexOf('?') >= 0; }); - Promise.all(promises) - .then(done); + const promises = file.src.map(function (src) { + // If the destination is a directory, use the source file name + // with the '.png' or format-dependent extension + const dest = single ? file.dest : join(file.dest, parse(src).name + '.' + format); + const dir = dirname(dest); + grunt.file.mkdir(dir); + return processDiagram(src, dest, options); + }); + return Promise.all(promises); + }); + Promise + .all(promises) + .then(done); }); }; diff --git a/test/nomnoml_test.js b/test/nomnoml_test.js index 6347476..b79a85a 100644 --- a/test/nomnoml_test.js +++ b/test/nomnoml_test.js @@ -4,9 +4,14 @@ const { statSync } = require('fs'); const { join } = require('path'); exports.nomnoml = { - task: function (test) { - var output = statSync(join(__dirname, 'piracy.png')); + png: function (test) { + const output = statSync(join(__dirname, 'piracy.png')); test.ok(output.isFile() && output.size > 0, 'creates a PNG file'); test.done(); + }, + svg: function (test) { + const output = statSync(join(__dirname, 'piracy.svg')); + test.ok(output.isFile() && output.size > 0, 'creates a SVG file'); + test.done(); } };