compass-svg-templates 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ce22fbc07b592fdd745dc5fdbf2db4d4e249fa29
4
+ data.tar.gz: d0adbbf48a527b538c6f0d4f892d49bb94c0a72b
5
+ SHA512:
6
+ metadata.gz: 81e5c0197fd8cdbdc3e4d7658951316c16c1f139d80d51e3a4843d5a04c7097ad820685922b8cac96f67ede856f0c7b2c2ed3f2e1facd2d379c85f7edcb62646
7
+ data.tar.gz: 177acc31283a6454762fa5bbb6b9708ac5ddf2f706b0f447d87765e276d13fe2981d774caef289da59721a2a4d48e5d28e862fd5177f49a9a1b5764910eb67d8
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Thomas Bachem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,125 @@
1
+ Compass SVG Templates
2
+ ===========================================================================================
3
+
4
+ Use variables in SVGs to modify images on-the-fly
5
+ -------------------------------------------------------------------------------------------
6
+
7
+ This [Compass](http://compass-style.org) extension can read SVG files, replace variables
8
+ in these images (e.g. to manipulate colors) and optionally also render them as PNG images
9
+ e.g. for Internet Explorer <= 8.
10
+
11
+ ### Installation
12
+
13
+ ```sh
14
+ sudo gem install compass-svg-templates
15
+ ```
16
+
17
+ If you want to make use of the PNG rendering functionality as well, you need to have
18
+ librsvg, ImageMagick and RMagick installed. You can install those on OS X using
19
+ [Homebrew](http://brew.sh) like this:
20
+ ```sh
21
+ brew install librsvg
22
+ brew install imagemagick --with-librsvg
23
+ sudo ARCHFLAGS="-arch x86_64" gem install rmagick
24
+ ```
25
+
26
+ ### Usage
27
+
28
+ To use the extension in your existing Compass project, add the following to your *config.rb* file:
29
+ ```ruby
30
+ require 'svg-templates'
31
+ ```
32
+
33
+ To make use of the shipped mixins in addition to the functions, add this to your SCSS file:
34
+ ```scss
35
+ @import 'svg-templates'
36
+ ```
37
+
38
+ ### Example
39
+
40
+ SVG template (*img/circle.svg*):
41
+ ```svg
42
+ <?xml version="1.0" encoding="utf-8"?>
43
+ <svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
44
+ <circle stroke="#{$stroke||black}" fill="#{$fill||white}" cx="50" cy="50" r="40" stroke-width="3" />
45
+ </svg>
46
+ ```
47
+
48
+ SCSS (*scss/styles.scss*):
49
+ ```scss
50
+ .myselector {
51
+ background-image: svg-template('circle.svg', $stroke: red);
52
+ }
53
+ ```
54
+
55
+ Compiled CSS (*css/styles.css*):
56
+ ```css
57
+ .myselector {
58
+ background-image: url('styles-circle.12345.svg?12345');
59
+ }
60
+ ```
61
+
62
+ Generated SVG (*css/styles-circle.12345.svg*):
63
+ ```svg
64
+ <?xml version="1.0" encoding="utf-8"?>
65
+ <svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50">
66
+ <circle stroke="red" fill="white" cx="50" cy="50" r="40" stroke-width="3" />
67
+ </svg>
68
+ ```
69
+
70
+ ### Functions
71
+
72
+ #### `svg-template($path, $variables...)`
73
+
74
+ Reads the SVG file from `$path` (relative to Compass' image path), optionally replaces
75
+ variables within the SVG, puts the resulting SVG file in the CSS directory and returns
76
+ a `url(...)` statement that points there.
77
+
78
+ #### `svg-template-inline($path, $variables...)`
79
+
80
+ Like `svg-template` but returns the generated SVG inlined as `url('data:...')`.
81
+
82
+ #### `svg-template-png($path, $variables...)`
83
+
84
+ Generates the SVG like `svg-template` but uses RMagick/ImageMagick to render it into
85
+ a PNG file and returns the `url(...)` statement pointing to that PNG file.
86
+
87
+ #### `svg-template-png-inline($path, $variables...)`
88
+
89
+ Like `svg-template-png` but returns the generated PNG inlined as `url('data:...')`.
90
+
91
+ ### Mixins
92
+
93
+ The shipped mixins do automatically include a PNG fallback.
94
+
95
+ #### `svg-template-background($path, $variables...)`
96
+
97
+ ```scss
98
+ @mixin svg-template-background($path, $variables...) {
99
+ background-image: svg-template-png($path, keywords($variables));
100
+ background-image: svg-template($path, keywords($variables));
101
+ }
102
+ ```
103
+
104
+ #### `svg-template-background-inline($path, $variables...)`
105
+
106
+ ```scss
107
+ @mixin svg-template-background-inline($path, $variables...) {
108
+ background-image: svg-template-png-inline($path, keywords($variables));
109
+ background-image: svg-template-inline($path, keywords($variables));
110
+ }
111
+ ```
112
+
113
+ ### Configuration Settings
114
+
115
+ #### `$svg-templates-cache-buster` (default: `auto`)
116
+
117
+ Set this variable to `null` or an empty string to prevent the extension from
118
+ appending a cache buster to the query string or provide a static cache buster
119
+ value to be used. The default `auto` tells the extension to use the file's
120
+ shortened MD5 hash as the cache buster.
121
+
122
+ #### `$svg-templates-minify` (default: `1`)
123
+
124
+ Set this variable to `0` to disable automatic SVG minification (stripping of
125
+ XML prolog, doctype, comments and redundant whitespace).
@@ -0,0 +1,4 @@
1
+ require 'svg-templates/sass_extensions'
2
+
3
+ extension_path = File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ Compass::Frameworks.register('svg-templates', :path => extension_path)
@@ -0,0 +1,159 @@
1
+ module Sass::Script::Functions
2
+
3
+ # Allow to supply variables as keywords or as a map
4
+ declare :svg_template, [:path], :var_kwargs => true
5
+ declare :svg_template, [:path, :variables]
6
+
7
+ declare :svg_template_inline, [:path], :var_kwargs => true
8
+ declare :svg_template_inline, [:path, :variables]
9
+
10
+ declare :svg_template_png, [:path], :var_kwargs => true
11
+ declare :svg_template_png, [:path, :variables]
12
+
13
+ declare :svg_template_png_inline, [:path], :var_kwargs => true
14
+ declare :svg_template_png_inline, [:path, :variables]
15
+
16
+ def svg_template(path, variables = {})
17
+ real_path = File.join(Compass.configuration.images_path, path.value)
18
+
19
+ # Process SVG
20
+ data = read_svg_file(real_path)
21
+ data = replace_svg_variables(data, variables)
22
+
23
+ # Generate filename
24
+ css_basepath = Pathname.new(options[:css_filename].to_s).dirname.sub_ext('').to_s
25
+ css_filename = Pathname.new(options[:css_filename].to_s).basename.sub_ext('').to_s
26
+ svg_filename = Pathname.new(real_path).basename.sub_ext('').to_s
27
+ hash = Digest::MD5.hexdigest(data)[0, 5]
28
+ svg_basename = "#{css_filename}-#{svg_filename}.#{hash}.svg"
29
+
30
+ # Write SVG image file
31
+ File.write(css_basepath + '/' + svg_basename, minify_svg_data(data))
32
+
33
+ url = handle_cache_buster(svg_basename, hash)
34
+ unquoted_string("url('#{url}')")
35
+ end
36
+
37
+ def svg_template_inline(path, variables = {})
38
+ real_path = File.join(Compass.configuration.images_path, path.value)
39
+
40
+ # Process SVG
41
+ data = read_svg_file(real_path)
42
+ data = replace_svg_variables(data, variables)
43
+ data = minify_svg_data(data)
44
+
45
+ # Inline SVG image
46
+ data = [data].flatten.pack('m').gsub("\n", '')
47
+ url = "data:image/svg+xml;base64,#{data}"
48
+ unquoted_string("url('#{url}')")
49
+ end
50
+
51
+ def svg_template_png(path, variables = {})
52
+ real_path = File.join(Compass.configuration.images_path, path.value)
53
+
54
+ # Process SVG
55
+ data = read_svg_file(real_path)
56
+ data = replace_svg_variables(data, variables)
57
+
58
+ # Generate filename
59
+ css_basepath = Pathname.new(options[:css_filename].to_s).dirname.sub_ext('').to_s
60
+ css_filename = Pathname.new(options[:css_filename].to_s).basename.sub_ext('').to_s
61
+ svg_filename = Pathname.new(real_path).basename.sub_ext('').to_s
62
+ hash = Digest::MD5.hexdigest(data)[0, 5]
63
+ png_basename = "#{css_filename}-#{svg_filename}.#{hash}.png"
64
+
65
+ # Render SVG as PNG image file using ImageMagick
66
+ img = load_svg_image_data(data)
67
+ img.write(css_basepath + '/' + png_basename)
68
+
69
+ url = handle_cache_buster(png_basename, hash)
70
+ unquoted_string("url('#{url}')")
71
+ end
72
+
73
+ def svg_template_png_inline(path, variables = {})
74
+ real_path = File.join(Compass.configuration.images_path, path.value)
75
+
76
+ # Process SVG
77
+ data = read_svg_file(real_path)
78
+ data = replace_svg_variables(data, variables)
79
+
80
+ # Render SVG as PNG image file using ImageMagick
81
+ img = load_svg_image_data(data)
82
+ data = img.to_blob()
83
+
84
+ # Inline SVG image
85
+ data = [data].flatten.pack('m').gsub("\n", '')
86
+ url = "data:image/svg+xml;base64,#{data}"
87
+ unquoted_string("url('#{url}')")
88
+ end
89
+
90
+ private
91
+
92
+ def get_config_setting(name, default = nil)
93
+ # Read config setting from a scoped or global variable
94
+ setting = environment.caller.var(name)
95
+ if setting.nil?
96
+ default
97
+ else
98
+ setting.value
99
+ end
100
+ end
101
+
102
+ def read_svg_file(real_path)
103
+ if File.readable?(real_path)
104
+ File.open(real_path, "rb") {|io| io.read}
105
+ else
106
+ raise Compass::Error, "File not found or cannot be read: #{real_path}"
107
+ end
108
+ end
109
+
110
+ def replace_svg_variables(data, variables)
111
+ # Cast Sass map to hash
112
+ if variables.respond_to?(:to_h)
113
+ variables = variables.to_h
114
+ end
115
+
116
+ # Replace variables
117
+ variables.each do |key, value|
118
+ data = data.gsub('#{$' + key.to_s + '}', value.to_s)
119
+ end
120
+
121
+ # Replace undefined variables by their defaults or strip them entirely
122
+ data.gsub(/#\{\$[a-z_][a-zA-Z0-9_]*(?:\|\|(.+?))?\}/, '\1')
123
+ end
124
+
125
+ def minify_svg_data(data)
126
+ if get_config_setting('svg-templates-minify', 1) == 1
127
+ # Strip XML prolog, as browsers don't need it when a MIME type is specified
128
+ data = data.gsub(/^<\?xml\s.+?\?>/, '')
129
+ # Strip doctype
130
+ data = data.gsub(/^<!DOCTYPE\s.+?>/, '')
131
+ # Strip comments
132
+ data = data.gsub(/<!--.*?-->/, '')
133
+ # Compress consecutive whitespace
134
+ data = data.gsub(/[\s\r\n]+/, ' ')
135
+ else
136
+ data
137
+ end
138
+ end
139
+
140
+ def load_svg_image_data(data)
141
+ begin
142
+ require 'RMagick'
143
+ rescue LoadError
144
+ raise Compass::Error, "Please install RMagick to make use of the PNG rendering functionality."
145
+ end
146
+ begin
147
+ Magick::Image.from_blob(data).first
148
+ rescue
149
+ raise Compass::Error, "SVG file #{real_path} cannot be processed – probably invalid?"
150
+ end
151
+ end
152
+
153
+ def handle_cache_buster(url, hash)
154
+ cache_buster = get_config_setting('svg-templates-cache-buster', 'auto')
155
+ cache_buster = hash if cache_buster == 'auto'
156
+ cache_buster.to_s.length > 0 ? url + '?' + cache_buster.to_s : url
157
+ end
158
+
159
+ end
@@ -0,0 +1,14 @@
1
+ $svg-templates-cache-buster: auto !default;
2
+ $svg-templates-minify: 1 !default;
3
+
4
+ @mixin svg-template-background($path, $variables...) {
5
+ background-image: svg-template-png($path, keywords($variables));
6
+ // Use a second background-image so IE <= 8 won't parse this property
7
+ background-image: svg-template($path, keywords($variables)), none;
8
+ }
9
+
10
+ @mixin svg-template-background-inline($path, $variables...) {
11
+ background-image: svg-template-png-inline($path, keywords($variables));
12
+ // Use a second background-image so IE <= 8 won't parse this property
13
+ background-image: svg-template-inline($path, keywords($variables)), none;
14
+ }
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compass-svg-templates
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Bachem
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sass
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: compass
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This Compass extension can read SVG files, replace variables in these
42
+ images (e.g. to manipulate colors) and optionally render them as PNG images.
43
+ email:
44
+ - mail@thomasbachem.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE
50
+ - README.md
51
+ - lib/svg-templates.rb
52
+ - lib/svg-templates/sass_extensions.rb
53
+ - stylesheets/_svg-templates.scss
54
+ homepage: http://github.com/thomasbachem/compass-svg-templates
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.2.0
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Use variables in SVG files to modify CSS images on-the-fly
78
+ test_files: []