require yui compressor
[shapado:piglops-shapado.git] / lib / jobs / themes.rb
1 require "yui/compressor"
2 module Jobs
3   class Themes
4     extend Jobs::Base
5
6     def self.generate_stylesheet(theme_id)
7       theme = Theme.find(theme_id)
8       css = StringIO.new
9       template_file = File.join(Rails.root,"lib","sass","theme_template.scss")
10
11       buffer = self.define_vars(theme) << File.read(template_file) << "\n" << theme.custom_css || ""
12
13       theme.last_error = ""
14       2.times do
15         template = Sass::Engine.new(buffer,
16                                    {:style => Sass::Plugin.options[:style],
17                                     :syntax => :scss,
18                                     :cache => false,
19                                     :load_paths => [File.join(Rails.root,"lib","sass"), "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"]})
20
21         compiled_css = ""
22         begin
23           compiled_css = template.render
24         rescue => e
25           last_error = e.to_s
26           puts "Error processing #{theme_id}: #{last_error}"
27           theme.last_error = last_error
28
29           buffer = self.define_vars(theme) << File.read(template_file) << "\n"
30         end
31
32         if theme.last_error.empty?
33           if Rails.env == "production"
34             css << YUI::CssCompressor.new.compress(compiled_css)
35           else
36             css << compiled_css
37           end
38
39           break
40         end
41       end
42
43       theme.stylesheet = css
44       theme.stylesheet["extension"] = "css"
45       theme.stylesheet["content_type"] = "text/css"
46       theme.ready = true
47       theme.increment_version
48       theme.save
49     end
50
51     private
52     def self.define_vars(theme)
53 %@
54 $has_bg_image: #{theme.has_bg_image?};
55 $bg_color: ##{theme.bg_color.to_s.gsub(/#/, "")};
56 $fg_color: ##{theme.fg_color.to_s.gsub(/#/, "")};
57 $bg_image_url: '/_files/themes/bg_image/#{theme.group_id}/#{theme.id}/#{theme.version}.png';
58 $topbar_color: ##{theme.topbar_color.to_s.gsub(/#/, "")};
59 $logo_url: '/_files/groups/logo/#{theme.group_id}/#{theme.version}.png';
60 $view_bg_color: ##{theme.view_bg_color.to_s.gsub(/#/, "")};
61 $brand_color: ##{theme.brand_color.to_s.gsub(/#/, "")};
62 $fluid: #{theme.fluid};
63 $bg_shadow:      #999;
64 @
65     end
66   end
67 end