compass-img-delivery 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/img-delivery/functions.rb +56 -18
  2. metadata +2 -1
@@ -6,7 +6,7 @@ module Compass::ImgDelivery
6
6
 
7
7
  def scss_rule(basename, mime_type, data_uri)
8
8
  rule = <<-SCSS
9
- .#{basename.downcase} {
9
+ %#{basename.downcase} {
10
10
  background-image: url("data:#{mime_type};base64,#{data_uri}");
11
11
  background-repeat: no-repeat;
12
12
  }
@@ -16,7 +16,7 @@ module Compass::ImgDelivery
16
16
 
17
17
  def fallback_scss_rule(basename, img_dir)
18
18
  rule = <<-SCSS
19
- .#{basename.downcase} {
19
+ %#{basename.downcase} {
20
20
  background-image: url("#{img_dir}/#{basename}.png");
21
21
  background-repeat: no-repeat;
22
22
  }
@@ -46,27 +46,38 @@ module Sass::Script::Functions
46
46
  include Compass::ImgDelivery
47
47
 
48
48
  def img_delivery(images_dir = '', css_dir = '', js_dir = '')
49
- directory_path = Compass.configuration.images_path + "#{images_dir.value}"
49
+
50
+ begin
51
+
52
+ directory_path = Compass.configuration.images_path + images_dir.value
53
+ png_path = directory_path + 'png/'
50
54
  svg_scss_content = ""
51
55
  png_scss_content = ""
52
56
  fallback_scss_content = ""
57
+ oupput_types = ['svg', 'png', 'fallback']
58
+ css_basename = File.basename(css_dir.value) || 'images'
59
+ css_dir = File.dirname(css_dir.value) + '/'
60
+
61
+
62
+ Dir.mkdir(png_path) unless Dir.exists?(png_path)
53
63
 
54
64
  Dir["#{directory_path}svg/*.svg"].each do |file|
55
65
  basename = File.basename(file, ".svg")
56
- outputfile = directory_path + 'png/' + basename + '.png'
66
+ outputfile = png_path + basename + '.png'
57
67
 
58
68
  # svg data-uri css rule
59
69
  svg_data_uri = img2b64(file)
60
70
  svg_scss_content << scss_rule(basename, "image/svg+xml", svg_data_uri )
61
71
 
62
- # make png/ dir if not exists
63
- mkdir_if_not_exists(outputfile)
64
-
65
- # create png files with svg2png command
66
- cmd = "svg2png #{file} #{outputfile}"
67
- puts "Writing #{outputfile}"
68
- # exec shell command
69
- shell = %x[ #{cmd} ]
72
+ if !File.exists?(file)
73
+ # make png/ dir if not exists
74
+ mkdir_if_not_exists(outputfile)
75
+ # create png files with svg2png command
76
+ cmd = "svg2png #{file} #{outputfile}"
77
+ puts "Writing #{outputfile}"
78
+ # exec shell command
79
+ shell = %x[ #{cmd} ]
80
+ end
70
81
 
71
82
  # png data-uri css rule
72
83
  png_data_uri = img2b64(outputfile)
@@ -77,13 +88,35 @@ module Sass::Script::Functions
77
88
  fallback_scss_content << fallback_scss_rule(basename, fallback_img_path)
78
89
  end
79
90
 
80
- # create svg.scss file
81
- css_path = Compass.configuration.css_path + "#{css_dir.value}"
82
- svg_scss_file = css_path + '-svg.css.scss'
83
- png_scss_file = css_path + '-png.css.scss'
84
- fallback_scss_file = css_path + '-fallback.css.scss'
85
91
 
86
- mkdir_if_not_exists(svg_scss_file)
92
+ src_css_path = Compass.configuration.css_path + css_dir + '.img-delivery/'
93
+ css_path = Compass.configuration.css_path + css_dir
94
+
95
+ # CREATE SOURCE PARTIALS
96
+
97
+ src_style_basename = "_#{css_basename}"
98
+ src_svg_scss_file = src_css_path + "#{src_style_basename}-svg.scss"
99
+ src_png_scss_file = src_css_path + "#{src_style_basename}-png.scss"
100
+ src_fallback_scss_file = src_css_path + "#{src_style_basename}-fallback.scss"
101
+
102
+ # mkdir_if_not_exists(svg_scss_file)
103
+ mkdir_if_not_exists(src_svg_scss_file)
104
+
105
+ # copy original file, to import different files for each image-type-stylesheet
106
+ src_file = css_path + "_#{css_basename}.scss"
107
+ FileUtils.cp(src_file, src_svg_scss_file)
108
+ FileUtils.cp(src_file, src_png_scss_file)
109
+ FileUtils.cp(src_file, src_fallback_scss_file)
110
+
111
+ # CREATE FINAL SCSS
112
+ svg_scss_content << "@import '#{src_css_path}_#{css_basename}-svg.scss';"
113
+ png_scss_content << "@import '#{src_css_path}_#{css_basename}-png.scss';"
114
+ fallback_scss_content << "@import '#{src_css_path}_#{css_basename}-fallback.scss';"
115
+
116
+ # output paths
117
+ svg_scss_file = css_path + "#{css_basename}-svg.css.scss"
118
+ png_scss_file = css_path + "#{css_basename}-png.css.scss"
119
+ fallback_scss_file = css_path + "#{css_basename}-fallback.css.scss"
87
120
 
88
121
  # create svg.scss file
89
122
  write_file(svg_scss_file, svg_scss_content)
@@ -102,6 +135,11 @@ module Sass::Script::Functions
102
135
 
103
136
  mkdir_if_not_exists(js_file)
104
137
  write_file(js_file, js_content)
138
+
139
+ rescue Exception => e
140
+ puts 'Ups! There was an error:'
141
+ puts e.inspect
142
+ end
105
143
  end
106
144
 
107
145
  def img2b64(file)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compass-img-delivery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -65,3 +65,4 @@ specification_version: 3
65
65
  summary: Compass plugin for managing and delivering sharp vector images to all devices
66
66
  and browsers.
67
67
  test_files: []
68
+ has_rdoc: false