Skip to content

Commit

Permalink
Extended the ID generator callback signature (closes #176)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed Aug 11, 2016
1 parent 3f9f36c commit a0acc4e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.4 Bugfix release (unreleased)
* Updated dependencies
* Extended the ID generator callback signature ([#176](https://github.com/jkphl/svg-sprite/issues/176))

## 1.3.3 Bugfix release (2016-04-28)
* Fixed CLI regression bug ([#173](https://github.com/jkphl/svg-sprite/issues/173))
* Fixed CLI root attributes file handling ([#144](https://github.com/jkphl/svg-sprite/issues/144))
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ shape : {
Property | Type | Default | Description |
-------------------------| --------------- | ------------- | ------------------------------------------ |
`shape.id.separator` | String | `"--"` | Separator for traversing a directory structure into a shape ID |
`shape.id.generator` | Function∣String | See desc. | Callback for translating the local part of a shape's file name into a shape ID. The callback's signature is `function(name) { /* ... */ return id; }`. By default, the file extension `".svg"` is stripped off and directory structures get traversed using the `id.separator` as replacement for the directory separator. You may also provide a template string (e.g. `"icon-%s"`), in which case the placeholder `"%s"` gets substituted with the traversed local file name. If the string doesn't contain any placeholder, it is used as a prefix to the local file name. |
`shape.id.generator` | Function∣String | See desc. | Callback for translating the local part of a shape's file name into a shape ID. The callback's signature is `function(name, file) { /* ... */ return id; }`, where `name` is the relative path of the source file within the base directory and `file` the original [vinyl](https://github.com/wearefractal/vinyl) file object. By default, the file extension `".svg"` is stripped off the `name` value and directory structures are traversed using the `id.separator` as replacement for the directory separator. You may also provide a template string (e.g. `"icon-%s"`), in which case the placeholder `"%s"` gets substituted with the traversed local file name. If the string doesn't contain any placeholder, it is used as a prefix to the local file name. |
`shape.id.pseudo` | String | `"~"` | String separator for pseudo CSS classes in file names. Example: `my-icon.svg` and `my-icon~hover.svg` for an icon with a regular and a `:hover` state. |
`shape.id.whitespace` | String | `"_"` | Replacement string for whitespace characters in file names during shape ID generation. Example: By default, `My Custom Icon.svg` will result in the shape ID `my_custom_icon`. |

Expand Down
42 changes: 25 additions & 17 deletions lib/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,33 @@ SVGSpriter.prototype.add = function(file, name, svg) {
// If no vinyl file object has been given
if (!this._isVinylFile(file)) {
file = _.trim(file);
name = _.trimStart(_.trim(name), path.sep + '.') || path.basename(file);
svg = _.trim(svg);

// Argument validation
var error = null;
if (arguments.length < 3) {
error = 'SVGSpriter.add: You must provide 3 arguments';
}
if (!file.length) {
error = util.format('SVGSpriter.add: "%s" is not a valid absolute file name', file);
}
if (!name.length) {
// If the name part of the file path is absolute
if (path.isAbsolute(name)) {
error = util.format('SVGSpriter.add: "%s" is not a valid relative file name', name);
}
if (!svg.length) {
error = 'SVGSpriter.add: You must provide SVG contents';
}
if (file.substr(-name.length) !== name) {
error = util.format('SVGSpriter.add: "%s" is not the local part of "%s"', name, file);

// Else
} else {
name = _.trimStart(_.trim(name), path.sep + '.') || path.basename(file);
svg = _.trim(svg);

// Argument validation
var error = null;
if (arguments.length < 3) {
error = 'SVGSpriter.add: You must provide 3 arguments';
}
if (!file.length) {
error = util.format('SVGSpriter.add: "%s" is not a valid absolute file name', file);
}
if (!name.length) {
error = util.format('SVGSpriter.add: "%s" is not a valid relative file name', name);
}
if (!svg.length) {
error = 'SVGSpriter.add: You must provide SVG contents';
}
if (file.substr(-name.length) !== name) {
error = util.format('SVGSpriter.add: "%s" is not the local part of "%s"', name, file);
}
}

// In case of an error: Throw it!
Expand Down
12 changes: 10 additions & 2 deletions lib/svg-sprite/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ async = require('async'),
* @return {String} Shape ID
*/
createIdGenerator = function(template) {
return function(name) {
/**
* ID generator
*
* @param {String} name Relative file path
* @param {File} file Original vinyl file
* @returns {String} Shape ID
*/
var generator = function(name, file) {
return util.format(template || '%s', path.basename(name.split(path.sep).join(this.separator).replace(/\s+/g, this.whitespace), '.svg'));
};
return generator;
},
/**
* Default shape configuration
Expand Down Expand Up @@ -145,7 +153,7 @@ function SVGShape(file, spriter) {
this.config.id.generator = createIdGenerator(_.isString(this.config.id.generator) ? (this.config.id.generator + ((this.config.id.generator.indexOf('%s') >= 0) ? '' : '%s')) : '%s');
}

this.id = this.config.id.generator(this.name);
this.id = this.config.id.generator(this.name, this.source);
this.state = this.id.split(this.config.id.pseudo);
this.base = this.state.shift();
this.state = this.state.shift() || null;
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svg-sprite",
"version": "1.3.3",
"version": "1.3.4",
"author": "Joschi Kuphal <[email protected]> (https://jkphl.is)",
"description": "SVG sprites & stacks galore — A low-level Node.js module that takes a bunch of SVG files, optimizes them and bakes them into SVG sprites of several types along with suitable stylesheet resources (e.g. CSS, Sass, LESS, Stylus, etc.)",
"homepage": "https://github.com/jkphl/svg-sprite",
Expand Down Expand Up @@ -36,21 +36,21 @@
},
"dependencies": {
"mkdirp": "^0.5.1",
"async": "^2.0.0-rc.6",
"lodash": "^4.13.1",
"async": "^2.0.1",
"lodash": "^4.15.0",
"lodash.pluck": "^3.1.2",
"glob": "^7.0.5",
"xmldom": "0.1.22",
"xpath": "^0.0.23",
"vinyl": "^1.1.1",
"vinyl": "^1.2.0",
"svgo": "0.6.6",
"cssom": "^0.3.1",
"css-selector-parser": "^1.1.0",
"phantomjs-prebuilt": "^2.1.7",
"phantomjs-prebuilt": "^2.1.12",
"cssmin": "^0.4.3",
"mustache": "^2.2.1",
"js-yaml": "^3.6.1",
"yargs": "^4.7.1",
"yargs": "^4.8.1",
"winston": "^2.2.0",
"prettysize": "^0.0.3"
},
Expand All @@ -63,7 +63,7 @@
"rimraf": "",
"vinyl-fs": "^2.4.3",
"svg2png": "jkphl/svg2png",
"image-diff": "^1.6.0",
"image-diff": "^1.6.3",
"node-sass": "^3.8.0",
"less": "^2.7.1",
"stylus": "^0.54.5",
Expand Down

0 comments on commit a0acc4e

Please sign in to comment.