Class: Shinmun::Blog

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

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

Constructor Details

#initialize(path) ⇒ Blog

Initialize the blog



15
16
17
18
19
20
21
22
23
24
# File 'lib/shinmun/blog.rb', line 15

def initialize(path)
  super

  @config = {}
  @templates = {}

  load_posts
  load_pages
  sort_posts
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/shinmun/blog.rb', line 7

def config
  @config
end

#pagesObject (readonly)

Returns the value of attribute pages.



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

def pages
  @pages
end

#postsObject (readonly)

Returns the value of attribute posts.



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

def posts
  @posts
end

#posts_by_categoryObject (readonly)

Returns the value of attribute posts_by_category.



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

def posts_by_category
  @posts_by_category
end

#posts_by_dateObject (readonly)

Returns the value of attribute posts_by_date.



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

def posts_by_date
  @posts_by_date
end

#posts_by_tagObject (readonly)

Returns the value of attribute posts_by_tag.



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

def posts_by_tag
  @posts_by_tag
end

Class Method Details

.init(path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shinmun/blog.rb', line 26

def self.init(path)
  path = File.expand_path(path)
  Dir.mkdir(path)

  FileUtils.cp_r "#{ROOT}/public", path
  FileUtils.cp_r "#{ROOT}/templates", path
  FileUtils.cp "#{ROOT}/config.ru", path

  Dir.mkdir("#{path}/posts")
  Dir.mkdir("#{path}/pages")
  File.open("#{path}/Gemfile", "w") do |io|
    io.puts('gem "shinmun"')
  end
end

Instance Method Details

#archivesObject

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



124
125
126
# File 'lib/shinmun/blog.rb', line 124

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

#call(env) ⇒ Object



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

def call(env)
  reload_changed_files # if ENV['RACK_ENV'] != 'production'
  super
end

#find_category(permalink) ⇒ Object



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

def find_category(permalink)
  categories.find { |name| urlify(name) == permalink }
end

#load_pagesObject



61
62
63
64
65
66
67
68
# File 'lib/shinmun/blog.rb', line 61

def load_pages
  @pages = {}

  Dir["#{ path }/pages/*.md"].each do |file|
    page = Post.new(:file => file)
    @pages[page.name] = page
  end
end

#load_postsObject



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

def load_posts
  @posts = Dir["#{ path }/posts/**/*.md"].map do |file|
    Post.new(:file => file)
  end
end

#posts_with_tags(tags) ⇒ Object

Return all posts with any of given tags.



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

def posts_with_tags(tags)
  return [] if tags.nil? or tags.empty?
  tags = tags.split(',').map { |t| t.strip } if tags.is_a?(String)

  tags.map { |tag| posts_by_tag[tag] }.flatten.uniq
end

#reload_changed_filesObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/shinmun/blog.rb', line 45

def reload_changed_files
  (@posts + @pages.values).each do |post|
    if post.changed?
      post.load
      @changed = true
    end
  end

  sort_posts if @changed
end

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



41
42
43
# File 'lib/shinmun/blog.rb', line 41

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

#sort_postsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/shinmun/blog.rb', line 76

def sort_posts      
  @posts = @posts.sort_by { |post| post.date.to_s }.reverse
  
  @posts_by_category = Hash.new do |hash, category|
    hash[category] = []
  end
  
  @posts_by_tag = Hash.new do |hash, tag|
    hash[tag] = []
  end

  @posts_by_date = Hash.new do |hash, year|
    hash[year] = Hash.new do |hash_months, month|
      hash_months[month] = Hash.new
    end
  end
  
  @posts.each do |post|
    post.tag_list.each { |tag| @posts_by_tag[tag] << post }
    @posts_by_category[post.category] << post if post.category
    @posts_by_date[post.year][post.month][post.name] = post
  end
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

#urlObject



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

def url
  "http://#{request.host}"
end