class Post

Constants

DIV

Attributes

base_dir[RW]
glob[RW]

Public Class Methods

categories() click to toggle source
# File lib/post.rb, line 20
def categories
  find.reduce({}) do |tag_score, post| 
    (post[:tags] || []).each do |tag|
      tag_score[tag] ||= 0
      tag_score[tag] += 1
    end
    tag_score
  end.sort
end
filenames() click to toggle source
# File lib/post.rb, line 10
def filenames
  Dir.glob File.join(base_dir, Post.glob)
end
find() click to toggle source
# File lib/post.rb, line 34
def find
  filenames \
    .map { |path| from_file(path) } \
    .sort { |p1,p2| p2[:date] <=> p1[:date] }
end
find_by_tag(tag) click to toggle source
# File lib/post.rb, line 30
def find_by_tag(tag)
  find.select { |post| (post[:tags] || []).map { |t| t.downcase }.include? tag.downcase }
end
find_latest() click to toggle source
# File lib/post.rb, line 14
def find_latest
  find.first
end
Also aliased as: find_single
find_single()
Alias for: find_latest
from_file(path) click to toggle source
# File lib/post.rb, line 40
def from_file(path)
  date = Date.parse(File.basename(path)[0..9])
  match = path.match(/posts\/(.+)\//)
  template = (match[1] + '_post').to_sym if match
  meta, textile = split_and_parse read_file_strip_bom(path)
  new meta, textile, date, path, template
end
from_s(raw, date = nil) click to toggle source
# File lib/post.rb, line 48
def from_s(raw, date = nil)
  date ||= Date.today
  meta, textile = split_and_parse raw
  new meta, textile, date
end
new(meta, textile, date, path = nil, template = nil) click to toggle source
# File lib/post.rb, line 69
def initialize(meta, textile, date, path = nil, template = nil)
  @meta = meta
  @textile = textile
  @path = path
  @meta['date'] = date
  @meta['template'] = template
end

Private Class Methods

read_file_strip_bom(path) click to toggle source
# File lib/post.rb, line 56
def read_file_strip_bom(path)
  IO.read(path, 3) == "\357\273\277" ? IO.read(path, nil, 3) : IO.read(path)
end
split_and_parse(raw) click to toggle source
# File lib/post.rb, line 60
def split_and_parse(raw)
  meta, body = {}, ''
  header_body = raw.split(DIV)
  meta = YAML.load(header_body.first) if raw =~ DIV
  body = header_body.last if (header_body.size == 1 and raw !~ DIV) or header_body.size == 2
  [meta, body]
end

Public Instance Methods

[](property) click to toggle source
# File lib/post.rb, line 77
def [](property)
  @meta[property.to_s]
end
html() click to toggle source
# File lib/post.rb, line 81
def html
  r = RedCloth.new(@textile)
  r.extend VideoTag
  r.to_html
end
path() click to toggle source
# File lib/post.rb, line 87
def path
  @path
end
slug() click to toggle source
# File lib/post.rb, line 91
def slug
  return '#' unless @path
  File.basename(@path, '.textile').sub('-','/').sub('-','/').sub('-','/')
end