Class: Jekyll::Archives::Archive
- Inherits:
-
Page
- Object
- Page
- Jekyll::Archives::Archive
- Defined in:
- lib/jekyll-archives/archive.rb
Constant Summary collapse
- ATTRIBUTES_FOR_LIQUID =
Attributes for Liquid templates
%w( posts type title date name path url permalink slug category tag ).freeze
Instance Attribute Summary collapse
-
#posts ⇒ Object
Returns the value of attribute posts.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #category ⇒ Object
-
#date ⇒ Object
Produce a date object if a date-based archive.
- #date? ⇒ Boolean
-
#initialize(site, title, type, posts) ⇒ Archive
constructor
Initialize a new Archive page.
-
#inspect ⇒ Object
Returns the object as a debug String.
-
#layout ⇒ Object
The layout to use for rendering.
- #permalink ⇒ Object
-
#relative_path ⇒ Object
Obtain the write path relative to the destination directory.
- #tag ⇒ Object
-
#template ⇒ Object
The template of the permalink.
-
#title ⇒ Object
Produce a title object suitable for Liquid based on type of archive.
-
#url ⇒ Object
The generated relative url of this page.
-
#url_placeholders ⇒ Object
Returns a hash of URL placeholder names (as symbols) mapping to the desired placeholder replacements.
Constructor Details
#initialize(site, title, type, posts) ⇒ Archive
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.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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 } 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 |
Instance Attribute Details
#posts ⇒ Object
Returns the value of attribute posts.
6 7 8 |
# File 'lib/jekyll-archives/archive.rb', line 6 def posts @posts end |
#slug ⇒ Object
Returns the value of attribute slug.
6 7 8 |
# File 'lib/jekyll-archives/archive.rb', line 6 def slug @slug end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/jekyll-archives/archive.rb', line 6 def type @type end |
Instance Method Details
#category ⇒ Object
144 145 146 |
# File 'lib/jekyll-archives/archive.rb', line 144 def category @title[:category] if @title.is_a? Hash end |
#date ⇒ Object
Produce a date object if a date-based archive
Returns a Date.
135 136 137 138 139 140 141 142 |
# 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? ⇒ Boolean
168 169 170 |
# File 'lib/jekyll-archives/archive.rb', line 168 def date? %w[year month day].include? @type end |
#inspect ⇒ Object
Returns the object as a debug String.
164 165 166 |
# File 'lib/jekyll-archives/archive.rb', line 164 def inspect "#<Jekyll:Archive @type=#{@type} @title=#{@title} @data=#{@data.inspect}>" end |
#layout ⇒ Object
The layout to use for rendering
Returns the layout as a String
81 82 83 |
# File 'lib/jekyll-archives/archive.rb', line 81 def layout @config.dig("layouts", type) || @config["layout"] end |
#permalink ⇒ Object
116 117 118 |
# File 'lib/jekyll-archives/archive.rb', line 116 def permalink data&.is_a?(Hash) && data["permalink"] end |
#relative_path ⇒ Object
Obtain the write path relative to the destination directory
Returns the destination relative path String.
155 156 157 158 159 160 161 |
# 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 ⇒ Object
148 149 150 |
# File 'lib/jekyll-archives/archive.rb', line 148 def tag @title[:tag] if @title.is_a? Hash end |
#template ⇒ Object
The template of the permalink.
Returns the template String.
71 72 73 74 75 76 |
# 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 ⇒ Object
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.
124 125 126 127 128 129 130 |
# 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 ⇒ Object
The generated relative url of this page. e.g. /about.html.
Returns the String url.
106 107 108 109 110 111 112 113 114 |
# 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 ⇒ Object
Returns a hash of URL placeholder names (as symbols) mapping to the desired placeholder replacements. For details see “url.rb”.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# 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 |