Class: Shinmun::Blog

Inherits:
Kontrol::Application
  • Object
show all
Includes:
Helpers
Defined in:
lib/shinmun/blog.rb

Constant Summary collapse

EXAMPLE_DIR =
File.expand_path(File.dirname(__FILE__) + '/../../example')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#archive_link, #archive_path, #diff_line_class, #distance_of_time_in_words, #html_escape, #human_date, #navi_link, #post_link, #post_path, #rfc822

Constructor Details

#initialize(path) ⇒ Blog

Initialize the blog



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shinmun/blog.rb', line 20

def initialize(path)
  super

  @aggregations = {}

  if ENV['RACK_ENV'] == 'production'
    @store = GitStore.new(path)
  else
    @store = GitStore::FileStore.new(path)
  end
  
  @store.load
  
  @repo = Grit::Repo.new(path) if defined?(Grit)
  
  Thread.start do
    loop do
      load_aggregations
      sleep 300
    end
  end
end

Instance Attribute Details

#aggregationsObject (readonly)

Returns the value of attribute aggregations.



9
10
11
# File 'lib/shinmun/blog.rb', line 9

def aggregations
  @aggregations
end

#categoriesObject (readonly)

Returns the value of attribute categories.



9
10
11
# File 'lib/shinmun/blog.rb', line 9

def categories
  @categories
end

#commentsObject (readonly)

Returns the value of attribute comments.



9
10
11
# File 'lib/shinmun/blog.rb', line 9

def comments
  @comments
end

#repoObject (readonly)

Returns the value of attribute repo.



9
10
11
# File 'lib/shinmun/blog.rb', line 9

def repo
  @repo
end

#storeObject (readonly)

Returns the value of attribute store.



9
10
11
# File 'lib/shinmun/blog.rb', line 9

def store
  @store
end

Class Method Details

.init(name) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/shinmun/blog.rb', line 43

def self.init(name)
  Dir.mkdir name      
  Dir.chdir name
  FileUtils.cp_r EXAMPLE_DIR + '/.', '.'
  `git init`
  `git add .`
  `git commit -m 'init'`
end

Instance Method Details

#archivesObject

Return all archives as tuples of [year, month].



96
97
98
# File 'lib/shinmun/blog.rb', line 96

def archives
  posts.map { |p| [p.year, p.month] }.uniq.sort
end

#call(env) ⇒ Object



60
61
62
63
# File 'lib/shinmun/blog.rb', line 60

def call(env)
  store.refresh!      
  super
end

#comments_for(path) ⇒ Object



137
138
139
# File 'lib/shinmun/blog.rb', line 137

def comments_for(path)
  comments[path + '.yml'] || []
end

#create_post(atts) ⇒ Object

Create a new post with given attributes.



116
117
118
119
120
121
# File 'lib/shinmun/blog.rb', line 116

def create_post(atts)
  post = Post.new(atts)
  transaction "create '#{post.title}'" do
    store[post.path] = post
  end
end

#delete_post(post) ⇒ Object



131
132
133
134
135
# File 'lib/shinmun/blog.rb', line 131

def delete_post(post)
  transaction "delete '#{post.title}'" do
    store.delete(post.path)
  end
end

#find_category(permalink) ⇒ Object



155
156
157
158
159
# File 'lib/shinmun/blog.rb', line 155

def find_category(permalink)
  name = categories.find { |name| urlify(name) == permalink } or raise "category not found"
  posts = self.posts.select { |p| p.category == name }.sort_by { |p| p.date }.reverse
  { :name => name, :posts => posts, :permalink => permalink }
end

#find_page(name) ⇒ Object



147
148
149
# File 'lib/shinmun/blog.rb', line 147

def find_page(name)
  pages.find { |p| p.name == name }
end

#find_post(year, month, name) ⇒ Object



151
152
153
# File 'lib/shinmun/blog.rb', line 151

def find_post(year, month, name)
  tree = posts[year, month] and tree.find { |p| p.name == name }
end

#load_aggregationsObject



65
66
67
68
69
# File 'lib/shinmun/blog.rb', line 65

def load_aggregations
  config['aggregations.yml'].to_a.each do |c|
    aggregations[c['name']] = Object.const_get(c['class']).new(c['url'])
  end
end

#load_template(file) ⇒ Object



52
53
54
# File 'lib/shinmun/blog.rb', line 52

def load_template(file)
  templates[file] or raise "template #{file} not found"
end

#post_comment(path, params) ⇒ Object



141
142
143
144
145
# File 'lib/shinmun/blog.rb', line 141

def post_comment(path, params)
  transaction "new comment for '#{path}'" do
    comments[path + '.yml'] = comments[path + '.yml'].to_a + [ Comment.new(params) ]
  end
end

#posts_by_dateObject



71
72
73
# File 'lib/shinmun/blog.rb', line 71

def posts_by_date
  posts.sort_by { |post| post.date.to_s }.reverse
end

#posts_for_month(year, month) ⇒ Object

Return all posts for a given month.



80
81
82
# File 'lib/shinmun/blog.rb', line 80

def posts_for_month(year, month)
  posts_by_date.select { |p| p.year == year and p.month == month }
end

#posts_with_tags(tags) ⇒ Object

Return all posts with any of given tags.



85
86
87
88
89
90
91
92
93
# File 'lib/shinmun/blog.rb', line 85

def posts_with_tags(tags)
  return [] if tags.nil? or tags.empty?
  tags = tags.split(',').map { |t| t.strip } if tags.is_a?(String)
  posts.select do |post|
    tags.any? do |tag| 
      post.tag_list.include?(tag)
    end
  end
end

#recent_postsObject



75
76
77
# File 'lib/shinmun/blog.rb', line 75

def recent_posts
  posts_by_date[0, 20]
end

#render(name, vars = {}) ⇒ Object



56
57
58
# File 'lib/shinmun/blog.rb', line 56

def render(name, vars = {})
  super(name, vars.merge(:blog => self))
end

#symbolize_keys(hash) ⇒ Object



104
105
106
107
108
109
# File 'lib/shinmun/blog.rb', line 104

def symbolize_keys(hash)      
  hash.inject({}) do |h, (k, v)|
    h[k.to_sym] = v
    h
  end
end

#transaction(message, &block) ⇒ Object



111
112
113
# File 'lib/shinmun/blog.rb', line 111

def transaction(message, &block)
  store.transaction(message, &block)
end

#tree(post) ⇒ Object



100
101
102
# File 'lib/shinmun/blog.rb', line 100

def tree(post)
  post.date ? posts.tree(post.year).tree(post.month) : pages
end

#update_post(post, data) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/shinmun/blog.rb', line 123

def update_post(post, data)
  transaction "update '#{post.title}'" do
    store.delete(post.path)
    post.parse data
    store[post.path] = post
  end
end

#write(file, template, vars = {}) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/shinmun/blog.rb', line 161

def write(file, template, vars={})
  file = "public/#{base_path}/#{file}"
  FileUtils.mkdir_p(File.dirname(file))
  open(file, 'wb') do |io|
    io << render(template, vars)
  end
end