class OakTree::Template::Blog

Public Class Methods

modes() click to toggle source
# File lib/oaktree/template/blog.rb, line 23
def self.modes
  @@MODES
end
new(tree, options = {}) click to toggle source
# 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
template_for_mode(mode) click to toggle source
# 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

Public Instance Methods

archive() click to toggle source

the current archive

# File lib/oaktree/template/blog.rb, line 263
def archive
  @archive[@page_index] if archive?
end
archive?() click to toggle source

page tags

# File lib/oaktree/template/blog.rb, line 150
def archive?
  @mode == :archive
end
archive_date() click to toggle source

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.

# File lib/oaktree/template/blog.rb, line 177
def archive_date
  return nil unless archive?
  proc_for_datetime(@archive[@page_index])
end
archives() click to toggle source

archive tags

# File lib/oaktree/template/blog.rb, line 258
def archives
  @archive
end
blog_author() click to toggle source
# File lib/oaktree/template/blog.rb, line 136
def blog_author
  @spec.author
end
blog_description() click to toggle source
# File lib/oaktree/template/blog.rb, line 140
def blog_description
  @spec.description
end
blog_title() click to toggle source
# File lib/oaktree/template/blog.rb, line 132
def blog_title
  @spec.title
end
blog_url() click to toggle source
# File lib/oaktree/template/blog.rb, line 144
def blog_url
  @spec.base_url
end
has_next?() click to toggle source

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

# File lib/oaktree/template/blog.rb, line 227
def has_next?
  1 < self.page
end
has_previous?() click to toggle source

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

# File lib/oaktree/template/blog.rb, line 222
def has_previous?
  self.page < self.pages
end
home?() click to toggle source
# File lib/oaktree/template/blog.rb, line 158
def home?
  @mode == :home
end
local_path() click to toggle source

blog tags

# 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
mode() click to toggle source
# File lib/oaktree/template/blog.rb, line 98
def mode
  @mode
end
mode=(mode) click to toggle source
# 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
modes() click to toggle source

returns an enumerator for all modes supported by the template

# File lib/oaktree/template/blog.rb, line 36
def modes
  self.class.modes
end
next_archive() click to toggle source
# File lib/oaktree/template/blog.rb, line 321
def next_archive
  archive? && has_next? ? @archive[page_index - 1] : nil
end
next_post() click to toggle source

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.

next_url() click to toggle source
# 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
page() click to toggle source

returns the current page number

# File lib/oaktree/template/blog.rb, line 206
def page
  @page_index + 1
end
page=(page_num) click to toggle source
# 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?() click to toggle source
# File lib/oaktree/template/blog.rb, line 217
def paged?
  has_previous? || has_next?
end
pages() click to toggle source

returns the number of pages

# 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
post() click to toggle source

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

# File lib/oaktree/template/blog.rb, line 301
def post
  case mode
  when :single ; @posts[@page_index]
  when :statics ; @statics[@page_index]
  end
end
posts() click to toggle source

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

# 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_archive() click to toggle source
# File lib/oaktree/template/blog.rb, line 325
def previous_archive
  archive? && has_previous? ? @archive[page_index + 1] : nil
end
previous_post() click to toggle source

returns the previous post. only works in single mode.

previous_url() click to toggle source
# 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?() click to toggle source
# File lib/oaktree/template/blog.rb, line 166
def rss_feed?
  @mode == :rss_feed
end
rss_feed_url() click to toggle source
# File lib/oaktree/template/blog.rb, line 170
def rss_feed_url
  "#{@spec.base_url}feeds/rss.xml"
end
single?() click to toggle source
# File lib/oaktree/template/blog.rb, line 154
def single?
  @mode == :single
end
statics() click to toggle source
# File lib/oaktree/template/blog.rb, line 182
def statics
  @statics
end
statics?() click to toggle source
# File lib/oaktree/template/blog.rb, line 162
def statics?
  @mode == :statics
end
today() click to toggle source

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

# File lib/oaktree/template/blog.rb, line 332
def today
  proc_for_datetime(DateTime.now)
end
url_encode() click to toggle source

tag to provide simple URL encoding

# File lib/oaktree/template/blog.rb, line 28
def url_encode
  proc {
    |input|
    render(URI.encode_www_form_component(render(input)))
  }
end