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

Methods included from Contracts

#Contract

Constructor Details

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

Returns a new instance of FrontMatter.


61
62
63
64
65
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 61

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

  @cache = {}
end

Instance Method Details

#before_configurationObject


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

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

#clear_data(updated_files, removed_files) ⇒ Object


137
138
139
140
141
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 137

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


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 119

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

  return [{}, nil] unless file

  file_path = file[:full_path].to_s

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

#manipulate_resource_list_container!(resource_list) ⇒ Object


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 72

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


114
115
116
# File 'middleman-core/lib/middleman-core/core_extensions/front_matter.rb', line 114

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