Class: Post

Inherits:
Object
  • Object
show all
Defined in:
lib/post.rb

Constant Summary collapse

DIV =
/\r?\n---/

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta, textile, date, path = nil, template = nil) ⇒ Post

Returns a new instance of Post.



69
70
71
72
73
74
75
# 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

Class Attribute Details

.base_dirObject

Returns the value of attribute base_dir.



8
9
10
# File 'lib/post.rb', line 8

def base_dir
  @base_dir
end

.globObject

Returns the value of attribute glob.



8
9
10
# File 'lib/post.rb', line 8

def glob
  @glob
end

Class Method Details

.categoriesObject



20
21
22
23
24
25
26
27
28
# 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

.filenamesObject



10
11
12
# File 'lib/post.rb', line 10

def filenames
  Dir.glob File.join(base_dir, Post.glob)
end

.findObject



34
35
36
37
38
# 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) ⇒ Object



30
31
32
# 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_latestObject Also known as: find_single



14
15
16
# File 'lib/post.rb', line 14

def find_latest
  find.first
end

.from_file(path) ⇒ Object



40
41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
# 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

Instance Method Details

#[](property) ⇒ Object



77
78
79
# File 'lib/post.rb', line 77

def [](property)
  @meta[property.to_s]
end

#htmlObject



81
82
83
84
85
# File 'lib/post.rb', line 81

def html
  r = RedCloth.new(@textile)
  r.extend VideoTag
  r.to_html
end

#pathObject



87
88
89
# File 'lib/post.rb', line 87

def path
  @path
end

#slugObject



91
92
93
94
# File 'lib/post.rb', line 91

def slug
  return '#' unless @path
  File.basename(@path, '.textile').sub('-','/').sub('-','/').sub('-','/')
end