class Jekyll::Archives::Archive

Constants

ATTRIBUTES_FOR_LIQUID

Attributes for Liquid templates

Attributes

posts[RW]
slug[RW]
type[RW]

Public Class Methods

new(site, title, type, posts) click to toggle source

Initialize a new Archive page

site - The Site object. title - The name of the tag/category or a Hash of the year/month/day in case of date.

e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".

type - The type of archive. Can be one of “year”, “month”, “day”, “category”, or “tag” posts - The array of posts that belong in this archive.

# File lib/jekyll-archives/archive.rb, line 30
def initialize(site, title, type, posts)
  @site   = site
  @posts  = posts
  @type   = type
  @title  = title
  @config = site.config["jekyll-archives"]
  @slug   = slugify_string_title

  # Use ".html" for file extension and url for path
  @ext  = File.extname(relative_path)
  @path = relative_path
  @name = File.basename(relative_path, @ext)

  @data = { "layout" => layout, "title" => title }

  if title.is_a?(Hash) && !date?
    title['_layout'] = title.delete 'layout'
    @data.merge! title
  end

  @content = ""

  data.default_proc = proc do |_, key|
    site.frontmatter_defaults.find(relative_path, type, key)
  end

  # Replace the value with the archive except for date and
  # category-tag.  For category-tag, do we want to replace the
  # category or the tag?
  return unless replace?
  @posts.each do |post|
    case post.data[attribute]
    when Array then post.data[attribute].map!{|v| (v == title) ? self : v }
    else post.data[attribute] = self
    end
  end
end

Public Instance Methods

category() click to toggle source
# File lib/jekyll-archives/archive.rb, line 144
def category
  @title[:category] if @title.is_a? Hash
end
date() click to toggle source

Produce a date object if a date-based archive

Returns a Date.

# File lib/jekyll-archives/archive.rb, line 135
def date
  return unless date?

  @date ||= begin
    args = @title.values.map(&:to_i)
    Date.new(*args)
  end
end
date?() click to toggle source
# File lib/jekyll-archives/archive.rb, line 168
def date?
  %w[year month day].include? @type
end
inspect() click to toggle source

Returns the object as a debug String.

# File lib/jekyll-archives/archive.rb, line 164
def inspect
  "#<Jekyll:Archive @type=#{@type} @title=#{@title} @data=#{@data.inspect}>"
end
layout() click to toggle source

The layout to use for rendering

Returns the layout as a String

# File lib/jekyll-archives/archive.rb, line 81
def layout
  @config.dig("layouts", type) || @config["layout"]
end
relative_path() click to toggle source

Obtain the write path relative to the destination directory

Returns the destination relative path String.

# File lib/jekyll-archives/archive.rb, line 155
def relative_path
  @relative_path ||= begin
    path = URL.unescape_path(url).gsub(%r!^/!, "")
    path = File.join(path, "index.html") if url.end_with?("/")
    path
  end
end
tag() click to toggle source
# File lib/jekyll-archives/archive.rb, line 148
def tag
  @title[:tag] if @title.is_a? Hash
end
template() click to toggle source

The template of the permalink.

Returns the template String.

# File lib/jekyll-archives/archive.rb, line 71
def template
  t = @config.dig("permalinks", type)
  t = t.is_a?(Hash) ? t[@title] : t

  t || "/#{type}/:name/"
end
title() click to toggle source

Produce a title object suitable for Liquid based on type of archive.

Returns a String (for tag and category archives) and nil for date-based archives.

# File lib/jekyll-archives/archive.rb, line 124
def title
  if @title.is_a? String
    @config.dig('titles', @title) || @title
  elsif !date?
    @title.values.join(@config.fetch('separator', ' / '))
  end
end
url() click to toggle source

The generated relative url of this page. e.g. /about.html.

Returns the String url.

# File lib/jekyll-archives/archive.rb, line 106
def url
  @url ||= URL.new(
    :template     => template,
    :placeholders => url_placeholders,
    :permalink    => nil
  ).to_s
rescue ArgumentError
  raise ArgumentError, "Template \"#{template}\" provided is invalid."
end
url_placeholders() click to toggle source

Returns a hash of URL placeholder names (as symbols) mapping to the desired placeholder replacements. For details see “url.rb”.

# File lib/jekyll-archives/archive.rb, line 87
def url_placeholders
  if @title.is_a? Hash
    placeholders = @title.merge(:type => @type)

    unless date?
      placeholders.transform_values! do |v|
        Utils.slugify(v, mode: @config.dig('slug'))
      end
    end

    placeholders
  else
    { :name => @slug, :type => @type }
  end
end

Private Instance Methods

attribute() click to toggle source
# File lib/jekyll-archives/archive.rb, line 179
def attribute
  case type
  when 'tag' then 'tags'
  when 'category' then 'categories'
  else type
  end
end
replace?() click to toggle source

Layout is a special attribute that can't be replaced

# File lib/jekyll-archives/archive.rb, line 175
def replace?
  @config['replace'] && !@type.is_a?(Hash) && attribute != 'layout'
end
slugify_string_title() click to toggle source

Generate slug if @title attribute is a string.

Note: mode other than those expected by Jekyll returns the given string after downcasing it.

# File lib/jekyll-archives/archive.rb, line 191
def slugify_string_title
  return unless title.is_a?(String)

  mode = @config["slug_mode"] || @config["slug"]

  Utils.slugify(title, :mode => mode)
end