Skip to content

Commit

Permalink
fix(plugin): issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavliko committed Jul 31, 2015
1 parent 41454d0 commit 9a6bdbf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### 1.0.1 - 2015-07-10

Fix some issues:
* [Raise a error on unknown file](https://github.com/Pavliko/postcss-svg/issues/7)
* [Absolute paths](https://github.com/Pavliko/postcss-svg/issues/8)
* [SVG names with single quotes do not get replaced](https://github.com/Pavliko/postcss-svg/issues/10)

# 1.0.0 - 2015-07-10

Initial release from [postcss-svg](https://github.com/Pavliko/postcss-svg)
13 changes: 8 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,43 @@
_ = require('lodash');

module.exports = postcss.plugin("postcss-svg", function(options) {
var SVGRegExp, funcName, silent;
var SVGRegExp, funcName, replaceRegExp, silent;
if (options == null) {
options = {};
}
funcName = options.func || 'svg';
SVGRegExp = new RegExp(funcName + "\\(\"([^\"]+)\"(,\\s*\"([^\"]+)\")?\\)");
replaceRegExp = new RegExp(funcName + "\\((\"[^\"]+\"|\'[^\']+\')(,\\s*(\"[^\"]+\"|\'[^\']+\'))?\\)");
silent = _.isBoolean(options.silent) ? options.silent : true;
if (options.debug) {
silent = false;
}
return function(style) {
SVGCache.init(options);
return style.eachDecl(/^background|^filter|^content|image$/, function(decl) {
var args, error, matches, name, params, replace, svg;
var ___, error, matches, name, params, replace, svg;
if (!decl.value) {
return;
}
if (matches = SVGRegExp.exec(decl.value.replace(/'/g, '"'))) {
replace = matches[0], args = 2 <= matches.length ? slice.call(matches, 1) : [];
name = args[0], params = 2 <= args.length ? slice.call(args, 1) : [];
___ = matches[0], name = matches[1], params = 3 <= matches.length ? slice.call(matches, 2) : [];
if (options.debug) {
console.time("Render svg " + name);
}
try {
svg = SVGCache.get(name);
} catch (_error) {
error = _error;
if (!silent) {
if (silent) {
console.info("postcss-svg: " + error);
} else {
throw decl.error(error);
}
}
if (!svg) {
return;
}
replace = replaceRegExp.exec(decl.value)[0];
decl.value = decl.value.replace(replace, svg.dataUrl(params[1]));
if (options.debug) {
console.timeEnd("Render svg " + name);
Expand Down
26 changes: 18 additions & 8 deletions dist/lib/svg_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
module.exports = {
defaultPaths: ['svg'],
init: function(options) {
var debug, filename, i, j, len, len1, myPath, ref, ref1, stat, svgOptions;
var debug, filename, i, j, len, len1, myPath, ref, ref1, stat;
debug = _.isBoolean(options.debug) && options.debug;
options.svgo || (options.svgo = false);
this.templates = {};
Expand All @@ -32,8 +32,9 @@
}
});
}
svgOptions = _.pick(options, 'defaults', 'svgo');
ref = options.paths || this.defaultPaths;
this.svgOptions = _.pick(options, 'defaults', 'svgo');
this.paths = options.paths || this.defaultPaths;
ref = this.paths;
for (i = 0, len = ref.length; i < len; i++) {
myPath = ref[i];
if (fs.existsSync(myPath)) {
Expand All @@ -42,11 +43,11 @@
ref1 = fs.readdirSync(myPath);
for (j = 0, len1 = ref1.length; j < len1; j++) {
filename = ref1[j];
this.addToIndex("" + myPath + path.sep + filename, svgOptions);
this.addToIndex("" + myPath + path.sep + filename, this.svgOptions);
}
} else {
if (stat.isFile()) {
this.addToIndex(myPath, svgOptions);
this.addToIndex(myPath, this.svgOptions);
}
}
}
Expand All @@ -69,7 +70,7 @@
error: true,
livel: 'Warning',
getMessage: function() {
return "You have some files with this basename: " + (this.paths.join(', '));
return "You have some files with this basename: " + (this.filesIndex[basename].paths.join(', '));
},
paths: [this.filesIndex[basename].path, svg.path]
};
Expand All @@ -79,8 +80,11 @@
return this.filesIndex[filePath] = this.filesIndex[filePath.slice(0, -4)] = svg;
}
},
get: function(identifier) {
get: function(identifier, second) {
var ids, link, ref, svg;
if (second == null) {
second = false;
}
ref = identifier.split('#'), link = ref[0], ids = 2 <= ref.length ? slice.call(ref, 1) : [];
if (svg = this.filesIndex[link]) {
if (svg.error) {
Expand All @@ -89,7 +93,13 @@
return svg.svgFor(ids);
}
} else {
throw "'" + link + "' not found in SVG csche";
if (second) {
throw "'" + link + "' not found in SVG csche (paths: " + (this.paths.join(', ')) + ")";
} else {
identifier = (identifier.indexOf('.svg') === -1 ? identifier + ".svg" : identifier);
this.addToIndex(identifier, this.svgOptions);
return this.get(identifier, true);
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion example/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ header h1 a:after {
content: '';
width: 50px;
height: 50px;
content: svg("ei#sc-github", "[fill]: black");
content: svg('ei#sc-github', '[fill]: black');
}

header ul {
Expand Down
12 changes: 8 additions & 4 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ _ = require('lodash')
module.exports = postcss.plugin "postcss-svg", (options = {}) ->
funcName = options.func || 'svg'
SVGRegExp = new RegExp("#{funcName}\\(\"([^\"]+)\"(,\\s*\"([^\"]+)\")?\\)")
replaceRegExp = new RegExp("#{funcName}\\((\"[^\"]+\"|\'[^\']+\')(,\\s*(\"[^\"]+\"|\'[^\']+\'))?\\)")
silent = if _.isBoolean(options.silent) then options.silent else true
silent = false if options.debug

(style) ->
SVGCache.init(options)

style.eachDecl /^background|^filter|^content|image$/, (decl) ->
return unless decl.value
if matches = SVGRegExp.exec(decl.value.replace(/'/g, '"'))
[replace, args...] = matches
[name, params...] = args
[___, name, params...] = matches
console.time ("Render svg #{name}") if options.debug
try
svg = SVGCache.get(name)
catch error
throw decl.error(error) unless silent
if silent
console.info "postcss-svg: #{error}"
else
throw decl.error(error)
return unless svg
replace = replaceRegExp.exec(decl.value)[0]
decl.value = decl.value.replace replace, svg.dataUrl(params[1])
console.timeEnd ("Render svg #{name}") if options.debug
return
24 changes: 15 additions & 9 deletions lib/svg_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fs = require('fs')

module.exports =
defaultPaths: ['svg']

init: (options) ->
debug = _.isBoolean(options.debug) && options.debug
options.svgo ||= false
Expand All @@ -21,15 +21,16 @@ module.exports =
postfix: '-icon'
prefix: 'ei-'

svgOptions = _.pick options, 'defaults', 'svgo'
@svgOptions = _.pick options, 'defaults', 'svgo'
@paths = options.paths || @defaultPaths

for myPath in (options.paths || @defaultPaths)
for myPath in @paths
if fs.existsSync(myPath)
stat = fs.statSync(myPath)
if stat.isDirectory()
@addToIndex("#{myPath}#{path.sep}#{filename}", svgOptions) for filename in fs.readdirSync(myPath)
@addToIndex("#{myPath}#{path.sep}#{filename}", @svgOptions) for filename in fs.readdirSync(myPath)
else
@addToIndex(myPath, svgOptions) if stat.isFile()
@addToIndex(myPath, @svgOptions) if stat.isFile()
console.timeEnd('Index generation') if debug

addToIndex: (filePath, options = {}) ->
Expand All @@ -45,20 +46,25 @@ module.exports =
else
error: true
livel: 'Warning'
getMessage: -> "You have some files with this basename: #{@paths.join(', ')}"
getMessage: -> "You have some files with this basename: #{@filesIndex[basename].paths.join(', ')}"
paths: [@filesIndex[basename].path, svg.path]
else
@filesIndex[basename] = @filesIndex[basenameWithExt] = svg

@filesIndex[filePath] = @filesIndex[filePath[0..-5]] = svg

get: (identifier)->
get: (identifier, second = false)->
[link, ids...] = identifier.split('#')
# console.log Object.keys(@filesIndex), "#{link}", ids, @filesIndex[link]

if svg = @filesIndex[link]
if svg.error
throw svg.getMessage()
else
svg.svgFor(ids)
else
throw "'#{link}' not found in SVG csche"
if second
throw "'#{link}' not found in SVG csche (paths: #{@paths.join(', ')})"
else
identifier = (if identifier.indexOf('.svg') == -1 then "#{identifier}.svg" else identifier)
@addToIndex identifier, @svgOptions
@get(identifier, true)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-svg",
"version": "1.0.0",
"version": "1.0.1",
"description": "Postcss plugin for insert inline SVG to CSS",
"main": "dist/index.js",
"repository": {
Expand Down

0 comments on commit 9a6bdbf

Please sign in to comment.