compass-img-delivery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ require 'compass'
2
+ require 'img-delivery/functions'
3
+
4
+ Compass::Frameworks.register("img-delivery",
5
+ :stylesheets_directory => File.join(File.dirname(__FILE__), "stylesheets"),
6
+ :templates_directory => File.join(File.dirname(__FILE__), "templates")
7
+ )
@@ -0,0 +1,138 @@
1
+ require "rubygems"
2
+ require "base64"
3
+
4
+ # module Compass::ImgDelivery
5
+
6
+ # def scss_rule(basename, mime_type, data_uri)
7
+ # rule = <<-SCSS
8
+ # .#{basename.downcase} {
9
+ # background-image: url("data:#{mime_type};base64,#{data_uri}");
10
+ # background-repeat: no-repeat;
11
+ # }
12
+ # SCSS
13
+ # end
14
+
15
+ # def fallback_scss_rule(basename, img_dir)
16
+ # rule = <<-SCSS
17
+ # .#{basename.downcase} {
18
+ # background-image: url("#{img_dir}/#{basename}.png");
19
+ # background-repeat: no-repeat;
20
+ # }
21
+ # SCSS
22
+ # end
23
+
24
+ # end
25
+
26
+
27
+ module Sass::Script::Functions
28
+
29
+ def img_delivery(images_dir = '', css_dir = '', js_dir = '')
30
+ directory_path = Compass.configuration.images_path + "#{images_dir.value}"
31
+ svg_scss_content = ""
32
+ png_scss_content = ""
33
+ fallback_scss_content = ""
34
+
35
+ Dir["#{directory_path}svg/*.svg"].each do |file|
36
+ basename = File.basename(file, ".svg")
37
+ outputfile = directory_path + 'png/' + basename + '.png'
38
+ svg_data_uri = svg_base64(file)
39
+
40
+ svg_scss_content << <<-SCSS
41
+ .#{basename.downcase} {
42
+ background-image: #{svg_data_uri};
43
+ background-repeat: no-repeat;
44
+ }
45
+
46
+ SCSS
47
+
48
+ # generate pngs
49
+ # svg2png command
50
+ cmd = "svg2png #{file} #{outputfile}"
51
+ puts "Writing #{outputfile}"
52
+ shell = %x[ #{cmd} ]
53
+
54
+ png_data_uri = png_base64(outputfile)
55
+ png_scss_content << <<-SCSS
56
+ .#{basename.downcase} {
57
+ background-image: #{png_data_uri};
58
+ background-repeat: no-repeat;
59
+ }
60
+
61
+ SCSS
62
+
63
+ fallback_img_path = "/" + Compass.configuration.images_dir + "#{images_dir.value}png"
64
+ fallback_scss_content << <<-SCSS
65
+ .#{basename.downcase} {
66
+ background-image: url("#{fallback_img_path}/#{basename}.png");
67
+ background-repeat: no-repeat;
68
+ }
69
+
70
+ SCSS
71
+
72
+
73
+ if !File.exists?(outputfile)
74
+ d = directory_path + 'png'
75
+ Dir.mkdir(d) unless Dir.exists?(d)
76
+ end
77
+ end
78
+
79
+ # create svg.scss file
80
+ css_path = Compass.configuration.css_path + "#{css_dir.value}"
81
+ svg_scss_file = css_path + '-svg.css.scss'
82
+ png_scss_file = css_path + '-png.css.scss'
83
+ fallback_scss_file = css_path + '-fallback.css.scss'
84
+
85
+ if !File.exists?(svg_scss_file)
86
+ d = File.dirname(svg_scss_file)
87
+ Dir.mkdir(d) unless Dir.exists?(d)
88
+ end
89
+
90
+ File.open(svg_scss_file, 'w') do |f|
91
+ puts "Writing #{svg_scss_file}"
92
+ f.write(svg_scss_content)
93
+ end
94
+
95
+ # create png.scss file
96
+ File.open(png_scss_file, 'w') do |f|
97
+ puts "Writing #{png_scss_file}"
98
+ f.write(png_scss_content)
99
+ end
100
+
101
+ # create fallback.scss file
102
+ File.open(fallback_scss_file, 'w') do |f|
103
+ puts "Writing #{fallback_scss_file}"
104
+ f.write(fallback_scss_content)
105
+ end
106
+
107
+ # create js file
108
+ js_content = File.open(File.join(File.dirname(__FILE__), "../javascripts/img-delivery.js"), "rb")
109
+ js_content = js_content.read
110
+ js_path = Compass.configuration.javascripts_path + "#{js_dir.value}"
111
+
112
+ js_file = js_path + '.js'
113
+ if !File.exists?(js_file)
114
+ d = File.dirname(js_file)
115
+ Dir.mkdir(d) unless Dir.exists?(d)
116
+ end
117
+
118
+ File.open(js_file, 'w') do |f|
119
+ puts "Writing #{js_file}"
120
+ f.write(js_content)
121
+ end
122
+
123
+ end
124
+
125
+ def svg_base64(file)
126
+ data = Base64.encode64(File.read(file)).gsub("\n", '')
127
+ uri = "data:image/svg+xml;base64,#{data}"
128
+
129
+ "url('#{uri}')"
130
+ end
131
+
132
+ def png_base64(file)
133
+ data = Base64.encode64(File.read(file)).gsub("\n", '')
134
+ uri = "data:image/png;base64,#{data}"
135
+
136
+ "url('#{uri}')"
137
+ end
138
+ end
@@ -0,0 +1,32 @@
1
+ // JS taken from filamentgroup/unicon (https://github.com/aaronrussell/compass-rgbapng/blob/master/lib/rgbapng/functions.rb)
2
+ window.unrepeater = function( css, foo ){
3
+ // expects a css array with 3 items representing CSS paths to datasvg, datapng, urlpng
4
+ if( !css || css.length !== 3 ){
5
+ return;
6
+ }
7
+
8
+ // Thanks Modernizr & Erik Dahlstrom
9
+ var w = window,
10
+ svg = !!w.document.createElementNS && !!w.document.createElementNS('http://www.w3.org/2000/svg', 'svg').createSVGRect,
11
+
12
+ loadCSS = function( data ){
13
+ var link = w.document.createElement( "link" ),
14
+ ref = w.document.getElementsByTagName( "script" )[ 0 ];
15
+ link.rel = "stylesheet";
16
+ link.href = css[ data && svg ? 0 : data ? 1 : 2 ];
17
+ ref.parentNode.insertBefore( link, ref );
18
+ },
19
+
20
+ // Thanks Modernizr
21
+ img = new w.Image();
22
+
23
+ img.onerror = function(){
24
+ loadCSS( false );
25
+ };
26
+
27
+ img.onload = function(){
28
+ loadCSS( img.width === 1 && img.height === 1 );
29
+ };
30
+
31
+ img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
32
+ };
File without changes
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compass-img-delivery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Javier Sánchez-Marín
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: compass
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.0
30
+ description: ! 'Compass plugin for managing and delivering sharp vector images to
31
+ all devices and browsers. I''m not reinventing the wheel, this an idea of Filament
32
+ Group. Take a look at Unicon http://filamentgroup.com/lab/unicon/ '
33
+ email: javiersanchezmarin@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/img-delivery/functions.rb
39
+ - lib/img-delivery.rb
40
+ - lib/javascripts/img-delivery.js
41
+ - lib/stylesheets/_img-delivery.scss
42
+ homepage: http://github.com/vieron/compass-img-delivery
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.24
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Compass plugin for managing and delivering sharp vector images to all devices
66
+ and browsers.
67
+ test_files: []