Sha256: 37a9fb8b329b9bc01682dedc3916ea192afaa3ad71cfc188873481469b251d0c

Contents?: true

Size: 838 Bytes

Versions: 8

Compression:

Stored size: 838 Bytes

Contents

require 'yaml'
# Extract YAML frontmatter from data and build metadata table. 

class Gollum::Filter::YAML < Gollum::Filter
  
  # Regexp thanks to jekyll
  YAML_FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m

  def extract(data)
    data.gsub!(YAML_FRONT_MATTER_REGEXP) do
      @markup.metadata ||= {}
      begin
        frontmatter = ::YAML.safe_load(@markup.sanitize.clean(Regexp.last_match[1]))
        @markup.metadata.merge!(frontmatter) if frontmatter.respond_to?(:keys) && frontmatter.respond_to?(:values)
      rescue ::Psych::SyntaxError, ::Psych::DisallowedClass, ::Psych::BadAlias => error
        @markup.metadata['errors'] ||= []
        @markup.metadata['errors'] << "Failed to load YAML frontmater: #{error.message}"
      end
      ''
    end
    data
  end

  def process(data)
    data
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gollum-lib-5.0.a.4-java lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.4 lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.3-java lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.3 lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.2-java lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.2 lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.1-java lib/gollum-lib/filter/yaml.rb
gollum-lib-5.0.a.1 lib/gollum-lib/filter/yaml.rb