Class: Middleman::CoreExtensions::FrontMatter

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

Defined Under Namespace

Modules: ResourceInstanceMethods

Constant Summary collapse

YAML_ERRORS =
[ StandardError ]

Instance Attribute Summary

Attributes inherited from Extension

#app, #options

Instance Method Summary collapse

Methods inherited from Extension

activate, activated_extension, after_extension_activated, clear_after_extension_callbacks, config, extension_name, helpers, option, register

Constructor Details

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

Returns a new instance of FrontMatter.



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

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

  @cache = {}
end

Instance Method Details

#after_configurationObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 34

def after_configuration
  app.extensions[:frontmatter] = self
  app.ignore %r{\.frontmatter$}

  ::Middleman::Sitemap::Resource.send :include, ResourceInstanceMethods

  app.sitemap. do |path|
    fmdata = data(path).first

    data = {}
    [:layout, :layout_engine].each do |opt|
      data[opt] = fmdata[opt] unless fmdata[opt].nil?
    end

    { :options => data, :page => ::Middleman::Util.recursively_enhance(fmdata).freeze }
  end
end

#before_configurationObject



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

def before_configuration
  ext = self
  app.files.changed { |file| ext.clear_data(file) }
  app.files.deleted { |file| ext.clear_data(file) }
end

#clear_data(file) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 107

def clear_data(file)
  # Copied from Sitemap::Store#file_to_path, but without
  # removing the file extension
  file = File.join(app.root, file)
  prefix = app.source_dir.sub(/\/$/, "") + "/"
  return unless file.include?(prefix)
  path = file.sub(prefix, "").sub(/\.frontmatter$/, "")

  @cache.delete(path)
end

#data(path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/middleman-core/core_extensions/front_matter.rb', line 93

def data(path)
  p = normalize_path(path)
  @cache[p] ||= begin
    data, content = frontmatter_and_content(p)

    if app.files.exists?("#{path}.frontmatter")
      external_data, _ = frontmatter_and_content("#{p}.frontmatter")
      data = external_data.deep_merge(data)
    end

    [data, content]
  end
end