Class: Serif::FileDigest

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/serif/site.rb

Constant Summary collapse

DIGEST_CACHE =
{}
Syntax =

file_digest “file.css” [prefix:.]

/^\s*(\S+)\s*(?:(prefix\s*:\s*\S+)\s*)?$/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ FileDigest

Returns a new instance of FileDigest.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/serif/site.rb', line 31

def initialize(tag_name, markup, tokens)
  super

  if markup =~ Syntax
    @path = $1

    if $2
      @prefix = $2.gsub(/\s*prefix\s*:\s*/, "")
    else
      @prefix = ""
    end
  else
    raise SyntaxError.new("Syntax error for file_digest")
  end
end

Instance Method Details

#render(context) ⇒ Object

Takes the given path and returns the MD5 hex digest of the file’s contents.

The path argument is first stripped, and any leading “/” has no effect.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/serif/site.rb', line 52

def render(context)
  return "" unless ENV["ENV"] == "production"

  full_path = File.join(context["site"]["directory"], @path.strip)

  return @prefix + DIGEST_CACHE[full_path] if DIGEST_CACHE[full_path]

  digest = Digest::MD5.hexdigest(File.read(full_path))
  DIGEST_CACHE[full_path] = digest

  @prefix + digest
end