Repository: svg/svgo
Pull request #526 (2016-03-27 03:13:27)

Add option to indent when pretty printing svg
I took a stab at adding an option to indent svgs for #465 

##### CLI

```bash
svgo --pretty --indent 2 image.svg
```

##### JS API

```js
new svgo({ 
  full: true, 
  plugins: [], 
  js2svg : { 
    pretty: true,
    indent: 2 
  }
}).optimize(file, function(out) { ... });
```

It defaults to 4 to stick with the existing convention, but hopefully provides a little more flexibility to those who are looking to have different indention settings.


Patch:
diff --git a/lib/svgo/coa.js b/lib/svgo/coa.js
index b735bd6..875c6ed 100644
--- a/lib/svgo/coa.js
+++ b/lib/svgo/coa.js
@@ -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')
@@ -216,6 +223,7 @@ module.exports = require('coa').Cmd()
 
             config.js2svg = config.js2svg || {};
             config.js2svg.pretty = true;
+            config.js2svg.indent = parseInt(opts.indent, 10);
 
         }
 
diff --git a/lib/svgo/js2svg.js b/lib/svgo/js2svg.js
index d4f2637..677573b 100644
--- a/lib/svgo/js2svg.js
+++ b/lib/svgo/js2svg.js
@@ -22,7 +22,7 @@ var defaults = {
     cdataEnd: ']]>',
     textStart: '',
     textEnd: '',
-    indent: '    ',
+    indent: 4,
     regEntities: /[&'"<>]/g,
     regValEntities: /[&"<>]/g,
     encodeEntity: encodeEntity,
@@ -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';


Test patch:
diff --git a/test/mocha.opts b/test/mocha.opts
index 5abdafe..3b56cd1 100644
--- a/test/mocha.opts
+++ b/test/mocha.opts
@@ -4,3 +4,4 @@ test/config
 test/svg2js
 test/plugins
 test/jsapi
+test/svgo
diff --git a/test/svgo/_index.js b/test/svgo/_index.js
new file mode 100644
index 0000000..815ecdc
--- /dev/null
+++ b/test/svgo/_index.js
@@ -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();
+            });
+
+        });
+
+    });
+
+});
diff --git a/test/svgo/test.svg b/test/svgo/test.svg
new file mode 100644
index 0000000..c4390f0
--- /dev/null
+++ b/test/svgo/test.svg
@@ -0,0 +1,19 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <g attr1="val1">
+        <g attr2="val2">
+            <path attr2="val3" d="..."/>
+        </g>
+        <path d="..."/>
+    </g>
+</svg>
+
+@@@
+
+<svg xmlns="http://www.w3.org/2000/svg">
+  <g attr1="val1">
+    <g attr2="val2">
+      <path attr2="val3" d="..."/>
+    </g>
+    <path d="..."/>
+  </g>
+</svg>