Class: Middleman::CoreExtensions::FrontMatter

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-core/core_extensions/front_matter.rb

Constant Summary

Constants included from Contracts

Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

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, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ FrontMatter



23
24
25
26
27
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 23

def initialize(app, options_hash={}, &block)
  super

  @cache = {}
end

Instance Method Details

#before_configurationObject



29
30
31
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 29

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

#clear_data(updated_files, removed_files) ⇒ Object



87
88
89
90
91
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 87

def clear_data(updated_files, removed_files)
  (updated_files + removed_files).each do |file|
    @cache.delete(file[:full_path].to_s)
  end
end

#data(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 71

def data(path)
  file = app.files.find(:source, path)

  return [{}, nil] unless file

  file_path = file[:full_path].to_s

  @cache[file_path] ||= begin
    ::Middleman::Util::Data.parse(
      file,
      app.config[:frontmatter_delims]
    )
  end
end

#manipulate_resource_list(resources) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 35

def manipulate_resource_list(resources)
  resources.each do |resource|
    next if resource.binary?
    next if resource.file_descriptor.nil?
    next if resource.file_descriptor[:types].include?(:no_frontmatter)

    fmdata = data(resource.file_descriptor[:full_path].to_s).first.dup

    # Copy over special options
    # TODO: Should we make people put these under "options" instead of having
    # special known keys?
    opts = fmdata.extract!(:layout, :layout_engine, :renderer_options, :directory_index, :content_type)
    opts[:renderer_options].symbolize_keys! if opts.key?(:renderer_options)

    ignored = fmdata.delete(:ignored)

    # TODO: Enhance data? NOOOO
    # TODO: stringify-keys? immutable/freeze?

    resource. options: opts, page: fmdata

    resource.ignore! if ignored == true && !resource.is_a?(::Middleman::Sitemap::ProxyResource)

    # TODO: Save new template here somewhere?
  end
end

#template_data_for_file(path) ⇒ Object



66
67
68
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 66

def template_data_for_file(path)
  data(path).last
end