Class: Dadablog::Blog

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/dadablog/blog.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Blog

Returns a new instance of Blog.



15
16
17
18
19
# File 'lib/dadablog/blog.rb', line 15

def initialize(root)
  self.class.set :root, root
  super
  deploy
end

Instance Method Details

#deployObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dadablog/blog.rb', line 46

def deploy

  articles = []

  Dir.glob "#{settings.root}/articles/*md" do |file|
    meta, content = File.read(file, {:encoding => 'utf-8'}).split("\n\n", 2)

    article = OpenStruct.new YAML.load(meta)

    article.date = Time.parse article.date.to_s

    article.content = content

    article.slug = File.basename(file, '.md')

    self.class.get "/articles/#{article.slug}" do
      erb :post, :locals => {:article => article}
    end

    articles << article
  end

  articles.sort_by! { |article| article.date }
  articles.reverse!

  self.class.set :articles, articles

  self.class.get '/' do
    erb :index
  end
end