class Slimdown::PageParser

Internal class to manage parsing the markdown document.

Public Class Methods

new(path) click to toggle source

@param [String] path The absolute path to the markdown document.

# File lib/slimdown/page_parser.rb, line 8
def initialize(path)
  @path = path

  parse_file
end

Public Instance Methods

body() click to toggle source

The parsed markdown document body.

@return [Kramdown::Document] a markdown document object.

# File lib/slimdown/page_parser.rb, line 33
def body
  Kramdown::Document.new(@body_text)
end
headers() click to toggle source

A hash of the headers in the document

Example:

{
  "title" => "Test title",
  "template" => "test_template",
}

@return [Hash] document headers

# File lib/slimdown/page_parser.rb, line 23
def headers
  head = {}
  head = YAML.load @header_text unless @header_text == ''

  head
end

Private Instance Methods

parse_file() click to toggle source
# File lib/slimdown/page_parser.rb, line 39
def parse_file
  # Simple state machine.
  part = nil

  @header_text = ''
  @body_text = ''

  File.open(@path, 'r') do |f|
    f.each_line do |line|
      if line.strip == '---' && !part
        part = :header
      elsif line.strip == '---' && part == :header
        part = :body
      elsif part == :header
        @header_text += line
      elsif part == :body || part == nil
        @body_text += line
      end
    end
  end

rescue Errno::ENOENT
  raise Slimdown::Exception, 'Page not found'
end