Sha256: 64093f7171e2530d01ae939d7217a5c64a1981bf90a8dfba7eb37c0706614669

Contents?: true

Size: 1.04 KB

Versions: 89

Compression:

Stored size: 1.04 KB

Contents

module Brief
  class Document
    module FrontMatter
      extend ActiveSupport::Concern

      def frontmatter= attributes
        @frontmatter = attributes
      end

      def data= attributes
        self.frontmatter= attributes
      end

      def frontmatter
        (@frontmatter || load_frontmatter).tap do |d|
          d[:type] ||= parent_folder_name && parent_folder_name.to_s.singularize if parent_folder_name && parent_folder_name.length > 0
        end
      end

      def frontmatter_line_count
        (@raw_frontmatter && @raw_frontmatter.lines.count) || 0
      end

      protected

      def load_frontmatter
        if raw_content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
          self.content = raw_content[(Regexp.last_match[1].size + Regexp.last_match[2].size)..-1]
          @frontmatter_line_count = Regexp.last_match[1].lines.size
          @raw_frontmatter = Regexp.last_match[1]
          @frontmatter = YAML.load(Regexp.last_match[1]).to_mash
        else
          @frontmatter = {}.to_mash
        end
      end
    end
  end
end

Version data entries

89 entries across 89 versions & 1 rubygems

Version Path
brief-1.9.4 lib/brief/document/front_matter.rb
brief-1.9.3 lib/brief/document/front_matter.rb
brief-1.9.2 lib/brief/document/front_matter.rb
brief-1.9.1 lib/brief/document/front_matter.rb
brief-1.9.0 lib/brief/document/front_matter.rb
brief-1.8.12 lib/brief/document/front_matter.rb
brief-1.8.11 lib/brief/document/front_matter.rb
brief-1.8.10 lib/brief/document/front_matter.rb
brief-1.8.9 lib/brief/document/front_matter.rb
brief-1.8.8 lib/brief/document/front_matter.rb
brief-1.8.6 lib/brief/document/front_matter.rb
brief-1.8.5 lib/brief/document/front_matter.rb
brief-1.8.4 lib/brief/document/front_matter.rb
brief-1.8.3 lib/brief/document/front_matter.rb
brief-1.8.2 lib/brief/document/front_matter.rb
brief-1.8.1 lib/brief/document/front_matter.rb
brief-1.8.0 lib/brief/document/front_matter.rb
brief-1.7.3 lib/brief/document/front_matter.rb
brief-1.7.2 lib/brief/document/front_matter.rb
brief-1.7.1 lib/brief/document/front_matter.rb