2011-06-06 14:39:57 -04:00
|
|
|
require "yaml"
|
|
|
|
require "tilt"
|
|
|
|
|
2011-07-06 13:15:31 -04:00
|
|
|
module Middleman::CoreExtensions::FrontMatter
|
2011-06-06 14:39:57 -04:00
|
|
|
class << self
|
|
|
|
def registered(app)
|
|
|
|
app.extend ClassMethods
|
2011-06-08 00:44:10 -04:00
|
|
|
|
|
|
|
::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md'
|
2011-06-28 18:51:25 -04:00
|
|
|
::Tilt::register RedClothTemplate, 'textile'
|
|
|
|
::Tilt::register ERBTemplate, 'erb', 'rhtml'
|
|
|
|
::Tilt::register ErubisTemplate, 'erb', 'rhtml', 'erubis'
|
2011-07-07 01:41:12 -04:00
|
|
|
#
|
|
|
|
# app.before do
|
|
|
|
# full_file_path = "#{extensionless_path}.#{template_engine}"
|
|
|
|
# system_path = File.join(settings.views, full_file_path)
|
|
|
|
# data, content = self.class.parse_front_matter(File.read(system_path))
|
|
|
|
#
|
|
|
|
# %w(layout layout_engine).each do |opt|
|
|
|
|
# if data.has_key?(opt)
|
|
|
|
# options[opt.to_sym] = data.delete(opt)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # Forward remaining data to helpers
|
|
|
|
# self.class.data_content("page", data)
|
|
|
|
# end
|
2011-06-06 14:39:57 -04:00
|
|
|
end
|
|
|
|
alias :included :registered
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
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}"
|
|
|
|
end
|
|
|
|
|
|
|
|
content = content.split(yaml_regex).last
|
|
|
|
end
|
|
|
|
|
|
|
|
data ||= {}
|
|
|
|
[data, content]
|
|
|
|
end
|
|
|
|
end
|
2011-06-08 00:44:10 -04:00
|
|
|
|
|
|
|
module YamlAware
|
|
|
|
def prepare
|
|
|
|
options, @data = Middleman::Server.parse_front_matter(@data)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2011-06-06 14:39:57 -04:00
|
|
|
|
2011-06-28 18:51:25 -04:00
|
|
|
# MARKDOWN
|
2011-06-08 00:44:10 -04:00
|
|
|
class RDiscountTemplate < ::Tilt::RDiscountTemplate
|
2011-07-06 13:15:31 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
2011-06-08 00:44:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# TEXTILE
|
|
|
|
class RedClothTemplate < ::Tilt::RedClothTemplate
|
2011-07-06 13:15:31 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
2011-06-08 00:44:10 -04:00
|
|
|
end
|
2011-06-14 16:54:59 -04:00
|
|
|
|
|
|
|
# ERb
|
|
|
|
class ERBTemplate < ::Tilt::ERBTemplate
|
2011-07-06 13:15:31 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
2011-06-14 16:54:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class ErubisTemplate < ::Tilt::ErubisTemplate
|
2011-07-06 13:15:31 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
2011-06-14 16:54:59 -04:00
|
|
|
end
|
2011-06-08 00:44:10 -04:00
|
|
|
end
|