Sha256: 8d045c32f7682cad1e4ab6ef599a380a8ff6bd17035db086dac2efa99684803a

Contents?: true

Size: 630 Bytes

Versions: 3

Compression:

Stored size: 630 Bytes

Contents

module Lookbook
  class FrontmatterExtractor < Service
    FRONTMATTER_REGEX = /\A---(.|\n)*?---/

    attr_reader :content

    def initialize(content)
      @content = content.to_s
    end

    def call
      frontmatter = extract_frontmatter(content)
      rest = strip_frontmatter(content)
      [frontmatter, rest]
    end

    protected

    def extract_frontmatter(text)
      matches = text.match(FRONTMATTER_REGEX)
      data = matches ? YAML.safe_load(matches[0]).deep_symbolize_keys : {}
      DataObject.new(data)
    end

    def strip_frontmatter(text)
      text.gsub(FRONTMATTER_REGEX, "").strip
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lookbook-3.0.0.alpha.2 lib/lookbook/services/frontmatter_extractor.rb
lookbook-3.0.0.alpha.1 lib/lookbook/services/frontmatter_extractor.rb
lookbook-3.0.0.alpha.0 lib/lookbook/services/frontmatter_extractor.rb