Class: Bridgetown::ImagePipeline::Builder

Inherits:
Builder
  • Object
show all
Defined in:
lib/bridgetown/image_pipeline/builder.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, cache_root: nil) ⇒ Builder

Returns a new instance of Builder.



21
22
23
24
25
26
27
28
29
30
# File 'lib/bridgetown/image_pipeline/builder.rb', line 21

def initialize(*args, cache_root: nil)
  site = args.last
  super(*args)
  @site        = site
  @config      = self.class.pending_config || Config.from
  cache_root ||= File.join(site.root_dir, ".bridgetown-cache", "image_pipeline")
  @manifest    = Manifest.new(cache_dir: cache_root)
  @output_root = site.in_dest_dir
  @processor   = Processor.new(config: @config, output_root: @output_root)
end

Class Attribute Details

.pending_configObject

Returns the value of attribute pending_config.



18
19
20
# File 'lib/bridgetown/image_pipeline/builder.rb', line 18

def pending_config
  @pending_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/bridgetown/image_pipeline/builder.rb', line 13

def config
  @config
end

#manifestObject (readonly)

Returns the value of attribute manifest.



13
14
15
# File 'lib/bridgetown/image_pipeline/builder.rb', line 13

def manifest
  @manifest
end

Instance Method Details

#attach_to_site!Object



61
62
63
64
65
66
67
# File 'lib/bridgetown/image_pipeline/builder.rb', line 61

def attach_to_site!
  site = @site
  manifest = @manifest
  config   = @config
  site.define_singleton_method(:image_pipeline_manifest) { manifest }
  site.define_singleton_method(:image_pipeline_config)   { config }
end

#buildObject



32
33
34
35
36
# File 'lib/bridgetown/image_pipeline/builder.rb', line 32

def build
  hook(:site, :pre_render) { run }
  attach_to_site!
  register_auto_rewrite_hooks! if @config.auto_rewrite
end

#html_output?(obj) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/bridgetown/image_pipeline/builder.rb', line 51

def html_output?(obj)
  return false unless obj.respond_to?(:output_ext)

  obj.output_ext.to_s.downcase == ".html"
end

#register_auto_rewrite_hooks!Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bridgetown/image_pipeline/builder.rb', line 38

def register_auto_rewrite_hooks!
  inspector = Inspector.new(manifest: @manifest, config: @config)
  site_to_match = @site
  rewriter = lambda do |obj|
    return unless obj.site.equal?(site_to_match)
    return unless html_output?(obj)

    obj.output = inspector.rewrite(obj.output.to_s)
  end
  Bridgetown::Hooks.register_one(:resources,       :post_render, reloadable: false, &rewriter)
  Bridgetown::Hooks.register_one(:generated_pages, :post_render, reloadable: false, &rewriter)
end

#runObject



57
58
59
# File 'lib/bridgetown/image_pipeline/builder.rb', line 57

def run
  sources.each { |src| process_one(src) }
end