Class: Middleman::Sitemap::Extensions::OnDisk

Inherits:
Extension
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activated_extension, #add_exposed_to_context, #after_build, #after_configuration, #after_extension_activated, after_extension_activated, #before_build, clear_after_extension_callbacks, config, define_setting, expose_to_application, expose_to_config, expose_to_template, global_config, helpers, #manipulate_resource_list, option, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block) ⇒ OnDisk

Returns a new instance of OnDisk.


12
13
14
15
16
17
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 12

def initialize(app, options_hash = ::Middleman::EMPTY_HASH, &block)
  super

  @file_paths_on_disk = Set.new
  @waiting_for_ready = true
end

Instance Method Details

#before_configurationObject


27
28
29
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 27

def before_configuration
  app.files.on_change(:source, &method(:update_files))
end

#files_for_sitemapObject


57
58
59
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 57

def files_for_sitemap
  @app.files.by_type(:source).files.reject(&method(:ignored?))
end

#ignored?(file) ⇒ Boolean

Returns:

  • (Boolean)

32
33
34
35
36
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 32

def ignored?(file)
  @app.config[:ignored_sitemap_matchers].any? do |_, callback|
    callback.call(file, @app)
  end
end

#manipulate_resource_list_container!(resource_list) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 63

def manipulate_resource_list_container!(resource_list)
  files_for_sitemap.each do |file|
    resource_list.add!(
      ::Middleman::Sitemap::Resource.new(
        @app.sitemap,
        @app.sitemap.file_to_path(file),
        file
      )
    )
  end
end

#readyObject


19
20
21
22
23
24
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 19

def ready
  @waiting_for_ready = false

  # Make sure the sitemap is ready for the first request
  app.sitemap.ensure_resource_list_updated!
end

#update_files(updated_files, removed_files) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb', line 42

def update_files(updated_files, removed_files)
  return if (updated_files + removed_files).all?(&method(:ignored?))

  # Rebuild the sitemap any time a file is touched
  # in case one of the other manipulators
  # (like asset_hash) cares about the contents of this file,
  # whether or not it belongs in the sitemap (like a partial)
  @app.sitemap.rebuild_resource_list!(:touched_file)

  # Force sitemap rebuild so the next request is ready to go.
  # Skip this during build because the builder will control sitemap refresh.
  @app.sitemap.ensure_resource_list_updated! unless @waiting_for_ready || @app.build?
end