lib/jekyll/tidy.rb in jekyll-tidy-0.0.1 vs lib/jekyll/tidy.rb in jekyll-tidy-0.2.0

- old
+ new

@@ -1,45 +1,59 @@ +require "jekyll/tidy/version" + require "jekyll" require "htmlbeautifier" require "htmlcompressor" -JEKYLL_CONFIG = Jekyll.configuration({}) - module Jekyll - module Tidy - def self.exclude?(path) - res = false - exclude = JEKYLL_CONFIG['jekyll_tidy'] && JEKYLL_CONFIG['jekyll_tidy']['exclude'] + module Tidy + def self.init(site) + @JEKYLL_CONFIG = site.config + end - if exclude - res = exclude.to_a.include? path - end + def self.jekyll_config + @JEKYLL_CONFIG || Jekyll.configuration({}) + end - return res - end + def self.exclude?(path, override = {}) + exclude_paths = jekyll_config.merge(override).dig("jekyll_tidy", "exclude").to_a + exclude_paths.any? { |exclude| File.fnmatch(exclude, path) } + end - def self.compress_html? - JEKYLL_CONFIG['jekyll_tidy'] && JEKYLL_CONFIG['jekyll_tidy']['compress_html'] - end + def self.compress_output? + jekyll_config.dig("jekyll_tidy", "compress_html") + end - def self.output_clean(output) - if compress_html? - compressor = HtmlCompressor::Compressor.new - return compressor.compress output - else - return HtmlBeautifier.beautify output - end - end + def self.output_clean(output, compress = false) + if compress + return HtmlCompressor::Compressor.new.compress output + else + return HtmlBeautifier.beautify output + end end -end -Jekyll::Hooks.register :posts, :post_render do |post| - unless Jekyll::Tidy::exclude?(post.path) - post.output = Jekyll::Tidy::output_clean(post.output) + def self.ignore_env? + Jekyll.env == jekyll_config.dig("jekyll_tidy", "ignore_env") end + end end +# Jekyll Hooks +# ------------------------------------- + +Jekyll::Hooks.register :site, :after_reset do |jekyll| + Jekyll::Tidy.init(jekyll) +end + +Jekyll::Hooks.register :documents, :post_render do |doc| + next if Jekyll::Tidy.ignore_env? + unless Jekyll::Tidy::exclude?(doc.relative_path) + doc.output = Jekyll::Tidy::output_clean(doc.output, Jekyll::Tidy.compress_output?) + end +end + Jekyll::Hooks.register :pages, :post_render do |page| - unless Jekyll::Tidy::exclude?(page.path) - page.output = Jekyll::Tidy::output_clean(page.output) - end + next if Jekyll::Tidy.ignore_env? + unless Jekyll::Tidy::exclude?(page.relative_path) + page.output = Jekyll::Tidy::output_clean(page.output, Jekyll::Tidy.compress_output?) + end end