Sha256: ffd55e890556e58ae68cedf3d4898afe3fabd888ca80743331e72f8152c8be1a

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Munge
  class System
    class ItemFactory
      class ContentParser
        def self.match(string)
          string.match(/
            # Start of string
            \A
            # Begin frontmatter
            (?:^---\s*[\n\r]+)
            # Capture frontmatter
            (.*)
            # End frontmatter
            (?:^---\s*[\n\r]+)
            /mx)
        end

        def self.parse(string)
          matchdata = match(string)

          [
            parse_frontmatter(matchdata),
            parse_content(matchdata, string)
          ]
        end

        def self.parse_frontmatter(matchdata)
          return {} if matchdata.nil?

          parsed_frontmatter = YAML.safe_load(matchdata[1])

          frontmatter_hash =
            if parsed_frontmatter.is_a?(Hash)
              parsed_frontmatter
            else
              {}
            end

          Munge::Util::SymbolHash.deep_convert(frontmatter_hash)
        end

        def self.parse_content(matchdata, string)
          if matchdata
            matchdata.post_match
          else
            string
          end
        end

        def initialize(string)
          @frontmatter, @content = self.class.parse(string)
        end

        attr_accessor :frontmatter, :content
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
munge-0.18.0 lib/munge/system/item_factory/content_parser.rb
munge-0.17.0 lib/munge/system/item_factory/content_parser.rb
munge-0.16.0 lib/munge/system/item_factory/content_parser.rb