From 1ab8e77085043318fd4cf94f2dbf1e5108358e7d Mon Sep 17 00:00:00 2001 From: Zachary Fox Date: Thu, 2 Mar 2017 14:10:58 -0600 Subject: [PATCH 1/4] Remove dependency on libxmljs-easy, use xml2js instead. Fixes preciousforever/SVG-Stacker#6 --- bin/svg-stacker | 32 +++++++++++++++++--------------- package.json | 4 ++-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bin/svg-stacker b/bin/svg-stacker index f798a21..7fe1840 100755 --- a/bin/svg-stacker +++ b/bin/svg-stacker @@ -3,7 +3,7 @@ var fs = require('fs') , path = require('path') , program = require('commander') - , easy = require('libxmljs-easy') + , xml2js = require('xml2js') /* -- internal */ , stack = [] /* -- user */ @@ -12,7 +12,7 @@ var fs = require('fs') ; program - .version('0.0.2') + .version('0.0.3') .option('-D, --no-demo', 'disables creation of demo files') .option('-s, --source [./]', 'source directory: location of svg files [./]', './') .option('-t, --target [./stack]', 'target directory: location for stack.svg and demo files [SOURCE_DIR/stack]', null) @@ -37,23 +37,25 @@ function addToStack(file) { var id = path.basename(file, '.svg'); var svgXml = fs.readFileSync(file, 'utf-8'); - var options = {}; + var parser = new xml2js.Parser(); + var builder = new xml2js.Builder({headless: true, rootName: 'svg'}); if ( program.huge ) { options['huge'] = true; } - - var svg = easy.parse(svgXml, options); - - // manipulate svg - svg.$class = 'i'; - svg.$id = id; - - stack.push({ - id: id, - svg: svg.$.toString().replace('xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"', ''), - w: svg.$width, - h: svg.$height + + parser.parseString(svgXml, function (err, result) { + + // manipulate svg + result.svg.$.class = 'i'; + result.svg.$.class = id; + + stack.push({ + id: id, + svg: builder.buildObject(result.svg).replace('xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"', ''), + w: result.svg.$.width, + h: result.svg.$.height + }); }); } diff --git a/package.json b/package.json index f23f8ab..8902bd1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svg-stacker", - "version": "0.0.2", + "version": "0.0.3", "description": "An experimental tool that turns a folder of SVG files into a single SVG Stack", "keywords": [ "svg", @@ -22,7 +22,7 @@ }, "dependencies": { "commander": "1.0.x", - "libxmljs-easy": "0.2.1" + "xml2js": "" }, "bugs": "http://github.com/preciousforever/SVG-Stacker/issues", "bin": "./bin/svg-stacker" From f06729b59952e389967754de125afad172e861e8 Mon Sep 17 00:00:00 2001 From: Zachary Fox Date: Thu, 2 Mar 2017 15:17:04 -0600 Subject: [PATCH 2/4] Work on linux! Also fix id bug --- bin/svg-stacker | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/svg-stacker b/bin/svg-stacker index 7fe1840..54c592f 100755 --- a/bin/svg-stacker +++ b/bin/svg-stacker @@ -1,4 +1,4 @@ -#!/usr/bin/env node --harmony-proxies +#!/usr/bin/env node var fs = require('fs') , path = require('path') @@ -48,7 +48,7 @@ function addToStack(file) { // manipulate svg result.svg.$.class = 'i'; - result.svg.$.class = id; + result.svg.$.id = id; stack.push({ id: id, From 6319a9eabe6529035071980e0f1e451567262058 Mon Sep 17 00:00:00 2001 From: Zachary Fox Date: Fri, 3 Mar 2017 06:51:06 -0600 Subject: [PATCH 3/4] Better way to remove xmlns and xmlns:xlink attributes --- bin/svg-stacker | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/svg-stacker b/bin/svg-stacker index 54c592f..55fba2e 100755 --- a/bin/svg-stacker +++ b/bin/svg-stacker @@ -49,10 +49,12 @@ function addToStack(file) { // manipulate svg result.svg.$.class = 'i'; result.svg.$.id = id; + delete result.svg.$['xmlns']; + delete result.svg.$['xmlns:xlink']; stack.push({ id: id, - svg: builder.buildObject(result.svg).replace('xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"', ''), + svg: builder.buildObject(result.svg), w: result.svg.$.width, h: result.svg.$.height }); From d61c2bc2383709ef14fb2852f3ed9ff56ad0bcf6 Mon Sep 17 00:00:00 2001 From: Zachary Fox Date: Fri, 3 Mar 2017 07:09:50 -0600 Subject: [PATCH 4/4] Can't remove options --- bin/svg-stacker | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/svg-stacker b/bin/svg-stacker index 55fba2e..801fcca 100755 --- a/bin/svg-stacker +++ b/bin/svg-stacker @@ -39,6 +39,7 @@ function addToStack(file) { var svgXml = fs.readFileSync(file, 'utf-8'); var parser = new xml2js.Parser(); var builder = new xml2js.Builder({headless: true, rootName: 'svg'}); + var options = {}; if ( program.huge ) { options['huge'] = true;