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.17.14 lib/brief/document/front_matter.rb
brief-1.17.13 lib/brief/document/front_matter.rb
brief-1.17.12 lib/brief/document/front_matter.rb
brief-1.17.11 lib/brief/document/front_matter.rb
brief-1.17.10 lib/brief/document/front_matter.rb
brief-1.17.9 lib/brief/document/front_matter.rb
brief-1.17.8 lib/brief/document/front_matter.rb
brief-1.17.7 lib/brief/document/front_matter.rb
brief-1.17.5 lib/brief/document/front_matter.rb
brief-1.17.4 lib/brief/document/front_matter.rb
brief-1.17.3 lib/brief/document/front_matter.rb
brief-1.17.2 lib/brief/document/front_matter.rb
brief-1.17.1 lib/brief/document/front_matter.rb
brief-1.17.0 lib/brief/document/front_matter.rb
brief-1.16.2 lib/brief/document/front_matter.rb
brief-1.16.1 lib/brief/document/front_matter.rb
brief-1.16.0 lib/brief/document/front_matter.rb
brief-1.15.5 lib/brief/document/front_matter.rb
brief-1.15.4 lib/brief/document/front_matter.rb
brief-1.15.3 lib/brief/document/front_matter.rb