class Jekyll::Converters::PostCss

Public Class Methods

new(config = {}) click to toggle source
Calls superclass method
# File lib/jekyll/converters/postcss.rb, line 12
def initialize(config = {})
  super

  @cache_enabled = config.fetch("postcss", {}).fetch("cache", true)
  @socket = config.fetch("socket") { ::PostCss::Socket.new }
  @raw_cache = nil
  @import_raw_cache = {}
  @converted_cache = nil
end

Public Instance Methods

convert(content) click to toggle source
# File lib/jekyll/converters/postcss.rb, line 30
def convert(content)
  raise PostCssNotFoundError unless Dir.exist?("./node_modules/postcss")

  @raw_digest = Digest::MD5.hexdigest content
  @raw_import_digests = import_digests(content)

  if cache_disabled? || cache_miss?
    @raw_cache = @raw_digest.dup
    @import_raw_cache = @raw_import_digests.dup

    @socket.write content

    @converted_cache = @socket.read
  end

  reset

  @converted_cache
end
matches(ext) click to toggle source
# File lib/jekyll/converters/postcss.rb, line 22
def matches(ext)
  [".css", ".scss", ".sass"].include?(ext.downcase)
end
output_ext(ext) click to toggle source
# File lib/jekyll/converters/postcss.rb, line 26
def output_ext(ext)
  ext
end

Private Instance Methods

cache_disabled?() click to toggle source
# File lib/jekyll/converters/postcss.rb, line 62
def cache_disabled?
  @cache_enabled == false
end
cache_miss?() click to toggle source
# File lib/jekyll/converters/postcss.rb, line 66
def cache_miss?
  @raw_import_digests
    .map { |import, hash| @import_raw_cache[import] != hash }
    .unshift(@raw_cache != @raw_digest)
    .any?
end
import_digests(content) click to toggle source
# File lib/jekyll/converters/postcss.rb, line 52
def import_digests(content)
  content
    .scan(%r!^@import "(?<file>.*)";$!)
    .flatten
    .each_with_object({}) do |import, acc|
      file = "#{import}.css"
      acc[import] = Digest::MD5.hexdigest IO.read(file) if File.file?(file)
    end
end
reset() click to toggle source
# File lib/jekyll/converters/postcss.rb, line 73
def reset
  @raw_digest = nil
  @raw_import_digest = nil
end