Module: Jekyll::Compressor

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

Instance Method Summary collapse

Instance Method Details

#output_compressed(path, context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/jekyll-minifier.rb', line 15

def output_compressed(path, context)
  case File.extname(path)
    when '.js'
      if path.end_with?('.min.js')
        output_file(path, context)
      else
        output_js(path, context)
      end
    when '.json'
      output_json(path, context)
    when '.css'
      if path.end_with?('.min.css')
        output_file(path, context)
      else
        output_css(path, context)
      end
    else
      output_html(path, context)
  end
end

#output_css(path, content) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jekyll-minifier.rb', line 129

def output_css(path, content)
  if ( ENV['JEKYLL_ENV'] == "production" )
    opts       = @site.config['jekyll-minifier']
    compress = true
    if ( !opts.nil? )
      compress    = opts['compress_css']               if opts.has_key?('compress_css')
    end
    if ( compress )
      compressor = CSSminify2.new()
      output_file(path, compressor.compress(content))
    else
      output_file(path, content)
    end
  else
    output_file(path, content)
  end

end

#output_file(dest, content) ⇒ Object



8
9
10
11
12
13
# File 'lib/jekyll-minifier.rb', line 8

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



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
75
76
77
78
79
80
81
82
83
# File 'lib/jekyll-minifier.rb', line 36

def output_html(path, content)
  if ( ENV['JEKYLL_ENV'] == "production" )
    html_args = { remove_comments: true, compress_css: true, compress_javascript: true, preserve_patterns: [] }
    js_args = {}

    opts = @site.config['jekyll-minifier']
    if ( !opts.nil? )
      # Javascript Arguments
      js_args[:uglifier_args] = Hash[opts['uglifier_args'].map{|(k,v)| [k.to_sym,v]}] if opts.has_key?('uglifier_args')

      # HTML Arguments
      html_args[:remove_spaces_inside_tags]   = opts['remove_spaces_inside_tags']  if opts.has_key?('remove_spaces_inside_tags')
      html_args[:remove_multi_spaces]         = opts['remove_multi_spaces']        if opts.has_key?('remove_multi_spaces')
      html_args[:remove_comments]             = opts['remove_comments']            if opts.has_key?('remove_comments')
      html_args[:remove_intertag_spaces]      = opts['remove_intertag_spaces']     if opts.has_key?('remove_intertag_spaces')
      html_args[:remove_quotes]               = opts['remove_quotes']              if opts.has_key?('remove_quotes')
      html_args[:compress_css]                = opts['compress_css']               if opts.has_key?('compress_css')
      html_args[:compress_javascript]         = opts['compress_javascript']        if opts.has_key?('compress_javascript')
      html_args[:simple_doctype]              = opts['simple_doctype']             if opts.has_key?('simple_doctype')
      html_args[:remove_script_attributes]    = opts['remove_script_attributes']   if opts.has_key?('remove_script_attributes')
      html_args[:remove_style_attributes]     = opts['remove_style_attributes']    if opts.has_key?('remove_style_attributes')
      html_args[:remove_link_attributes]      = opts['remove_link_attributes']     if opts.has_key?('remove_link_attributes')
      html_args[:remove_form_attributes]      = opts['remove_form_attributes']     if opts.has_key?('remove_form_attributes')
      html_args[:remove_input_attributes]     = opts['remove_input_attributes']    if opts.has_key?('remove_input_attributes')
      html_args[:remove_javascript_protocol]  = opts['remove_javascript_protocol'] if opts.has_key?('remove_javascript_protocol')
      html_args[:remove_http_protocol]        = opts['remove_http_protocol']       if opts.has_key?('remove_http_protocol')
      html_args[:remove_https_protocol]       = opts['remove_https_protocol']      if opts.has_key?('remove_https_protocol')
      html_args[:preserve_line_breaks]        = opts['preserve_line_breaks']       if opts.has_key?('preserve_line_breaks')
      html_args[:simple_boolean_attributes]   = opts['simple_boolean_attributes']  if opts.has_key?('simple_boolean_attributes')
      html_args[:compress_js_templates]       = opts['compress_js_templates']      if opts.has_key?('compress_js_templates')
      html_args[:preserve_patterns]          += [/<\?php.*?\?>/im]                 if opts['preserve_php'] == true
      html_args[:preserve_patterns]          += opts[:preserve_patterns].map { |pattern| Regexp.new(pattern)} if opts.has_key?(:preserve_patterns)
    end

    html_args[:css_compressor]              = CSSminify2.new()

    if ( !js_args[:uglifier_args].nil? )
      html_args[:javascript_compressor]       = Uglifier.new(js_args[:uglifier_args])
    else
      html_args[:javascript_compressor]       = Uglifier.new()
    end

    compressor = HtmlCompressor::Compressor.new(html_args)
    output_file(path, compressor.compress(content))
  else
    output_file(path, content)
  end
end

#output_js(path, content) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jekyll-minifier.rb', line 85

def output_js(path, content)
  if ( ENV['JEKYLL_ENV'] == "production" )
    js_args  = {}
    opts     = @site.config['jekyll-minifier']
    compress = true
    if ( !opts.nil? )
      compress                = opts['compress_javascript']                           if opts.has_key?('compress_javascript')
      js_args[:uglifier_args] = Hash[opts['uglifier_args'].map{|(k,v)| [k.to_sym,v]}] if opts.has_key?('uglifier_args')
    end

    if ( compress )
      if ( !js_args[:uglifier_args].nil? )
        compressor = Uglifier.new(js_args[:uglifier_args])
      else
        compressor = Uglifier.new()
      end

      output_file(path, compressor.compile(content))
    else
      output_file(path, content)
    end
  else
    output_file(path, content)
  end
end

#output_json(path, content) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jekyll-minifier.rb', line 111

def output_json(path, content)
  if ( ENV['JEKYLL_ENV'] == "production" )
    opts       = @site.config['jekyll-minifier']
    compress = true
    if ( !opts.nil? )
      compress    = opts['compress_json']               if opts.has_key?('compress_json')
    end

    if ( compress )
      output_file(path, JSON.minify(content))
    else
      output_file(path, content)
    end
  else
    output_file(path, content)
  end
end