Class: SmallVictories::Compiler
- Inherits:
-
Object
- Object
- SmallVictories::Compiler
- Defined in:
- lib/smallvictories/compiler.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Instance Method Summary collapse
- #compile_css ⇒ Object
- #compile_html ⇒ Object
- #compile_js ⇒ Object
-
#initialize(attributes = {}) ⇒ Compiler
constructor
A new instance of Compiler.
- #inline_html ⇒ Object
- #liquid ⇒ Object
- #minify_css ⇒ Object
- #minify_js ⇒ Object
- #package(bundles = [config.stylesheets, config.javascripts], options = {}) ⇒ Object
- #prefix_css ⇒ Object
- #premail ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Compiler
Returns a new instance of Compiler.
10 11 12 |
# File 'lib/smallvictories/compiler.rb', line 10 def initialize attributes={} self.config = attributes[:config] end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
8 9 10 |
# File 'lib/smallvictories/compiler.rb', line 8 def config @config end |
Instance Method Details
#compile_css ⇒ Object
14 15 16 |
# File 'lib/smallvictories/compiler.rb', line 14 def compile_css package [config.stylesheets] if config.compile_css end |
#compile_html ⇒ Object
22 23 24 |
# File 'lib/smallvictories/compiler.rb', line 22 def compile_html liquid if config.compile_html end |
#compile_js ⇒ Object
18 19 20 |
# File 'lib/smallvictories/compiler.rb', line 18 def compile_js package [config.javascripts] if config.compile_js end |
#inline_html ⇒ Object
26 27 28 |
# File 'lib/smallvictories/compiler.rb', line 26 def inline_html premail end |
#liquid ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/smallvictories/compiler.rb', line 30 def liquid begin Liquid::Template.file_system = Liquid::LocalFileSystem.new(config.full_includes_path) layout_path = config.full_layout_path if File.exists?(layout_path) layout_file = File.open(layout_path).read layout = Liquid::Template.parse(layout_file) end rescue => e SmallVictories.logger.error "Liquid Error\n#{e}\n#{e.backtrace.first}" return end Dir.glob([File.join(config.full_source_path, '**/*.html'), File.join(config.full_source_path, '**/*.liquid')]) do |path| begin pathname = Pathname.new(path) file_name = pathname.basename.to_s.split('.').first folder_path = pathname.dirname.to_s.gsub(config.full_source_path, '') full_output_path = File.join(config.full_destination_path, folder_path) next if file_name =~ /^_/ # do not render partials or layout file = File.open(path).read liquid = Liquid::Template.parse(file) stylesheet_path = Pathname.new(config.full_destination_path) file_path = Pathname.new(full_output_path) relative_path = stylesheet_path.relative_path_from(file_path) data = { 'config' => { 'stylesheet' => File.join(relative_path, config.stylesheets.last), 'javascript' => File.join(relative_path, config.javascripts.last) } } content = liquid.render(data) output_file_name = file_name.concat('.html') output_path = File.join(config.full_destination_path, output_file_name) if layout html = layout.render(data.merge('content_for_layout' => liquid.render)) else html = liquid.render(data) end Dir.mkdir(full_output_path) unless File.exists?(full_output_path) File.open(File.join(full_output_path, output_file_name), 'w') { |file| file.write(html) } SmallVictories.logger.info "compiled #{File.join(config.destination, folder_path, output_file_name)}" rescue => e SmallVictories.logger.error "#{path}\n#{e}\n#{e.backtrace.first}" end end end |
#minify_css ⇒ Object
118 119 120 121 |
# File 'lib/smallvictories/compiler.rb', line 118 def minify_css package [config.stylesheets] prefix_css end |
#minify_js ⇒ Object
123 124 125 |
# File 'lib/smallvictories/compiler.rb', line 123 def minify_js package [config.javascripts], { js_compressor: :closure } end |
#package(bundles = [config.stylesheets, config.javascripts], options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/smallvictories/compiler.rb', line 76 def package bundles=[config.stylesheets, config.javascripts], ={} sprockets = Sprockets::Environment.new(config.full_source_path) do |environment| environment.gzip = true environment.logger = SmallVictories.logger environment.js_compressor = [:js_compressor] || :uglify environment.css_compressor = [:css_compressor] || :sass end sprockets.append_path('.') bundles.each do |bundle| begin if assets = sprockets.find_asset(bundle.first) FileUtils.mkpath config.full_destination_path assets.write_to File.join(config.full_destination_path, bundle.last) SmallVictories.logger.info "compiled #{File.join(config.destination, bundle.last)}" end rescue => e SmallVictories.logger.error "#{bundle.first}\n#{e}\n#{e.backtrace.first}" end end end |
#prefix_css ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/smallvictories/compiler.rb', line 98 def prefix_css begin path = File.join(config.full_destination_path, config.stylesheets.last) css = File.open(path).read prefixed = AutoprefixerRails.process(css, browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1'], cascade: false) File.open(path, 'w') { |file| file.write(prefixed.css) } sprockets = Sprockets::Environment.new(config.full_source_path) do |environment| environment.css_compressor = :yui end sprockets.append_path(config.full_destination_path) if assets = sprockets.find_asset(config.stylesheets.last) assets.write_to File.join(config.full_destination_path, config.stylesheets.last) SmallVictories.logger.info "prefixed #{File.join(config.destination,config.stylesheets.last)}" end rescue => e SmallVictories.logger.error "#{path}\n#{e}\n#{e.backtrace.first}" end end |
#premail ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/smallvictories/compiler.rb', line 127 def premail Dir.glob([File.join(config.full_destination_path, '**/*.html')]) do |path| begin premailer = Premailer.new(path, warn_level: Premailer::Warnings::SAFE) File.open(path, 'w') { |file| file.write(premailer.to_inline_css) } # Output any CSS warnings premailer.warnings.each do |w| SmallVictories.logger.warn "#{w[:message]} (#{w[:level]}) may not render properly in #{w[:clients]}" end file_name = Pathname.new(path).basename SmallVictories.logger.info "inlined #{File.join(config.destination, file_name)}" size = File.size(path) if size > 102000 SmallVictories.logger.warn "size is greater than 120kb (#{size})" else SmallVictories.logger.info "size is less than 120kb (#{size})" end rescue => e SmallVictories.logger.error "Inline Error\n#{e}" end end end |