Module: Jekyll::Compressor

Included in:
Page, Post, StaticFile
Defined in:
lib/jekyll-minifier.rb

Instance Method Summary collapse

Instance Method Details

#exclude?(dest, dest_path) ⇒ Boolean

Returns:

  • (Boolean)

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll-minifier.rb', line 6

def exclude?(dest, dest_path)
  res = false
  file_name = dest_path.slice(dest.length+1..dest_path.length)
  exclude = @site.config['jekyll-minifier'] && @site.config['jekyll-minifier']['exclude']
  if exclude
    if exclude.is_a? String
      exclude = [exclude]
    end
    exclude.each do |e|
      if e == file_name || File.fnmatch(e, file_name)
        res = true
        break
      end
    end
  end
  res
end

#output_css(path, content) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/jekyll-minifier.rb', line 45

def output_css(path, content)
  compressor = YUI::CssCompressor.new({ :line_break =>  -1 })
  output_file(path, compressor.compress(content))
rescue compressor::RuntimeError => e
  warn "Warning: parse error in #{path}. Don't panic - copying initial file"
  warn "Details: #{e.message.strip}"
  output_file(path, content)
end

#output_file(dest, content) ⇒ Object


24
25
26
27
28
29
# File 'lib/jekyll-minifier.rb', line 24

def output_file(dest, content)
  FileUtils.mkdir_p(File.dirname(dest))
  File.open(dest, 'w') do |f|
    f.write(content)
  end
end

#output_html(path, content) ⇒ Object


31
32
33
34
# File 'lib/jekyll-minifier.rb', line 31

def output_html(path, content)
  compressor = HtmlCompressor::Compressor.new({ :compress_css => true, :compress_javascript => true, :css_compressor => :yui, :javascript_compressor => :yui })
  output_file(path, compressor.compress(content))
end

#output_js(path, content) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/jekyll-minifier.rb', line 36

def output_js(path, content)
  compressor = YUI::JavaScriptCompressor.new({ :munge => true, :preserve_semicolons => true, :optimize => true, :line_break => nil })
  output_file(path, compressor.compress(content))
rescue compressor::ParseError => e
  warn "Warning: parse error in #{path}. Don't panic - copying initial file"
  warn "Details: #{e.message.strip}"
  output_file(path, content)
end