1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00
middleman--middleman/lib/middleman/core_extensions/front_matter.rb

112 lines
3.3 KiB
Ruby
Raw Normal View History

require "yaml"
require "tilt"
module Middleman::CoreExtensions::FrontMatter
class << self
def registered(app)
app.extend ClassMethods
2011-06-08 00:44:10 -04:00
::Tilt::register RDiscountTemplate, 'markdown', 'mkd', 'md'
2011-07-08 16:23:06 -04:00
::Tilt::register RedcarpetTemplate, 'markdown', 'mkd', 'md'
::Tilt::register MarukuTemplate, 'markdown', 'mkd', 'md'
2011-08-12 01:42:16 -04:00
::Tilt::register KramdownTemplate, 'markdown', 'mkd', 'md'
app.set :markdown_engine, MarukuTemplate
2011-07-08 16:23:06 -04:00
::Tilt::register RedClothTemplate, 'textile'
2011-07-08 16:23:06 -04:00
::Tilt.prefer(RedClothTemplate)
::Tilt::register ERBTemplate, 'erb', 'rhtml'
2011-07-08 16:23:06 -04:00
::Tilt.prefer(ERBTemplate)
::Tilt::register SlimTemplate, 'slim'
2011-07-08 16:23:06 -04:00
::Tilt.prefer(SlimTemplate)
::Tilt::register HamlTemplate, 'haml'
2011-07-08 16:23:06 -04:00
::Tilt.prefer(HamlTemplate)
app.after_configuration do
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-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-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
end
2011-07-13 20:48:31 -04:00
# Forward remaining data to helpers
app.data_content("page", data)
end
true
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}"
end
2011-07-13 20:48:31 -04:00
content = content.split(yaml_regex).last
end
2011-07-13 20:48:31 -04:00
data ||= {}
[data, content]
end
end
module ClassMethods
def parse_front_matter(content)
Middleman::CoreExtensions::FrontMatter.parse_front_matter(content)
end
end
2011-06-08 00:44:10 -04:00
module YamlAware
def prepare
options, @data = Middleman::CoreExtensions::FrontMatter.parse_front_matter(@data)
2011-06-08 00:44:10 -04:00
super
end
end
2011-06-08 00:44:10 -04:00
class RDiscountTemplate < ::Tilt::RDiscountTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
2011-06-08 00:44:10 -04:00
end
2011-07-08 16:23:06 -04:00
class RedcarpetTemplate < ::Tilt::RedcarpetTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
class MarukuTemplate < ::Tilt::MarukuTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
2011-06-08 00:44:10 -04:00
class RedClothTemplate < ::Tilt::RedClothTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
2011-06-08 00:44:10 -04:00
end
2011-08-12 01:42:16 -04:00
class KramdownTemplate < ::Tilt::KramdownTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
2011-06-14 16:54:59 -04:00
class ERBTemplate < ::Tilt::ERBTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
2011-06-14 16:54:59 -04:00
end
class HamlTemplate < ::Tilt::HamlTemplate
include Middleman::CoreExtensions::FrontMatter::YamlAware
end
2011-06-14 16:54:59 -04:00
class SlimTemplate < ::Slim::Template
include Middleman::CoreExtensions::FrontMatter::YamlAware
2011-06-14 16:54:59 -04:00
end
2011-06-08 00:44:10 -04:00
end