class PlainSite::Data::FrontMatterFile

Constants

DELIMITER

Attributes

path[R]

YAML Front Matter File Example file content:

---
title: Hello,world!
tags: [C,Java,Ruby,Haskell]
---
File content Here!

Public Class Methods

new(path) click to toggle source
# File lib/PlainSite/Data/FrontMatterFile.rb, line 21
def initialize(path)
  # The String file path
  @path=path
  @content_pos=0
end

Public Instance Methods

content() click to toggle source

Intended no cache, listen directory changes not work on platforms other than linux

# File lib/PlainSite/Data/FrontMatterFile.rb, line 54
def content
  self.headers # init @content_pos
  File.open(path) do |f|
    f.seek @content_pos,IO::SEEK_SET
    @content=f.read.strip.freeze
  end
end
headers() click to toggle source
# File lib/PlainSite/Data/FrontMatterFile.rb, line 27
def headers
  File.open(@path) do |f|
    line=f.readline.strip
    break if line!=DELIMITER
    header_lines=[]
    begin
      while (line=f.readline.strip)!=DELIMITER
        header_lines.push line
      end
      @headers = YAML.safe_load(header_lines.join "\n")
      unless Hash===@headers
        raise InvalidFrontMatterFileException,"Front YAML must be Hash,not #{@headers.class},in file: #{path}"
      end
      @content_pos=f.pos
      @headers['path'] = @path
      return @headers
    rescue YAML::SyntaxError => e
      raise  InvalidFrontMatterFileException,"YAML SyntaxError:#{e.message},in file: #{path}"
    rescue EOFError => e
      raise InvalidFrontMatterFileException,"Unclosed YAML in file: #{path}"
    end
  end

  return {"path" => @path }
end