Class: Shinmun::Blog
- Inherits:
-
Kontrol::Application
- Object
- Kontrol::Application
- Shinmun::Blog
- Includes:
- Helpers
- Defined in:
- lib/shinmun/blog.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#posts_by_category ⇒ Object
readonly
Returns the value of attribute posts_by_category.
-
#posts_by_date ⇒ Object
readonly
Returns the value of attribute posts_by_date.
-
#posts_by_tag ⇒ Object
readonly
Returns the value of attribute posts_by_tag.
Class Method Summary collapse
Instance Method Summary collapse
-
#archives ⇒ Object
Return all archives as tuples of [year, month, posts].
- #call(env) ⇒ Object
- #find_category(permalink) ⇒ Object
-
#initialize(path) ⇒ Blog
constructor
Initialize the blog.
- #load_pages ⇒ Object
- #load_posts ⇒ Object
-
#posts_with_tags(tags) ⇒ Object
Return all posts with any of given tags.
- #reload_changed_files ⇒ Object
- #render(name, vars = {}) ⇒ Object
- #sort_posts ⇒ Object
- #symbolize_keys(hash) ⇒ Object
- #url ⇒ Object
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
#config ⇒ Object
Returns the value of attribute config.
7 8 9 |
# File 'lib/shinmun/blog.rb', line 7 def config @config end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
8 9 10 |
# File 'lib/shinmun/blog.rb', line 8 def pages @pages end |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
8 9 10 |
# File 'lib/shinmun/blog.rb', line 8 def posts @posts end |
#posts_by_category ⇒ Object (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_date ⇒ Object (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_tag ⇒ Object (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.(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
#archives ⇒ Object
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_pages ⇒ Object
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_posts ⇒ Object
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 () return [] if .nil? or .empty? = .split(',').map { |t| t.strip } if .is_a?(String) .map { |tag| posts_by_tag[tag] }.flatten.uniq end |
#reload_changed_files ⇒ Object
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_posts ⇒ Object
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 |
#url ⇒ Object
100 101 102 |
# File 'lib/shinmun/blog.rb', line 100 def url "http://#{request.host}" end |