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.11.9 lib/brief/document/front_matter.rb
brief-1.11.8 lib/brief/document/front_matter.rb
brief-1.11.7 lib/brief/document/front_matter.rb
brief-1.11.6 lib/brief/document/front_matter.rb
brief-1.11.5 lib/brief/document/front_matter.rb
brief-1.11.4 lib/brief/document/front_matter.rb
brief-1.11.3 lib/brief/document/front_matter.rb
brief-1.11.2 lib/brief/document/front_matter.rb
brief-1.11.1 lib/brief/document/front_matter.rb
brief-1.11.0 lib/brief/document/front_matter.rb
brief-1.10.1 lib/brief/document/front_matter.rb
brief-1.10.0 lib/brief/document/front_matter.rb
brief-1.9.14 lib/brief/document/front_matter.rb
brief-1.9.13 lib/brief/document/front_matter.rb
brief-1.9.12 lib/brief/document/front_matter.rb
brief-1.9.11 lib/brief/document/front_matter.rb
brief-1.9.9 lib/brief/document/front_matter.rb
brief-1.9.8 lib/brief/document/front_matter.rb
brief-1.9.7 lib/brief/document/front_matter.rb
brief-1.9.6 lib/brief/document/front_matter.rb