class Middleman::CoreExtensions::FrontMatter::FrontMatter

def data(path)

def data(path)
  if @local_data.has_key?(path.to_s)
    @local_data[path.to_s]
  else
    nil
  end
end

def has_data?(path)

def has_data?(path)
  @local_data.has_key?(path.to_s)
end

def initialize(app)

def initialize(app)
  @app = app
  @source = File.expand_path(@app.source, @app.root)
  @local_data = {}
end

def parse_front_matter(content)

def parse_front_matter(content)
  yaml_regex = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
  if content =~ yaml_regex
    begin
      data = YAML.load($1)
    rescue => e
      puts "YAML Exception: #{e.message}"
      return false
    end
    content = content.split(yaml_regex).last
  else
    return false
  end
  [data, content]
end

def remove_file(file)

def remove_file(file)
  file = File.expand_path(file, @app.root)
  file = file.sub(@app.source_dir, "")
  # @app.logger.debug :frontmatter_remove, Time.now, file if @app.logging?
  
  if @local_data.has_key?(file)
    @local_data.delete(file) 
  end
end

def touch_file(file)

def touch_file(file)
  extension = File.extname(file).sub(/\./, "")
  return unless ::Tilt.mappings.has_key?(extension)
  
  file = File.expand_path(file, @app.root)
  content = File.read(file)
  
  # @app.logger.debug :frontmatter_update, Time.now, file if @app.logging?
  result = parse_front_matter(content)
    
  if result
    file = file.sub(@app.source_dir, "")
    @local_data[file] = result
    path = File.join(@app.source_dir, file)
    @app.cache.set([:raw_template, path], result[1])
    @app.frontmatter_did_change(path)
  end
end