|
1 | | -module Jekyll |
2 | | - module Archives |
3 | | - class Archive |
4 | | - include Convertible |
5 | | - |
6 | | - attr_accessor :posts, :type, :name, :slug |
7 | | - attr_accessor :data, :content, :output |
8 | | - attr_accessor :path, :ext |
9 | | - attr_accessor :site |
10 | | - |
11 | | - # Attributes for Liquid templates |
12 | | - ATTRIBUTES_FOR_LIQUID = %w[ |
13 | | - posts |
14 | | - type |
15 | | - title |
16 | | - date |
17 | | - name |
18 | | - path |
19 | | - url |
20 | | - ] |
21 | | - |
22 | | - # Initialize a new Archive page |
23 | | - # |
24 | | - # site - The Site object. |
25 | | - # title - The name of the tag/category or a Hash of the year/month/day in case of date. |
26 | | - # e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag". |
27 | | - # type - The type of archive. Can be one of "year", "month", "day", "category", or "tag" |
28 | | - # posts - The array of posts that belong in this archive. |
29 | | - def initialize(site, title, type, posts) |
30 | | - @site = site |
31 | | - @posts = posts |
32 | | - @type = type |
33 | | - @title = title |
34 | | - @config = site.config['jekyll-archives'] |
35 | | - |
36 | | - # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb) |
37 | | - if title.to_s.length |
38 | | - @slug = Utils.slugify(title.to_s) |
39 | | - end |
40 | | - |
41 | | - # Use ".html" for file extension and url for path |
42 | | - @ext = File.extname(relative_path) |
43 | | - @path = relative_path |
44 | | - @name = File.basename(relative_path, @ext) |
45 | | - |
46 | | - @data = { |
47 | | - "layout" => layout |
48 | | - } |
49 | | - @content = "" |
50 | | - |
51 | | - # set up dependencies |
52 | | - posts.each do |post| |
53 | | - site.regenerator.add_dependency(self.path, post.path) |
54 | | - end |
55 | | - end |
56 | | - |
57 | | - # The template of the permalink. |
58 | | - # |
59 | | - # Returns the template String. |
60 | | - def template |
61 | | - @config['permalinks'][type] |
62 | | - end |
63 | | - |
64 | | - # The layout to use for rendering |
65 | | - # |
66 | | - # Returns the layout as a String |
67 | | - def layout |
68 | | - if @config['layouts'] && @config['layouts'][type] |
69 | | - @config['layouts'][type] |
70 | | - else |
71 | | - @config['layout'] |
72 | | - end |
73 | | - end |
74 | | - |
75 | | - # Returns a hash of URL placeholder names (as symbols) mapping to the |
76 | | - # desired placeholder replacements. For details see "url.rb". |
77 | | - def url_placeholders |
78 | | - if @title.is_a? Hash |
79 | | - @title.merge({ :type => @type }) |
80 | | - else |
81 | | - { :name => @slug, :type => @type } |
82 | | - end |
83 | | - end |
84 | | - |
85 | | - # The generated relative url of this page. e.g. /about.html. |
86 | | - # |
87 | | - # Returns the String url. |
88 | | - def url |
89 | | - @url ||= URL.new({ |
90 | | - :template => template, |
91 | | - :placeholders => url_placeholders, |
92 | | - :permalink => nil |
93 | | - }).to_s |
94 | | - rescue ArgumentError |
95 | | - raise ArgumentError.new "Template \"#{template}\" provided is invalid." |
96 | | - end |
97 | | - |
98 | | - # Add any necessary layouts to this post |
99 | | - # |
100 | | - # layouts - The Hash of {"name" => "layout"}. |
101 | | - # site_payload - The site payload Hash. |
102 | | - # |
103 | | - # Returns nothing. |
104 | | - def render(layouts, site_payload) |
105 | | - payload = Utils.deep_merge_hashes({ |
106 | | - "page" => to_liquid |
107 | | - }, site_payload) |
108 | | - |
109 | | - do_layout(payload, layouts) |
110 | | - end |
111 | | - |
112 | | - # Convert this Convertible's data to a Hash suitable for use by Liquid. |
113 | | - # |
114 | | - # Returns the Hash representation of this Convertible. |
115 | | - def to_liquid(attrs = nil) |
116 | | - further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute| |
117 | | - [attribute, send(attribute)] |
118 | | - }] |
119 | | - |
120 | | - Utils.deep_merge_hashes(data, further_data) |
121 | | - end |
122 | | - |
123 | | - # Produce a title object suitable for Liquid based on type of archive. |
124 | | - # |
125 | | - # Returns a String (for tag and category archives) and nil for |
126 | | - # date-based archives. |
127 | | - def title |
128 | | - if @title.is_a? String |
129 | | - @title |
130 | | - end |
131 | | - end |
132 | | - |
133 | | - # Produce a date object if a date-based archive |
134 | | - # |
135 | | - # Returns a Date. |
136 | | - def date |
137 | | - if @title.is_a? Hash |
138 | | - args = @title.values.map { |s| s.to_i } |
139 | | - Date.new(*args) |
140 | | - end |
141 | | - end |
142 | | - |
143 | | - # Obtain destination path. |
144 | | - # |
145 | | - # dest - The String path to the destination dir. |
146 | | - # |
147 | | - # Returns the destination file path String. |
148 | | - def destination(dest) |
149 | | - path = Jekyll.sanitized_path(dest, URL.unescape_path(url)) |
150 | | - path = File.join(path, "index.html") if url =~ /\/$/ |
151 | | - path |
152 | | - end |
153 | | - |
154 | | - # Obtain the write path relative to the destination directory |
155 | | - # |
156 | | - # Returns the destination relative path String. |
157 | | - def relative_path |
158 | | - path = URL.unescape_path(url).gsub(/^\//, '') |
159 | | - path = File.join(path, "index.html") if url =~ /\/$/ |
160 | | - path |
161 | | - end |
162 | | - |
163 | | - # Returns the object as a debug String. |
164 | | - def inspect |
165 | | - "#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>" |
166 | | - end |
167 | | - |
168 | | - # Returns the Boolean of whether this Page is HTML or not. |
169 | | - def html? |
170 | | - true |
171 | | - end |
172 | | - end |
173 | | - end |
174 | | -end |
| 1 | +--- archive.rb |
| 2 | ++++ (clipboard) |
| 3 | +@@ -17,7 +17,6 @@ |
| 4 | + name |
| 5 | + path |
| 6 | + url |
| 7 | +- permalink |
| 8 | + ] |
| 9 | + |
| 10 | + # Initialize a new Archive page |
| 11 | +@@ -35,19 +34,24 @@ |
| 12 | + @config = site.config['jekyll-archives'] |
| 13 | + |
| 14 | + # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb) |
| 15 | +- if title.is_a? String |
| 16 | +- @slug = Utils.slugify(title) |
| 17 | ++ if title.to_s.length |
| 18 | ++ @slug = Utils.slugify(title.to_s) |
| 19 | + end |
| 20 | + |
| 21 | + # Use ".html" for file extension and url for path |
| 22 | + @ext = File.extname(relative_path) |
| 23 | +- @path = site.in_dest_dir(relative_path) |
| 24 | ++ @path = relative_path |
| 25 | + @name = File.basename(relative_path, @ext) |
| 26 | + |
| 27 | + @data = { |
| 28 | + "layout" => layout |
| 29 | + } |
| 30 | + @content = "" |
| 31 | ++ |
| 32 | ++ # set up dependencies |
| 33 | ++ posts.each do |post| |
| 34 | ++ site.regenerator.add_dependency(self.path, post.path) |
| 35 | ++ end |
| 36 | + end |
| 37 | + |
| 38 | + # The template of the permalink. |
| 39 | +@@ -91,10 +95,6 @@ |
| 40 | + raise ArgumentError.new "Template \"#{template}\" provided is invalid." |
| 41 | + end |
| 42 | + |
| 43 | +- def permalink |
| 44 | +- data && data.is_a?(Hash) && data['permalink'] |
| 45 | +- end |
| 46 | +- |
| 47 | + # Add any necessary layouts to this post |
| 48 | + # |
| 49 | + # layouts - The Hash of {"name" => "layout"}. |
| 50 | +@@ -109,15 +109,6 @@ |
| 51 | + do_layout(payload, layouts) |
| 52 | + end |
| 53 | + |
| 54 | +- # Add dependencies for incremental mode |
| 55 | +- def add_dependencies |
| 56 | +- archive_path = site.in_dest_dir(relative_path) |
| 57 | +- site.regenerator.add(archive_path) |
| 58 | +- @posts.each do |post| |
| 59 | +- site.regenerator.add_dependency(archive_path, post.path) |
| 60 | +- end |
| 61 | +- end |
| 62 | +- |
| 63 | + # Convert this Convertible's data to a Hash suitable for use by Liquid. |
| 64 | + # |
| 65 | + # Returns the Hash representation of this Convertible. |
| 66 | +@@ -169,10 +160,6 @@ |
| 67 | + path |
| 68 | + end |
| 69 | + |
| 70 | +- def regenerate? |
| 71 | +- site.regenerator.regenerate?(self) |
| 72 | +- end |
| 73 | +- |
| 74 | + # Returns the object as a debug String. |
| 75 | + def inspect |
| 76 | + "#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>" |
0 commit comments