diff --git a/CHANGELOG.md b/CHANGELOG.md index b645427..60afc74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/dist/index.js b/dist/index.js index a967b2a..b414fbf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9,12 +9,13 @@ _ = 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; @@ -22,13 +23,12 @@ 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); } @@ -36,13 +36,16 @@ 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); diff --git a/dist/lib/svg_cache.js b/dist/lib/svg_cache.js index c4f84e5..65497b0 100644 --- a/dist/lib/svg_cache.js +++ b/dist/lib/svg_cache.js @@ -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 = {}; @@ -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)) { @@ -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); } } } @@ -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] }; @@ -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) { @@ -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); + } } } }; diff --git a/example/style.css b/example/style.css index 07a431c..354350b 100644 --- a/example/style.css +++ b/example/style.css @@ -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 { diff --git a/index.coffee b/index.coffee index 7d7e25f..bc2a1b1 100644 --- a/index.coffee +++ b/index.coffee @@ -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 diff --git a/lib/svg_cache.coffee b/lib/svg_cache.coffee index 0c218e8..610a60c 100644 --- a/lib/svg_cache.coffee +++ b/lib/svg_cache.coffee @@ -5,7 +5,7 @@ fs = require('fs') module.exports = defaultPaths: ['svg'] - + init: (options) -> debug = _.isBoolean(options.debug) && options.debug options.svgo ||= false @@ -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 = {}) -> @@ -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) diff --git a/package.json b/package.json index 6d2974d..3aab198 100644 --- a/package.json +++ b/package.json @@ -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": {