Sha256: 38f08717a2c663775cf2106da12860b8aeea4586d861226140e056ea603e8526

Contents?: true

Size: 717 Bytes

Versions: 3

Compression:

Stored size: 717 Bytes

Contents

require 'metadown'

module LowVoltage
  class Post
    attr_reader :title, :date, :id, :content, :author

    def initialize(path)
      @id = File.basename(path, ".md")
      @path = path

      begin
        parse_post(Metadown.render(IO.read(@path)))
      rescue Errno::ENOENT
        raise ActionController::RoutingError, "Post not found: #{path}"
      end
    end

    private

    def parse_post(data)
      @content = data.output.html_safe
      parse_metadata(data.metadata)
    end

    def parse_metadata(metadata)
      @title = metadata['title'] || @id.titleize
      @date = Date.parse(metadata['date'] || File.mtime(@path).inspect)
      @author = metadata['author'] || "Anonymous"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
low_voltage-0.0.3 lib/low_voltage/post.rb
low_voltage-0.0.2 lib/low_voltage/post.rb
low_voltage-0.0.1 lib/low_voltage/post.rb