class Middleman::CoreExtensions::FrontMatter::FrontmatterManager

def clear_data(file)

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

def data(path)

def data(path)
  p = normalize_path(path)
  @cache[p] ||= frontmatter_and_content(p)
end

def frontmatter_and_content(path)

Returns:
  • (Array) -

Parameters:
  • path (String) --
def frontmatter_and_content(path)
  full_path = if Pathname(path).relative?
    File.join(@app.source_dir, path)
  else
    path
  end
  
  data = {}
  content = nil
  if !::Middleman::Util.binary?(full_path)
    content = File.read(full_path)
    
    begin
      if content =~ /\A.*coding:/
        lines = content.split(/\n/)
        lines.shift
        content = lines.join("\n")
      end
      if result = parse_yaml_front_matter(content)
        data, content = result
      elsif result = parse_json_front_matter(content)
        data, content = result
      end
    rescue => e
      # Probably a binary file, move on
    end
  end
  [::Middleman::Util.recursively_enhance(data).freeze, content]
end

def initialize(app)

def initialize(app)
  @app = app
  @cache = {}
end

def manipulate_resource_list(resources)

Returns:
  • (void) -
def manipulate_resource_list(resources)
  resources.each do |r|
    if !r.proxy? && !r.data.nil? && r.data["ignored"] == true
      r.frontmatter_ignored = true
    end
  end
  resources
end

def normalize_path(path)

def normalize_path(path)
  path.sub(%r{^#{@app.source_dir}\/}, "")
end

def parse_json_front_matter(content)

def parse_json_front_matter(content)
  json_regex = /\A(;;;\s*\n.*?\n?)^(;;;\s*$\n?)/m
  if content =~ json_regex
    content = content.sub(json_regex, "")
    begin
      json = ($1+$2).sub(";;;", "{").sub(";;;", "}")
      data = ActiveSupport::JSON.decode(json)
    rescue => e
      logger.error "JSON Exception: #{e.message}"
      return false
    end
  else
    return false
  end
  [data, content]
rescue
  [{}, content]
end

def parse_yaml_front_matter(content)

Returns:
  • (Array) -

Parameters:
  • content (String) --
def parse_yaml_front_matter(content)
  yaml_regex = /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
  if content =~ yaml_regex
    content = content.sub(yaml_regex, "")
    begin
      data = YAML.load($1)
    rescue *YAML_ERRORS => e
      logger.error "YAML Exception: #{e.message}"
      return false
    end
  else
    return false
  end
  [data, content]
rescue
  [{}, content]
end