Module: Yarrow::Tools::FrontMatter

Defined in:
lib/yarrow/tools/front_matter.rb

Instance Method Summary collapse

Instance Method Details

#extract_split_content(text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yarrow/tools/front_matter.rb', line 9

def extract_split_content(text)
  pattern = /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
  if text =~ pattern
    content = text.sub(pattern, "")

    begin
      meta = YAML.load($1)
      return [content, meta]
    rescue Psych::SyntaxError => error
      if defined? ::Logger
        # todo: application wide logger
        logger = ::Logger.new(STDOUT)
        logger.error "#{error.message}"
      end
      return [content, nil]
    end

  end

  [text, nil]
end

#read_split_content(path) ⇒ Object



5
6
7
# File 'lib/yarrow/tools/front_matter.rb', line 5

def read_split_content(path)
  extract_split_content(File.read(path, :encoding => 'utf-8'))
end