Class: Snippr::MetaData

Inherits:
Object
  • Object
show all
Defined in:
lib/snippr/meta_data.rb

Constant Summary collapse

INCLUDE =
"_include"

Class Method Summary collapse

Class Method Details

.extract(name, content, snippet = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/snippr/meta_data.rb', line 14

def self.extract(name, content, snippet=nil)
  if content =~ /^(---\s*\n.*?\n?)^(---\s*$?)/m
    content = Regexp.last_match.post_match.strip
    meta = yaml_load(name, $1)
    if meta
      if meta.keys.include?(INCLUDE)
        Array(meta[INCLUDE]).each do |include_path|
          if (snippet && include_path.start_with?("./"))
            include_path = snippet.name.gsub(/\/.*?$/,"") + "/" + include_path.gsub(/^\.\//, "")
          end
          snippet = Snippr.load(include_path)
          meta = deep_yaml_merge(snippet.meta, meta)
        end
      end
    end
  end

  meta = meta ? meta : {}

  [content, meta]
end