Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to apply aria-labelledby and role attributes by default / Support custom attrs #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.sublime-project
*.sublime-workspace

/node_modules
tmp
57 changes: 57 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/index.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Launch Tests",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "test"
},
"externalConsole": false,
"sourceMaps": false,
"outDir": null
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858,
"address": "localhost",
"restart": false,
"sourceMaps": false,
"outDir": null,
"localRoot": "${workspaceRoot}",
"remoteRoot": null
}
]
}
135 changes: 74 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,91 @@
var fs = require("fs"),
path = require("path"),
mkdirp = require("mkdirp"),
Writer = require("broccoli-writer"),
helpers = require("broccoli-kitchen-sink-helpers"),
cheerio = require("cheerio");
'use strict';

module.exports = SvgProcessor;
SvgProcessor.prototype = Object.create(Writer.prototype);
SvgProcessor.prototype.constructor = SvgProcessor;

function SvgProcessor (inputTree, options) {

if (!(this instanceof SvgProcessor)) return new SvgProcessor(inputTree, options);
var fs = require('fs');
var merge = require('merge');
var path = require('path');
var mkdirp = require('mkdirp');
var CachingWriter = require('broccoli-caching-writer');
var helpers = require('broccoli-kitchen-sink-helpers');

this.inputTree = inputTree;

this.options = {
outputFile: "/images.svg"
};

for (key in options) {
if (options.hasOwnProperty(key)) {
this.options[key] = options[key];
}
}
var svgToSymbol = require('./utils/svg-to-symbol');

var defaultSettings = {
outputFile: '/images.svg',
annotation: 'SVGStore Processor',
extraSymbolAttrs: [],
};

SvgProcessor.prototype.write = function (readTree, destDir) {

var self = this;

return readTree(this.inputTree).then(function (srcDir) {

var output = ["<svg xmlns='http://www.w3.org/2000/svg' style='display: none'>"];

try {
// TOOD: Perhaps be a bit more robust (and thus, more explicit about the proper API) with validation
var validationErrorPrefix = 'Expected a non-falsey argument for `_inputNode`, got ';


function SvgProcessor(_inputNode, _options) {
if (!(this instanceof SvgProcessor)) {
return new SvgProcessor(_inputNode, _options);
}

var options = merge(defaultSettings, _options);
if (options.name != null) {
this._name = options.name;
} else {
this._name = (this.constructor && this.constructor.name != null) ? this.constructor.name : 'SVGStore';
}
this._annotation = options.annotation;

var label = this._name + ' (' + this._annotation + ')';
if (!_inputNode) {
throw new TypeError(label + ': ' + validationErrorPrefix + _inputNode);
}

this._options = options;
var inputNodes = Array.isArray(_inputNode) ? _inputNode : [_inputNode];

CachingWriter.call(this, inputNodes, this._options);
}

SvgProcessor.prototype = Object.create(CachingWriter.prototype);
SvgProcessor.prototype.constructor = SvgProcessor;
SvgProcessor.prototype.description = 'svgstore';
module.exports = SvgProcessor;

var inputFiles = helpers.multiGlob(["**/*.svg"], { cwd: srcDir });
for (var i = 0; i < inputFiles.length; i++) {
var stat = fs.statSync(srcDir + "/" + inputFiles[i]);
if (stat && stat.isFile()) {
var fileContents = fs.readFileSync(srcDir + "/" + inputFiles[i], { encoding: "utf8" });
output.push(parseSvg(inputFiles[i], fileContents));
}
}

} catch (error) {
if (!error.message.match("did not match any files")) {
throw error;
}
}
SvgProcessor.prototype.build = function () {

output.push("</svg>");
var output = ['<svg xmlns="http://www.w3.org/2000/svg" style="display: none">'];
var extraSymbolAttrs = this._options.extraSymbolAttrs;

helpers.assertAbsolutePaths([self.options.outputFile]);
mkdirp.sync(path.join(destDir, path.dirname(self.options.outputFile)));
var concatenatedOutput = output.join("\n");
fs.writeFileSync(path.join(destDir, self.options.outputFile), concatenatedOutput);
try {
var srcDir, inputFiles, inputFilePath, stat;
for (var i = 0; i < this.inputPaths.length; i++) {
srcDir = this.inputPaths[i];
inputFiles = helpers.multiGlob(["**/*.svg"], { cwd: srcDir });

});
for (var j = 0; j < inputFiles.length; j++) {
inputFilePath = path.join(srcDir, inputFiles[j]);
stat = fs.statSync(inputFilePath);

};
if (stat && stat.isFile()) {
var fileContents = fs.readFileSync(inputFilePath, { encoding: 'utf8' });
output.push(svgToSymbol(inputFilePath, fileContents, {
extraAttrs: extraSymbolAttrs
}));
}
}
}
} catch (error) {
if (!error.message.match("did not match any files")) {
throw error;
}
}

function parseSvg (filename, fileContents) {
output.push("</svg>");

var $fileContents = cheerio.load(fileContents, { xmlMode: true }),
$svg = $fileContents("svg"),
viewBox = $svg.attr("viewBox"),
$outputContents = cheerio.load("<symbol id='" + path.basename(filename).replace(/\.[^/.]+$/, "") + "' viewBox='" + viewBox + "'></symbol>", { xmlMode: true }),
$symbol = $outputContents("symbol");
helpers.assertAbsolutePaths([this.outputPath]); // TODO: Necessary?

$symbol.html($svg.html());
var concatenatedOutput = output.join("\n");
var outputDestination = path.join(this.outputPath, this._options.outputFile);

return $outputContents.html();
mkdirp.sync(path.dirname(outputDestination));

return fs.writeFileSync(outputDestination, concatenatedOutput);
};
33 changes: 0 additions & 33 deletions npm-debug.log

This file was deleted.

17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"type": "git",
"url": "https://github.com/jmarquis/broccoli-svgstore.git"
},
"scripts": {
"test": "./node_modules/.bin/_mocha"
},
"author": "Jeremy Marquis <[email protected]>",
"license": "MIT",
"keywords": [
Expand All @@ -19,9 +22,15 @@
},
"homepage": "https://github.com/jmarquis/broccoli-svgstore",
"dependencies": {
"broccoli-kitchen-sink-helpers": "^0.2.5",
"broccoli-writer": "^0.1.1",
"cheerio": "^0.18.0",
"mkdirp": "^0.5.0"
"broccoli-caching-writer": "2.2.1",
"broccoli-kitchen-sink-helpers": "0.3.1",
"cheerio": "^0.20.0",
"merge": "1.2.0",
"mkdirp": "^0.5.1"
},
"devDependencies": {
"broccoli": "0.16.9",
"chai": "3.5.0",
"mocha": "2.5.3"
}
}
Loading