Sha256: 10669a756dc476c3b7c957474a6ec3836e8117ebb1e667c03893303e3bd84fda

Contents?: true

Size: 1.09 KB

Versions: 12

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "pakyow/support/indifferentize"

module Pakyow
  module Presenter
    # Parses front matter from text files.
    #
    # @api private
    module FrontMatterParser
      MATTER_MATCHER = /\A---\s*\n(.*?\n?)^---\s*$\n?/m

      # Parses HTML and returns a hash of front matter info
      #
      def self.parse(html_string, _file = nil)
        unless match = html_string.match(MATTER_MATCHER)
          return Support::IndifferentHash.new
        end

        begin
          Support::IndifferentHash.deep(YAML.load(match.captures[0]).to_h)
        rescue Psych::SyntaxError => error
          raise FrontMatterParsingError.build(error, context: match.captures[0])
        end
      end

      # Returns HTML with front matter removed
      #
      def self.scrub(html_string)
        html_string.gsub(MATTER_MATCHER, "")
      end

      # Parses HTML and returns:
      # - a hash of front matter info
      # - the HTML with front matter removed
      #
      def self.parse_and_scrub(html_string)
        return parse(html_string), scrub(html_string)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pakyow-presenter-1.0.6 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.5 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.4 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.3 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.2 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.1 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0.rc5 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0.rc4 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0.rc3 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0.rc2 lib/pakyow/presenter/front_matter_parser.rb
pakyow-presenter-1.0.0.rc1 lib/pakyow/presenter/front_matter_parser.rb