Class: OakTree::Template::Blog

Inherits:
Base
  • Object
show all
Defined in:
lib/oaktree/template/blog.rb

Constant Summary collapse

@@MODES =
[:home, :archive, :single, :statics, :rss_feed].freeze()
@@MODE_TEMPLATES =
{
  :home => 'home'.freeze(),
  :archive => 'archive'.freeze(),
  :single => 'single'.freeze(),
  :statics => 'statics'.freeze(),
  :rss_feed => 'rss_feed'.freeze()
}

Constants inherited from Base

OakTree::Template::Base::DEFAULT_DATETIME_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#proc_for_datetime

Constructor Details

#initialize(tree, options = {}) ⇒ Blog

Returns a new instance of Blog.



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
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/oaktree/template/blog.rb', line 49

def initialize tree, options = {}
  @tree = tree
  @spec = tree.blogspec
  @page_index = 0
  self.mode = options[:mode] || :home
  raise "Invalid mode" unless @@MODES.include? @mode

  @postdata = tree.posts.map { |post|
    Post.new(@spec, post)
  }.select(&:published?)

  @posts = @postdata.select(&:post?)
  @sorted_posts = @posts.sort { |pl, pr| pl.post_data.time <=> pr.post_data.time }.reverse!
  @statics = @postdata.select(&:static?)

  @posts.freeze
  @statics.freeze

  # build the archive
  @archive = []
  @posts.each { |post|
    data = post.post_data
    time = data.time

    arch = @archive.last
    if arch.nil? || arch.year != time.year || arch.month != time.month
      arch = PostArchive.new(time.year, time.month, [], @spec, self) { |a|
        a.next_archive = arch
        arch.previous_archive = a unless arch.nil?
      }
      @archive << arch
    end

    arch.posts << post
  }
  @archive.freeze

  page = options[:page]
  page = page.nil? ? 0 : page - 1
  @page_index = page
end

Class Method Details

.modesObject



23
24
25
# File 'lib/oaktree/template/blog.rb', line 23

def self.modes
  @@MODES
end

.template_for_mode(mode) ⇒ Object



43
44
45
46
47
# File 'lib/oaktree/template/blog.rb', line 43

def self.template_for_mode(mode)
  tmp = @@MODE_TEMPLATES[mode]
  tpath = File.expand_path("#{self.template_path}/#{tmp}.mustache")
  (tmp && File.exists?(tpath)) ? tmp : self.template_name
end

Instance Method Details

#archiveObject

the current archive



263
264
265
# File 'lib/oaktree/template/blog.rb', line 263

def archive
  @archive[@page_index] if archive?
end

#archive?Boolean

page tags

Returns:

  • (Boolean)


150
151
152
# File 'lib/oaktree/template/blog.rb', line 150

def archive?
  @mode == :archive
end

#archive_dateObject

uses the input as a format string for the first day of the month and year of the archive page. this returns nil if not in archive mode.



177
178
179
180
# File 'lib/oaktree/template/blog.rb', line 177

def archive_date
  return nil unless archive?
  proc_for_datetime(@archive[@page_index])
end

#archivesObject

archive tags



258
259
260
# File 'lib/oaktree/template/blog.rb', line 258

def archives
  @archive
end

#blog_authorObject



136
137
138
# File 'lib/oaktree/template/blog.rb', line 136

def blog_author
  @spec.author
end

#blog_descriptionObject



140
141
142
# File 'lib/oaktree/template/blog.rb', line 140

def blog_description
  @spec.description
end

#blog_titleObject



132
133
134
# File 'lib/oaktree/template/blog.rb', line 132

def blog_title
  @spec.title
end

#blog_urlObject



144
145
146
# File 'lib/oaktree/template/blog.rb', line 144

def blog_url
  @spec.base_url
end

#has_next?Boolean

determines whether there is a next page. next also means newer.

Returns:

  • (Boolean)


227
228
229
# File 'lib/oaktree/template/blog.rb', line 227

def has_next?
  1 < self.page
end

#has_previous?Boolean

determines whether there’s a previous page. previous also means older.

Returns:

  • (Boolean)


222
223
224
# File 'lib/oaktree/template/blog.rb', line 222

def has_previous?
  self.page < self.pages
end

#home?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/oaktree/template/blog.rb', line 158

def home?
  @mode == :home
end

#local_pathObject

blog tags



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/oaktree/template/blog.rb', line 104

def local_path
  path = @spec.blog_root + "public/"
  date_format = @spec.date_path_format

  if home? && @page_index == 0
    path << "index.html"
  elsif single? || statics?
    return post.post_data.public_path
  elsif rss_feed?
    path << 'feeds/rss.xml'
  else
    path << @spec.post_path

    case mode
      when :home
        path << "#{@page_index}.html"
      when :archive
        arch = @archive[@page_index]
        archdate = DateTime.new(arch.year, arch.month, 1)
        path << "#{archdate.strftime(date_format)}"
    end

    path << 'index.html' if path.end_with? '/'
  end

  path
end

#modeObject



98
99
100
# File 'lib/oaktree/template/blog.rb', line 98

def mode
  @mode
end

#mode=(mode) ⇒ Object



91
92
93
94
95
96
# File 'lib/oaktree/template/blog.rb', line 91

def mode= mode
  raise "Invalid mode" unless @@MODES.include? mode
  return mode  if @mode == mode
  @mode = mode
  self.template_name = self.class.template_for_mode(mode)
end

#modesObject

returns an enumerator for all modes supported by the template



36
37
38
# File 'lib/oaktree/template/blog.rb', line 36

def modes
  self.class.modes
end

#next_archiveObject



321
322
323
# File 'lib/oaktree/template/blog.rb', line 321

def next_archive
  archive? && has_next? ? @archive[page_index - 1] : nil
end

#next_postObject

returns the next post (you generally shouldn’t use this except to get some small bit of info about the next post). only works in single mode.



311
312
313
# File 'lib/oaktree/template/blog.rb', line 311

def next_post
  single? && has_next? ? @posts[page_index - 1] : nil
end

#next_urlObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/oaktree/template/blog.rb', line 231

def next_url
  return "" unless has_next?

  @next_url_cache ||= case mode
    when :home
      if @page_index == 1
        blog_url
      else
        blog_url + @spec.post_path + "#{@page_index - 1}.html"
      end
    when :archive ; @archive[@page_index - 1].permalink
    when :single ; @posts[@page_index - 1].permalink
  end
end

#pageObject

returns the current page number



206
207
208
# File 'lib/oaktree/template/blog.rb', line 206

def page
  @page_index + 1
end

#page=(page_num) ⇒ Object



210
211
212
213
214
215
# File 'lib/oaktree/template/blog.rb', line 210

def page= page_num
  @next_url_cache = nil
  @prev_url_cache = nil
  @posts_cache = nil
  @page_index = (page_num - 1)
end

#paged?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/oaktree/template/blog.rb', line 217

def paged?
  has_previous? || has_next?
end

#pagesObject

returns the number of pages



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/oaktree/template/blog.rb', line 187

def pages
  case mode
    # you can render multiple home pages, but it's not something I recommend
    # since re-syncing all homepage posts is a nightmare at some point
    when :home
      if @spec.posts_per_page > 0
        (@posts.length / @spec.posts_per_page) + 1
      else
        1
      end

    when :archive ; @archive.length
    when :single ; @posts.length
    when :statics ; @statics.length
    when :rss_feed ; 1
  end
end

#postObject

if there’s only one post being displayed, this returns its template. only works in single mode.



301
302
303
304
305
306
# File 'lib/oaktree/template/blog.rb', line 301

def post
  case mode
  when :single ; @posts[@page_index]
  when :statics ; @statics[@page_index]
  end
end

#postsObject

returns all visible posts note: visible means whatever is in the current page



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/oaktree/template/blog.rb', line 271

def posts
  @posts_cache ||= case mode
    when :home
      if @spec.posts_per_page > 0
        page_start = @page_index * @spec.posts_per_page
        page_end = page_start + @spec.posts_per_page - 1
        if @posts.length < page_end
          page_end = @posts.length
        end

        return [] unless page_start < @posts.length

        @posts[page_start .. page_end]
      else
        @posts[0..-1]
      end

    when :archive ; @archive[@page_index].posts
    when :single ; [@posts[@page_index]]
    when :statics ; [@statics[@page_index]]

    when :rss_feed
      rss_length = @spec.rss_length - 1
      rss_length = -1  if rss_length < -1
      @sorted_posts[0..(rss_length - 1)]
  end
end

#previous_archiveObject



325
326
327
# File 'lib/oaktree/template/blog.rb', line 325

def previous_archive
  archive? && has_previous? ? @archive[page_index + 1] : nil
end

#previous_postObject

returns the previous post. only works in single mode.



317
318
319
# File 'lib/oaktree/template/blog.rb', line 317

def previous_post
  single? && has_previous? ? @posts[page_index + 1] : nil
end

#previous_urlObject



246
247
248
249
250
251
252
253
254
# File 'lib/oaktree/template/blog.rb', line 246

def previous_url
  return "" unless has_previous?

  @prev_url_cache ||= case mode
    when :home ; blog_url + @spec.post_path + "#{@page_index + 1}.html"
    when :archive ; @archive[@page_index + 1].permalink
    when :single ; @posts[@page_index + 1].permalink
  end
end

#rss_feed?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/oaktree/template/blog.rb', line 166

def rss_feed?
  @mode == :rss_feed
end

#rss_feed_urlObject



170
171
172
# File 'lib/oaktree/template/blog.rb', line 170

def rss_feed_url
  "#{@spec.base_url}feeds/rss.xml"
end

#single?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/oaktree/template/blog.rb', line 154

def single?
  @mode == :single
end

#staticsObject



182
183
184
# File 'lib/oaktree/template/blog.rb', line 182

def statics
  @statics
end

#statics?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/oaktree/template/blog.rb', line 162

def statics?
  @mode == :statics
end

#todayObject

treats the input text as a format string for today’s date and time



332
333
334
# File 'lib/oaktree/template/blog.rb', line 332

def today
  proc_for_datetime(DateTime.now)
end

#url_encodeObject

tag to provide simple URL encoding



28
29
30
31
32
33
# File 'lib/oaktree/template/blog.rb', line 28

def url_encode
  proc {
    |input|
    render(URI.encode_www_form_component(render(input)))
  }
end