Class: Jekyll::Archive

Inherits:
Object
  • Object
show all
Includes:
Convertible
Defined in:
lib/jekyll-archives/archive.rb

Constant Summary collapse

ATTRIBUTES_FOR_LIQUID =

Attributes for Liquid templates

%w[
  posts
  type
  name
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, posts, type, name) ⇒ Archive

Initialize a new Archive page

site - The Site object. posts - The array of posts that belong in this archive. type - The type of archive. Can be one of “year”, “category”, or “tag” name - The name of the archive (e.g. “2014” or “my-category” or “my-tag”).



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll-archives/archive.rb', line 23

def initialize(site, posts, type, name)
  @site  = site
  @posts = posts
  @type  = type
  @name  = name

  # Use ".html" for file extension and url for path
  @ext  = ".html"
  @path = url

  @data = {
    "layout" => site.config['jekyll-archives']['layout']
  }
  @content = ""
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/jekyll-archives/archive.rb', line 6

def content
  @content
end

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/jekyll-archives/archive.rb', line 6

def data
  @data
end

#extObject

Returns the value of attribute ext.



7
8
9
# File 'lib/jekyll-archives/archive.rb', line 7

def ext
  @ext
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/jekyll-archives/archive.rb', line 5

def name
  @name
end

#outputObject

Returns the value of attribute output.



6
7
8
# File 'lib/jekyll-archives/archive.rb', line 6

def output
  @output
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/jekyll-archives/archive.rb', line 7

def path
  @path
end

#postsObject

Returns the value of attribute posts.



5
6
7
# File 'lib/jekyll-archives/archive.rb', line 5

def posts
  @posts
end

#siteObject

Returns the value of attribute site.



8
9
10
# File 'lib/jekyll-archives/archive.rb', line 8

def site
  @site
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/jekyll-archives/archive.rb', line 5

def type
  @type
end

Instance Method Details

#destination(dest) ⇒ Object

Obtain destination path.

dest - The String path to the destination dir.

Returns the destination file path String.



95
96
97
98
99
# File 'lib/jekyll-archives/archive.rb', line 95

def destination(dest)
  path = Jekyll.sanitized_path(dest, URL.unescape_path(url))
  path = File.join(path, "index.html") if url =~ /\/$/
  path
end

#html?Boolean

Returns the Boolean of whether this Page is HTML or not.

Returns:

  • (Boolean)


107
108
109
# File 'lib/jekyll-archives/archive.rb', line 107

def html?
  true
end

#inspectObject

Returns the object as a debug String.



102
103
104
# File 'lib/jekyll-archives/archive.rb', line 102

def inspect
  "#<Jekyll:Archive @type=#{@type.to_s} @name=#{@name}>"
end

#render(layouts, site_payload) ⇒ Object

Add any necessary layouts to this post

layouts - The Hash of => “layout”. site_payload - The site payload Hash.

Returns nothing.



71
72
73
74
75
76
77
# File 'lib/jekyll-archives/archive.rb', line 71

def render(layouts, site_payload)
  payload = Utils.deep_merge_hashes({
    "page" => to_liquid
  }, site_payload)

  do_layout(payload, layouts)
end

#templateObject

The template of the permalink.

Returns the template String.



42
43
44
# File 'lib/jekyll-archives/archive.rb', line 42

def template
  site.config['jekyll-archives']['permalinks'][type]
end

#to_liquid(attrs = nil) ⇒ Object

Convert this Convertible’s data to a Hash suitable for use by Liquid.

Returns the Hash representation of this Convertible.



82
83
84
85
86
87
88
# File 'lib/jekyll-archives/archive.rb', line 82

def to_liquid(attrs = nil)
  further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
    [attribute, send(attribute)]
  }]

  Utils.deep_merge_hashes(data, further_data)
end

#urlObject

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

Returns the String url.



55
56
57
58
59
60
61
62
63
# File 'lib/jekyll-archives/archive.rb', line 55

def url
  @url ||= URL.new({
    :template => template,
    :placeholders => url_placeholders,
    :permalink => nil
  }).to_s
rescue ArgumentError
  raise ArgumentError.new "Template \"#{template}\" provided is invalid."
end

#url_placeholdersObject

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



48
49
50
# File 'lib/jekyll-archives/archive.rb', line 48

def url_placeholders
  { :name => @name, :type => @type.to_s }
end