Skip to content

Commit

Permalink
Add option to indent when pretty printing svg
Browse files Browse the repository at this point in the history
  • Loading branch information
scurker committed Mar 27, 2016
1 parent b80944c commit 3d84960
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ module.exports = require('coa').Cmd()
.long('pretty')
.flag()
.end()
.opt()
.name('indent').title('Indent number when pretty printing SVGs')
.long('indent')
.val(function(val) {
return !isNaN(val) ? val : this.reject("Option '--indent' must be an integer number");
})
.end()
.opt()
.name('quiet').title('Only output error messages, not regular status messages')
.short('q').long('quiet')
Expand Down Expand Up @@ -216,6 +223,7 @@ module.exports = require('coa').Cmd()

config.js2svg = config.js2svg || {};
config.js2svg.pretty = true;
config.js2svg.indent = parseInt(opts.indent, 10);

}

Expand Down
6 changes: 5 additions & 1 deletion lib/svgo/js2svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var defaults = {
cdataEnd: ']]>',
textStart: '',
textEnd: '',
indent: ' ',
indent: 4,
regEntities: /[&'"<>]/g,
regValEntities: /[&"<>]/g,
encodeEntity: encodeEntity,
Expand Down Expand Up @@ -60,6 +60,10 @@ function JS2SVG(config) {
this.config = defaults;
}

if (this.config.indent && !isNaN(this.config.indent)) {
this.config.indent = new Array(this.config.indent + 1).join(' ');
}

if (this.config.pretty) {
this.config.doctypeEnd += '\n';
this.config.procInstEnd += '\n';
Expand Down
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/config
test/svg2js
test/plugins
test/jsapi
test/svgo
40 changes: 40 additions & 0 deletions test/svgo/_index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var FS = require('fs'),
PATH = require('path'),
SVGO = require(process.env.COVERAGE ?
'../../lib-cov/svgo':
'../../lib/svgo');

describe('indentation', function() {

it('should create indent with 2 spaces', function(done) {

var filepath = PATH.resolve(__dirname, './test.svg'),
svgo;

FS.readFile(filepath, 'utf8', function(err, data) {
if (err) {
throw err;
}

var splitted = data.trim().split(/\s*@@@\s*/),
orig = splitted[0],
should = splitted[1];

svgo = new SVGO({
full : true,
plugins : [],
js2svg : { pretty: true, indent: 2 }
});

svgo.optimize(orig, function(result) {
( result.data.trim() ).should.be.equal(should);
done();
});

});

});

});
19 changes: 19 additions & 0 deletions test/svgo/test.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3d84960

Please sign in to comment.