Class: Jekyll::Assets::Proxy

Inherits:
Extensible show all
Defined in:
lib/jekyll/assets/proxy.rb

Defined Under Namespace

Classes: Deleted

Instance Attribute Summary collapse

Attributes inherited from Extensible

#args, #asset, #ctx, #env, #jekyll

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Extensible

for?, for_args?, for_type?, inherited, internal!, internal?, requirements

Constructor Details

#initialize(file, **kwd) ⇒ Proxy



114
115
116
117
# File 'lib/jekyll/assets/proxy.rb', line 114

def initialize(file, **kwd)
  super(**kwd)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/jekyll/assets/proxy.rb', line 13

def file
  @file
end

Class Method Details

.args_key(key = nil) ⇒ Symbol

Note:

we will not run your proxy if the argkey doen’t match.

– Allows you to tell the proxier which args are yours. –

Parameters:

  • key (Symbol) (defaults to: nil)

    the key.

Returns:

  • (Symbol)

    the argument key.



100
101
102
# File 'lib/jekyll/assets/proxy.rb', line 100

def self.args_key(key = nil)
  key.nil? ? @key : @key = key
end

.copy(asset, ctx:, args:) ⇒ Pathutil

Note:

this is done so we do not directly alter.

– Copy the asset to the proxied directory. –

Parameters:

  • asset (Sprockets::Asset)

    the current asset.

  • env (Env)

    the environment.

  • args (Hash)

    the args.

Returns:

  • (Pathutil)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jekyll/assets/proxy.rb', line 73

def self.copy(asset, ctx:, args:)
  env = ctx.registers[:site].sprockets

  path = env.in_cache_dir("proxied")
  extname = File.extname(args[:argv1])
  out = Pathutil.new(path).join(digest(args))
    .sub_ext(extname)

  unless out.file?
    out.dirname.mkdir_p
    Pathutil.new(asset.filename)
      .cp(out)
  end

  out
end

.digest(args) ⇒ Object



90
91
92
# File 'lib/jekyll/assets/proxy.rb', line 90

def self.digest(args)
  Digest::SHA256.hexdigest(args.to_h.inspect)[0, 6]
end

.keysArray<Symbol>

– Return a list of proxy keys. This allows you to select their values from args. –

Returns:

  • (Array<Symbol>)


109
110
111
# File 'lib/jekyll/assets/proxy.rb', line 109

def self.keys
  inherited.map(&:arg_keys).flatten
end

.proxies_for(asset:, args:) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/jekyll/assets/proxy.rb', line 56

def self.proxies_for(asset:, args:)
  Proxy.inherited.select do |o|
    o.for?({
      type: asset.content_type,
      args: args,
    })
  end
end

.proxy(asset, args:, ctx:) ⇒ Sprockets::Asset

– Run all your proxies on assets. –

Parameters:

  • args (Hash)

    the args.

  • asset (Sprockets::Asset)

    the asset.

  • type (String)

    the assets content_type

  • env (Env)

    the environment.

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/jekyll/assets/proxy.rb', line 30

def self.proxy(asset, args:, ctx:)
  env = ctx.registers[:site].sprockets
  return asset if (proxies = proxies_for(asset: asset, args: args)).empty?
  key = digest(args)

  out = env.cache.fetch(key) do
    file = copy(asset, args: args, ctx: ctx)
    proxies.each do |o|
      obj = o.new(file, {
        args: args,
        asset: asset,
        ctx: ctx,
      })

      o = obj.process
      file = o if o.is_a?(Pathutil) && file != o
      raise Deleted, o unless file.exist?
    end

    file
  end

  env.find_asset!(out)
end