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
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md'
|
|
|
|
::Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md'
|
|
|
|
::Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md'
|
|
|
|
::Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md'
|
|
|
|
app.set :markdown_engine_prefix, ::Middleman::CoreExtensions::FrontMatter
|
2011-07-08 16:23:06 -04:00
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
::Tilt::register RedClothTemplate, 'textile'
|
|
|
|
::Tilt.prefer(RedClothTemplate)
|
2011-07-08 16:23:06 -04:00
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
::Tilt::register ERBTemplate, 'erb', 'rhtml'
|
|
|
|
::Tilt.prefer(ERBTemplate)
|
2011-07-08 16:23:06 -04:00
|
|
|
|
2011-09-03 13:23:04 -04:00
|
|
|
::Tilt::register LiquidTemplate, 'liquid'
|
|
|
|
::Tilt.prefer(LiquidTemplate)
|
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
::Tilt::register SlimTemplate, 'slim'
|
|
|
|
::Tilt.prefer(SlimTemplate)
|
2011-07-08 16:23:06 -04:00
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
::Tilt::register HamlTemplate, 'haml'
|
|
|
|
::Tilt.prefer(HamlTemplate)
|
2011-07-07 01:42:17 -04:00
|
|
|
|
2011-08-04 22:30:58 -04:00
|
|
|
app.after_configuration do
|
2011-07-14 15:32:39 -04:00
|
|
|
app.before_processing do
|
2011-07-22 14:43:00 -04:00
|
|
|
request_path = request.path_info.gsub("%20", " ")
|
|
|
|
result = resolve_template(request_path, :raise_exceptions => false)
|
2011-07-14 15:32:39 -04:00
|
|
|
|
2011-07-13 20:48:31 -04:00
|
|
|
if result && Tilt.mappings.has_key?(result[1].to_s)
|
|
|
|
extensionless_path, template_engine = result
|
|
|
|
full_file_path = "#{extensionless_path}.#{template_engine}"
|
|
|
|
system_path = File.join(settings.views, full_file_path)
|
|
|
|
data, content = app.parse_front_matter(File.read(system_path))
|
2011-07-14 15:32:39 -04:00
|
|
|
|
2011-07-13 20:48:31 -04:00
|
|
|
request['custom_options'] = {}
|
|
|
|
%w(layout layout_engine).each do |opt|
|
|
|
|
if data.has_key?(opt)
|
|
|
|
request['custom_options'][opt.to_sym] = data.delete(opt)
|
|
|
|
end
|
2011-07-07 01:42:17 -04:00
|
|
|
end
|
2011-07-14 15:32:39 -04:00
|
|
|
|
2011-07-13 20:48:31 -04:00
|
|
|
# Forward remaining data to helpers
|
|
|
|
app.data_content("page", data)
|
|
|
|
end
|
2011-07-14 15:32:39 -04:00
|
|
|
|
|
|
|
true
|
2011-07-07 01:42:17 -04:00
|
|
|
end
|
|
|
|
end
|
2011-07-13 20:48:31 -04:00
|
|
|
end
|
|
|
|
alias :included :registered
|
|
|
|
|
|
|
|
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}"
|
2011-07-13 03:38:04 -04:00
|
|
|
end
|
|
|
|
|
2011-07-13 20:48:31 -04:00
|
|
|
content = content.split(yaml_regex).last
|
2011-07-13 03:38:04 -04:00
|
|
|
end
|
2011-07-13 20:48:31 -04:00
|
|
|
|
|
|
|
data ||= {}
|
|
|
|
[data, content]
|
2011-06-06 14:39:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def parse_front_matter(content)
|
2011-07-13 03:38:04 -04:00
|
|
|
Middleman::CoreExtensions::FrontMatter.parse_front_matter(content)
|
2011-06-06 14:39:57 -04:00
|
|
|
end
|
|
|
|
end
|
2011-06-08 00:44:10 -04:00
|
|
|
|
|
|
|
module YamlAware
|
|
|
|
def prepare
|
2011-09-06 14:12:20 -04:00
|
|
|
@frontmatter, @data = Middleman::CoreExtensions::FrontMatter.parse_front_matter(@data)
|
2011-06-08 00:44:10 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2011-06-06 14:39:57 -04:00
|
|
|
|
2011-08-31 16:15:59 -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
|
2011-08-31 16:15:59 -04:00
|
|
|
class RedcarpetTemplate < ::Tilt::RedcarpetTemplate
|
2011-07-08 16:23:06 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
|
|
|
end
|
2011-08-31 16:15:59 -04:00
|
|
|
class MarukuTemplate < ::Tilt::MarukuTemplate
|
2011-08-05 13:32:53 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
|
|
|
end
|
2011-08-31 16:15:59 -04:00
|
|
|
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-08-31 16:15:59 -04:00
|
|
|
class KramdownTemplate < ::Tilt::KramdownTemplate
|
2011-08-12 01:42:16 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
|
|
|
end
|
2011-07-07 01:56:20 -04:00
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
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
|
2011-07-07 01:56:20 -04:00
|
|
|
|
2011-09-03 13:23:04 -04:00
|
|
|
class LiquidTemplate < ::Tilt::LiquidTemplate
|
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
|
|
|
end
|
|
|
|
|
2011-08-31 16:15:59 -04:00
|
|
|
class HamlTemplate < ::Tilt::HamlTemplate
|
2011-07-07 01:56:20 -04:00
|
|
|
include Middleman::CoreExtensions::FrontMatter::YamlAware
|
|
|
|
end
|
2011-06-14 16:54:59 -04:00
|
|
|
|
2011-07-07 01:56:20 -04:00
|
|
|
class SlimTemplate < ::Slim::Template
|
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
|