Class: FluffyBarbarian::Index

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

Instance Method Summary collapse

Constructor Details

#initialize(path = "content/_posts") ⇒ Index

Returns a new instance of Index.



4
5
6
7
8
9
# File 'lib/fluffy_barbarian/index.rb', line 4

def initialize(path="content/_posts")
  @path       = path
  @index_path = "#{@path}.mkd"
  @parsed     = nil
  @on_disk    = nil
end

Instance Method Details

#allObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluffy_barbarian/index.rb', line 53

def all
  @all ||= parsed.map do |post|
    if file = on_disk.find{ |f| f[:autoslug] == post[:autoslug] }
      post[:path]    = file[:path]
      post[:extname] = file[:extname]
      post
    end
  end.compact.each_with_index do |post, index|
    post[:index] = index
    post[:type] = "post"
    post[:updated_on] = updated_on(post)
  end
end

#find(splat, slug) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fluffy_barbarian/index.rb', line 67

def find(splat, slug)
  @post = all.find{|p| p[:slug] == slug.parameterize }

  return nil unless @post

  dir = splat.split("/").tidy.map{ |part| part.parameterize }.join("/")
  if slug != @post[:slug] || dir != @post[:happened_on].tr("-","/")
    @post[:fuzzy] = true
  else
    @post[:content] ||= Tilt.new(@post[:path]).render
    @post[:previous] = all[@post[:index] - 1] if @post[:index] > 0
    @post[:next] = all[@post[:index] + 1]
  end

  @post
end

#on_disk(path = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fluffy_barbarian/index.rb', line 35

def on_disk(path=nil)
  return @on_disk if @on_disk

  path ||= @path

  @on_disk = Dir[File.join(path, "**", "*")].select{ |f| File.file? f }
  @on_disk = @on_disk.sort.map do |file|
    title = File.basename(file, ".*").tidy
    { :title => title, :path => File.expand_path(file),
      :autoslug => title.parameterize,
      :extname => File.extname(file) }
  end
end

#parsed(text = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fluffy_barbarian/index.rb', line 11

def parsed(text=nil)
  return @parsed if @parsed

  text ||= File.read(@index_path)
  @parsed = []
  posts = text.split(/^(# .*$)\n/).tidy
  posts.each_with_index do |title, index|
    next unless title[0..1] == "# "

    post = parse_meta(posts[index+1])
    post[:title] = title[2..-1].tidy

    post[:autoslug] = post[:title].parameterize
    unless post[:slug]
      post[:slug] = post[:autoslug]
    end
    post[:url] = "/#{post[:happened_on].tr("-","/")}/#{post[:slug]}"

    @parsed << post
  end

  @parsed
end

#sane?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fluffy_barbarian/index.rb', line 49

def sane?
  return false if parsed.length != on_disk.length
end