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-2.3.4 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.3.3 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.3.2 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.3.0 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.2.2 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.2.1 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.2.0 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.1.1 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.1.0 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.5 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.4 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.3 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.2 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.1 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.0 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.0.rc.3 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-1.5.5 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-1.5.4 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.0.rc.2 lib/lookbook/services/templates/frontmatter_extractor.rb
lookbook-2.0.0.rc.1 lib/lookbook/services/templates/frontmatter_extractor.rb