Sha256: 8073ce8050ed09558d192e6ebbc89c1e9e5c4fb1dc926b45ecec19e33b5533d2

Contents?: true

Size: 1.27 KB

Versions: 13

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.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

13 entries across 13 versions & 1 rubygems

Version Path
munge-0.15.1 lib/munge/system/item_factory/content_parser.rb
munge-0.15.0 lib/munge/system/item_factory/content_parser.rb
munge-0.14.0 lib/munge/system/item_factory/content_parser.rb
munge-0.13.0 lib/munge/system/item_factory/content_parser.rb
munge-0.12.0 lib/munge/system/item_factory/content_parser.rb
munge-0.11.1 lib/munge/system/item_factory/content_parser.rb
munge-0.11.0 lib/munge/system/item_factory/content_parser.rb
munge-0.10.0 lib/munge/system/item_factory/content_parser.rb
munge-0.9.0 lib/munge/system/item_factory/content_parser.rb
munge-0.8.0 lib/munge/system/item_factory/content_parser.rb
munge-0.7.1 lib/munge/system/item_factory/content_parser.rb
munge-0.7.0 lib/munge/system/item_factory/content_parser.rb
munge-0.6.0 lib/munge/system/item_factory/content_parser.rb