mirror of
https://github.com/middleman/middleman.git
synced 2022-11-09 12:20:27 -05:00
more robust frontmatter impl, still need to tie into Tilt
This commit is contained in:
parent
80e120060b
commit
9849e2aa2f
3 changed files with 46 additions and 18 deletions
|
@ -71,6 +71,9 @@ module Middleman::Features
|
|||
# to dynamic requests.
|
||||
autoload :Data, "middleman/features/data"
|
||||
|
||||
# Parse YAML metadata from templates
|
||||
autoload :FrontMatter, "middleman/features/front_matter"
|
||||
|
||||
# Proxy web services requests in dev mode only
|
||||
# autoload :Proxy, "middleman/features/proxy"
|
||||
|
||||
|
|
38
lib/middleman/features/front_matter.rb
Normal file
38
lib/middleman/features/front_matter.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
require "yaml"
|
||||
require "tilt"
|
||||
|
||||
module Middleman::Features::FrontMatter
|
||||
class << self
|
||||
def registered(app)
|
||||
app.extend ClassMethods
|
||||
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
|
||||
end
|
||||
|
||||
# class FrontMatter < Tilt::RDiscountTemplate
|
||||
# def prepare
|
||||
# options, @data = Middleman::Server.parse_front_matter(@data)
|
||||
# super
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# Tilt.register 'markdown', FrontMatter
|
|
@ -40,6 +40,9 @@ module Middleman
|
|||
# Activate custom features
|
||||
register Middleman::Features
|
||||
|
||||
# Activate Yaml Front Matter
|
||||
register Middleman::Features::FrontMatter
|
||||
|
||||
# Activate built-in helpers
|
||||
register Middleman::Features::DefaultHelpers
|
||||
|
||||
|
@ -139,23 +142,6 @@ module Middleman
|
|||
path.gsub(%r{^/}, '')
|
||||
end
|
||||
|
||||
def self.parse_front_matter(path)
|
||||
content = File.read(File.join(settings.views, path))
|
||||
|
||||
if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
|
||||
content = $POSTMATCH
|
||||
|
||||
begin
|
||||
data = YAML.load($1)
|
||||
rescue => e
|
||||
puts "YAML Exception: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
data ||= {}
|
||||
[data, content]
|
||||
end
|
||||
|
||||
# Internal method to look for templates and evaluate them if found
|
||||
def process_request(options={})
|
||||
# Normalize the path and add index if we're looking at a directory
|
||||
|
@ -172,7 +158,8 @@ module Middleman
|
|||
|
||||
|
||||
full_file_path = "#{extensionless_path}.#{template_engine}"
|
||||
data, content = self.class.parse_front_matter(full_file_path)
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue