Sha256: 116ab8113693f60825944a77f2e3bab1b1ceef3348ea15dcc6b1599b6f8647f2

Contents?: true

Size: 596 Bytes

Versions: 44

Compression:

Stored size: 596 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)
      matches ? YAML.safe_load(matches[0]).deep_symbolize_keys : {}
    end

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

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
lookbook-1.3.4 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-1.3.3 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-1.3.1 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-1.3.0 lib/lookbook/services/templates/frontmatter_extractor.rb