Skip to content

Commit

Permalink
Merge pull request #23 from chris-huxtable/htmlcompressor-options
Browse files Browse the repository at this point in the history
Htmlcompressor options
  • Loading branch information
digitalsparky authored May 17, 2017
2 parents 1857c31 + 1bcfc98 commit 406408f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,27 @@ Optionally, you can also add exclusions using:
<pre><code>jekyll-minifier:
exclude: 'atom.xml' # Exclude files from processing - file name, glob pattern or array of file names and glob patterns
</code></pre>
and toggle htmlcompressor features using:
<pre><code>jekyll-minifier:
preserve_php: true # Default: false
remove_spaces_inside_tags: true # Default: true
remove_multi_spaces: true # Default: true
remove_comments: true # Default: true
remove_intertag_spaces: true # Default: false
remove_quotes: false # Default: false
compress_css: true # Default: true
compress_javascript: true # Default: true
simple_doctype: false # Default: false
remove_script_attributes: false # Default: false
remove_style_attributes: false # Default: false
remove_link_attributes: false # Default: false
remove_form_attributes: false # Default: false
remove_input_attributes: false # Default: false
remove_javascript_protocol: false # Default: false
remove_http_protocol: false # Default: false
remove_https_protocol: false # Default: false
preserve_line_breaks: false # Default: false
simple_boolean_attributes: false # Default: false
compress_js_templates: false # Default: false
</code></pre>

37 changes: 36 additions & 1 deletion lib/jekyll-minifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,42 @@ def output_file(dest, content)
end

def output_html(path, content)
compressor = HtmlCompressor::Compressor.new({ :remove_comments => true, :compress_css => true, :compress_javascript => true, :css_compressor => CSSminify2.new, :javascript_compressor => Uglifier.new })
args = { remove_comments: true, compress_css: true, compress_javascript: true, preserve_patterns: [] }
args[:css_compressor] = CSSminify2.new
args[:javascript_compressor] = Uglifier.new

opts = @site.config['jekyll-minifier']

if ( !opts.nil? )
# Convert keys to symboles
opts.keys.each { |key| opts[(key.to_sym rescue key) || key] = opts.delete(key) }

args[:remove_spaces_inside_tags] = opts[:remove_spaces_inside_tags] if opts.has_key?(:remove_spaces_inside_tags)
args[:remove_multi_spaces] = opts[:remove_multi_spaces] if opts.has_key?(:remove_multi_spaces)
args[:remove_comments] = opts[:remove_comments] if opts.has_key?(:remove_comments)
args[:remove_intertag_spaces] = opts[:remove_intertag_spaces] if opts.has_key?(:remove_intertag_spaces)
args[:remove_quotes] = opts[:remove_quotes] if opts.has_key?(:remove_quotes)
args[:compress_css] = opts[:compress_css] if opts.has_key?(:compress_css)
args[:compress_javascript] = opts[:compress_javascript] if opts.has_key?(:compress_javascript)
args[:simple_doctype] = opts[:simple_doctype] if opts.has_key?(:simple_doctype)
args[:remove_script_attributes] = opts[:remove_script_attributes] if opts.has_key?(:remove_script_attributes)
args[:remove_style_attributes] = opts[:remove_style_attributes] if opts.has_key?(:remove_style_attributes)
args[:remove_link_attributes] = opts[:remove_link_attributes] if opts.has_key?(:remove_link_attributes)
args[:remove_form_attributes] = opts[:remove_form_attributes] if opts.has_key?(:remove_form_attributes)
args[:remove_input_attributes] = opts[:remove_input_attributes] if opts.has_key?(:remove_input_attributes)
args[:remove_javascript_protocol] = opts[:remove_javascript_protocol] if opts.has_key?(:remove_javascript_protocol)
args[:remove_http_protocol] = opts[:remove_http_protocol] if opts.has_key?(:remove_http_protocol)
args[:remove_https_protocol] = opts[:remove_https_protocol] if opts.has_key?(:remove_https_protocol)
args[:preserve_line_breaks] = opts[:preserve_line_breaks] if opts.has_key?(:preserve_line_breaks)
args[:simple_boolean_attributes] = opts[:simple_boolean_attributes] if opts.has_key?(:simple_boolean_attributes)
args[:compress_js_templates] = opts[:compress_js_templates] if opts.has_key?(:compress_js_templates)
args[:preserve_patterns] += [/<\?php.*?\?>/im] if opts[:preserve_php] == true

# Potential to add patterns from YAML
#args[:preserve_patterns] += opts[:preserve_patterns].map { |pattern| Regexp.new(pattern)} if opts.has_key?(:preserve_patterns)
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll-minifier/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jekyll
module Minifier
VERSION = "0.1.1"
VERSION = "0.1.2"
end
end

0 comments on commit 406408f

Please sign in to comment.