Class: Middleman::CoreExtensions::FrontMatter

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

Constant Summary

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, 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, #manipulate_resource_list, option, #ready, resources

Methods included from Contracts

#Contract

Constructor Details

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

Returns a new instance of FrontMatter


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

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

  @cache = {}
end

Instance Method Details

#before_configurationObject


27
28
29
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 27

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

#clear_data(updated_files, removed_files) ⇒ Object


99
100
101
102
103
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 99

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


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 79

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

  return [{}, nil] unless file

  file_path = file[:full_path].to_s

  @cache[file_path] ||= begin
    if ::Middleman::Util.contains_frontmatter?(file_path, app.config[:frontmatter_delims])
      ::Middleman::Util::Data.parse(
        file,
        app.config[:frontmatter_delims]
      )
    else
      [{}, nil]
    end
  end
end

#manipulate_resource_list_container!(resource_list) ⇒ Object


32
33
34
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
61
62
63
64
65
66
67
68
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 32

def manipulate_resource_list_container!(resource_list)
  resource_list.by_binary(false).each do |resource|
    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. opts

    if fmdata.key?(:id)
      resource_list.update!(resource, :page_id) do
        resource. fmdata
      end
    else
      resource. fmdata
    end

    next unless ignored == true && !resource.is_a?(::Middleman::Sitemap::ProxyResource)

    resource_list.update!(resource, :ignored) do
      resource.ignore!
    end

    # TODO: Save new template here somewhere?
  end
end

#template_data_for_file(path) ⇒ Object


74
75
76
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 74

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