jekyll-archives 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e5597290843350c0f349a0625e966c5b253d914
4
- data.tar.gz: 3b2f8221b82d532d32f2dc038554176d65a380a5
3
+ metadata.gz: 2b678cbc857435a0606f6903249ffafa2d0bc4c6
4
+ data.tar.gz: c31fe905416a36b9809252359eae9c6374552f6d
5
5
  SHA512:
6
- metadata.gz: 17b68ddb19f27ce71fa0bc4fd41cd5da92cbc51f11e74bfdb77b30b63629cd307f0312c4f6740aea94da38d1893e4a7122d4e4966dc1ce99235922baedb62b0f
7
- data.tar.gz: e9c28eb8ac183bee610d471e1b304ba6d2aec20c90654b1df7be20b3fff9948cda26941b87f70ca6f4e2eace331b189ab49306a2aaf620b317dcf87588b2cc1a
6
+ metadata.gz: b9e5dff177b866ea05d3195a35b46410882167453683e3275de7b368bec26e6bfec8e11528b088448c02a5e18d0587baadfeac95bf817546154afe0bd924b6ad
7
+ data.tar.gz: 9fc71856cd97ceb5186d05f661bd8bfe9ff4802499aec4ae6b7665cfd9ddbf766b6bf594c48d9a07d2747c3c8f3fd72f65cb38b758fc1e654367bcdcdb82b5f8
@@ -1,83 +1,162 @@
1
+ require 'jekyll'
2
+
1
3
  module Jekyll
2
- # Internal requires
3
- autoload :Archive, 'jekyll-archives/archive'
4
-
5
- class JekyllArchives < Jekyll::Generator
6
- safe true
7
-
8
- DEFAULTS = {
9
- 'layout' => 'archive',
10
- 'permalinks' => {
11
- 'year' => '/archive/:name/',
12
- 'tag' => '/tag/:name/',
13
- 'category' => '/category/:name/'
4
+ module Archives
5
+ # Internal requires
6
+ autoload :Archive, 'jekyll-archives/archive'
7
+ autoload :VERSION, 'jekyll-archives/version'
8
+
9
+ class Archives < Jekyll::Generator
10
+ safe true
11
+
12
+ DEFAULTS = {
13
+ 'layout' => 'archive',
14
+ 'enabled' => [],
15
+ 'permalinks' => {
16
+ 'year' => '/:year/',
17
+ 'month' => '/:year/:month/',
18
+ 'day' => '/:year/:month/:day/',
19
+ 'tag' => '/tag/:name/',
20
+ 'category' => '/category/:name/'
21
+ }
14
22
  }
15
- }
16
23
 
17
- def initialize(config = nil)
18
- @config = Utils.deep_merge_hashes(DEFAULTS, config['jekyll-archives'])
19
- end
24
+ def initialize(config = nil)
25
+ if config['jekyll-archives'].nil?
26
+ @config = DEFAULTS
27
+ else
28
+ @config = Utils.deep_merge_hashes(DEFAULTS, config['jekyll-archives'])
29
+ end
30
+ end
20
31
 
21
- def generate(site)
22
- @site = site
23
- @posts = site.posts
24
- @archives = []
32
+ def generate(site)
33
+ @site = site
34
+ @posts = site.posts
35
+ @archives = []
25
36
 
26
- @site.config['jekyll-archives'] = @config
37
+ @site.config['jekyll-archives'] = @config
27
38
 
28
- read_archives
29
- @site.pages += @archives
30
- end
39
+ read
40
+ render
41
+ write
31
42
 
32
- # Read archive data from posts
33
- def read_archives
34
- tags.each do |tag|
35
- @archives << Archive.new(@site, tag[1], "tag", tag[0])
43
+ @site.keep_files ||= []
44
+ @archives.each do |archive|
45
+ @site.keep_files << archive.relative_path
46
+ end
47
+ @site.config["archives"] = @archives
36
48
  end
37
- categories.each do |category|
38
- @archives << Archive.new(@site, category[1], "category", category[0])
49
+
50
+ # Read archive data from posts
51
+ def read
52
+ read_tags
53
+ read_categories
54
+ read_dates
39
55
  end
40
- years.each do |year|
41
- @archives << Archive.new(@site, year[1], "year", year[0])
56
+
57
+ def read_tags
58
+ if enabled? "tags"
59
+ tags.each do |title, posts|
60
+ @archives << Archive.new(@site, title, "tag", posts)
61
+ end
62
+ end
42
63
  end
43
- end
44
64
 
45
- # Construct a Hash of Posts indexed by the specified Post attribute.
46
- #
47
- # post_attr - The String name of the Post attribute.
48
- #
49
- # Examples
50
- #
51
- # post_attr_hash('categories')
52
- # # => { 'tech' => [<Post A>, <Post B>],
53
- # # 'ruby' => [<Post B>] }
54
- #
55
- # Returns the Hash: { attr => posts } where
56
- # attr - One of the values for the requested attribute.
57
- # posts - The Array of Posts with the given attr value.
58
- def post_attr_hash(post_attr)
59
- # Build a hash map based on the specified post attribute ( post attr =>
60
- # array of posts ) then sort each array in reverse order.
61
- hash = Hash.new { |h, key| h[key] = [] }
62
- @posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
63
- hash.values.each { |posts| posts.sort!.reverse! }
64
- hash
65
- end
65
+ def read_categories
66
+ if enabled? "categories"
67
+ categories.each do |title, posts|
68
+ @archives << Archive.new(@site, title, "category", posts)
69
+ end
70
+ end
71
+ end
66
72
 
67
- def tags
68
- post_attr_hash('tags')
69
- end
73
+ def read_dates
74
+ years.each do |year, posts|
75
+ @archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
76
+ months(posts).each do |month, posts|
77
+ @archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
78
+ days(posts).each do |day, posts|
79
+ @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
80
+ end
81
+ end
82
+ end
83
+ end
70
84
 
71
- def categories
72
- post_attr_hash('categories')
73
- end
85
+ # Checks if archive type is enabled in config
86
+ def enabled?(archive)
87
+ @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
88
+ @config["enabled"].include? archive
89
+ end
90
+ end
74
91
 
75
- # Custom `post_attr_hash` method for years
76
- def years
77
- hash = Hash.new { |h, key| h[key] = [] }
78
- @posts.each { |p| hash[p.date.year.to_s] << p }
79
- hash.values.each { |posts| posts.sort!.reverse! }
80
- hash
92
+ # Renders the archives into the layouts
93
+ def render
94
+ payload = @site.site_payload
95
+ @archives.each do |archive|
96
+ archive.render(@site.layouts, payload)
97
+ end
98
+ end
99
+
100
+ # Write archives to their destination
101
+ def write
102
+ @archives.each do |archive|
103
+ archive.write(@site.dest)
104
+ end
105
+ end
106
+
107
+ # Construct a Hash of Posts indexed by the specified Post attribute.
108
+ #
109
+ # post_attr - The String name of the Post attribute.
110
+ #
111
+ # Examples
112
+ #
113
+ # post_attr_hash('categories')
114
+ # # => { 'tech' => [<Post A>, <Post B>],
115
+ # # 'ruby' => [<Post B>] }
116
+ #
117
+ # Returns the Hash: { attr => posts } where
118
+ # attr - One of the values for the requested attribute.
119
+ # posts - The Array of Posts with the given attr value.
120
+ #
121
+ # Taken from jekyll/jekyll (Copyright (c) 2014 Tom Preston-Werner under the MIT).
122
+ def post_attr_hash(post_attr)
123
+ # Build a hash map based on the specified post attribute ( post attr =>
124
+ # array of posts ) then sort each array in reverse order.
125
+ hash = Hash.new { |h, key| h[key] = [] }
126
+ @posts.each { |p| p.send(post_attr.to_sym).each { |t| hash[t] << p } }
127
+ hash.values.each { |posts| posts.sort!.reverse! }
128
+ hash
129
+ end
130
+
131
+ def tags
132
+ post_attr_hash('tags')
133
+ end
134
+
135
+ def categories
136
+ post_attr_hash('categories')
137
+ end
138
+
139
+ # Custom `post_attr_hash` method for years
140
+ def years
141
+ hash = Hash.new { |h, key| h[key] = [] }
142
+ @posts.each { |p| hash[p.date.strftime("%Y")] << p }
143
+ hash.values.each { |posts| posts.sort!.reverse! }
144
+ hash
145
+ end
146
+
147
+ def months(year_posts)
148
+ hash = Hash.new { |h, key| h[key] = [] }
149
+ year_posts.each { |p| hash[p.date.strftime("%m")] << p }
150
+ hash.values.each { |posts| posts.sort!.reverse! }
151
+ hash
152
+ end
153
+
154
+ def days(month_posts)
155
+ hash = Hash.new { |h, key| h[key] = [] }
156
+ month_posts.each { |p| hash[p.date.strftime("%d")] << p }
157
+ hash.values.each { |posts| posts.sort!.reverse! }
158
+ hash
159
+ end
81
160
  end
82
161
  end
83
162
  end
@@ -1,111 +1,161 @@
1
1
  module Jekyll
2
- class Archive
3
- include Convertible
4
-
5
- attr_accessor :posts, :type, :name
6
- attr_accessor :data, :content, :output
7
- attr_accessor :path, :ext
8
- attr_accessor :site
9
-
10
- # Attributes for Liquid templates
11
- ATTRIBUTES_FOR_LIQUID = %w[
12
- posts
13
- type
14
- name
15
- ]
16
-
17
- # Initialize a new Archive page
18
- #
19
- # site - The Site object.
20
- # posts - The array of posts that belong in this archive.
21
- # type - The type of archive. Can be one of "year", "category", or "tag"
22
- # name - The name of the archive (e.g. "2014" or "my-category" or "my-tag").
23
- def initialize(site, posts, type, name)
24
- @site = site
25
- @posts = posts
26
- @type = type
27
- @name = name
28
-
29
- # Use ".html" for file extension and url for path
30
- @ext = ".html"
31
- @path = url
32
-
33
- @data = {
34
- "layout" => site.config['jekyll-archives']['layout']
35
- }
36
- @content = ""
37
- end
2
+ module Archives
3
+ class Archive
4
+ include Convertible
38
5
 
39
- # The template of the permalink.
40
- #
41
- # Returns the template String.
42
- def template
43
- site.config['jekyll-archives']['permalinks'][type]
44
- end
6
+ attr_accessor :posts, :type, :name, :slug
7
+ attr_accessor :data, :content, :output
8
+ attr_accessor :path, :ext
9
+ attr_accessor :site
45
10
 
46
- # Returns a hash of URL placeholder names (as symbols) mapping to the
47
- # desired placeholder replacements. For details see "url.rb".
48
- def url_placeholders
49
- { :name => @name, :type => @type.to_s }
50
- end
11
+ # Attributes for Liquid templates
12
+ ATTRIBUTES_FOR_LIQUID = %w[
13
+ posts
14
+ type
15
+ title
16
+ name
17
+ path
18
+ url
19
+ ]
51
20
 
52
- # The generated relative url of this page. e.g. /about.html.
53
- #
54
- # Returns the String url.
55
- def url
56
- @url ||= URL.new({
57
- :template => template,
58
- :placeholders => url_placeholders,
59
- :permalink => nil
60
- }).to_s
61
- rescue ArgumentError
62
- raise ArgumentError.new "Template \"#{template}\" provided is invalid."
63
- end
21
+ # Initialize a new Archive page
22
+ #
23
+ # site - The Site object.
24
+ # title - The name of the tag/category or a Hash of the year/month/day in case of date.
25
+ # e.g. { :year => 2014, :month => 08 } or "my-category" or "my-tag".
26
+ # type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
27
+ # posts - The array of posts that belong in this archive.
28
+ def initialize(site, title, type, posts)
29
+ @site = site
30
+ @posts = posts
31
+ @type = type
32
+ @title = title
33
+ @config = site.config['jekyll-archives']
64
34
 
65
- # Add any necessary layouts to this post
66
- #
67
- # layouts - The Hash of {"name" => "layout"}.
68
- # site_payload - The site payload Hash.
69
- #
70
- # Returns nothing.
71
- def render(layouts, site_payload)
72
- payload = Utils.deep_merge_hashes({
73
- "page" => to_liquid
74
- }, site_payload)
75
-
76
- do_layout(payload, layouts)
77
- end
35
+ # Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
36
+ if title.is_a? String
37
+ @slug = Utils.slugify(title)
38
+ end
78
39
 
79
- # Convert this Convertible's data to a Hash suitable for use by Liquid.
80
- #
81
- # Returns the Hash representation of this Convertible.
82
- def to_liquid(attrs = nil)
83
- further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
84
- [attribute, send(attribute)]
85
- }]
40
+ # Use ".html" for file extension and url for path
41
+ @ext = File.extname(relative_path)
42
+ @path = relative_path
43
+ @name = File.basename(relative_path, @ext)
86
44
 
87
- Utils.deep_merge_hashes(data, further_data)
88
- end
45
+ @data = {
46
+ "layout" => layout
47
+ }
48
+ @content = ""
49
+ end
89
50
 
90
- # Obtain destination path.
91
- #
92
- # dest - The String path to the destination dir.
93
- #
94
- # Returns the destination file path String.
95
- def destination(dest)
96
- path = Jekyll.sanitized_path(dest, URL.unescape_path(url))
97
- path = File.join(path, "index.html") if url =~ /\/$/
98
- path
99
- end
51
+ # The template of the permalink.
52
+ #
53
+ # Returns the template String.
54
+ def template
55
+ @config['permalinks'][type]
56
+ end
100
57
 
101
- # Returns the object as a debug String.
102
- def inspect
103
- "#<Jekyll:Archive @type=#{@type.to_s} @name=#{@name}>"
104
- end
58
+ # The layout to use for rendering
59
+ #
60
+ # Returns the layout as a String
61
+ def layout
62
+ if @config['layouts'] && @config['layouts'][type]
63
+ @config['layouts'][type]
64
+ else
65
+ @config['layout']
66
+ end
67
+ end
68
+
69
+ # Returns a hash of URL placeholder names (as symbols) mapping to the
70
+ # desired placeholder replacements. For details see "url.rb".
71
+ def url_placeholders
72
+ if @title.is_a? Hash
73
+ @title.merge({ :type => @type })
74
+ else
75
+ { :name => @slug, :type => @type }
76
+ end
77
+ end
78
+
79
+ # The generated relative url of this page. e.g. /about.html.
80
+ #
81
+ # Returns the String url.
82
+ def url
83
+ @url ||= URL.new({
84
+ :template => template,
85
+ :placeholders => url_placeholders,
86
+ :permalink => nil
87
+ }).to_s
88
+ rescue ArgumentError
89
+ raise ArgumentError.new "Template \"#{template}\" provided is invalid."
90
+ end
91
+
92
+ # Add any necessary layouts to this post
93
+ #
94
+ # layouts - The Hash of {"name" => "layout"}.
95
+ # site_payload - The site payload Hash.
96
+ #
97
+ # Returns nothing.
98
+ def render(layouts, site_payload)
99
+ payload = Utils.deep_merge_hashes({
100
+ "page" => to_liquid
101
+ }, site_payload)
102
+
103
+ do_layout(payload, layouts)
104
+ end
105
+
106
+ # Convert this Convertible's data to a Hash suitable for use by Liquid.
107
+ #
108
+ # Returns the Hash representation of this Convertible.
109
+ def to_liquid(attrs = nil)
110
+ further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
111
+ [attribute, send(attribute)]
112
+ }]
113
+
114
+ Utils.deep_merge_hashes(data, further_data)
115
+ end
116
+
117
+ # Produce a title object suitable for Liquid based on type of archive.
118
+ #
119
+ # Returns the title as a Date (for date-based archives) or a
120
+ # String (for tag and category archives)
121
+ def title
122
+ if @title.is_a? Hash
123
+ args = @title.values.map { |s| s.to_i }
124
+ Date.new(*args)
125
+ else
126
+ @title
127
+ end
128
+ end
129
+
130
+ # Obtain destination path.
131
+ #
132
+ # dest - The String path to the destination dir.
133
+ #
134
+ # Returns the destination file path String.
135
+ def destination(dest)
136
+ path = Jekyll.sanitized_path(dest, URL.unescape_path(url))
137
+ path = File.join(path, "index.html") if url =~ /\/$/
138
+ path
139
+ end
140
+
141
+ # Obtain the write path relative to the destination directory
142
+ #
143
+ # Returns the destination relative path String.
144
+ def relative_path
145
+ path = URL.unescape_path(url).gsub(/^\//, '')
146
+ path = File.join(path, "index.html") if url =~ /\/$/
147
+ path
148
+ end
149
+
150
+ # Returns the object as a debug String.
151
+ def inspect
152
+ "#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
153
+ end
105
154
 
106
- # Returns the Boolean of whether this Page is HTML or not.
107
- def html?
108
- true
155
+ # Returns the Boolean of whether this Page is HTML or not.
156
+ def html?
157
+ true
158
+ end
109
159
  end
110
160
  end
111
161
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-archives
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alfred Xing
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-17 00:00:00.000000000 Z
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -24,15 +24,71 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
27
83
  description: Automatically generate post archives by dates, tags, and categories.
28
- email: support@github.com
84
+ email:
29
85
  executables: []
30
86
  extensions: []
31
87
  extra_rdoc_files: []
32
88
  files:
33
89
  - lib/jekyll-archives.rb
34
90
  - lib/jekyll-archives/archive.rb
35
- homepage: https://github.com/alfredxing/jekyll-archives
91
+ homepage: https://github.com/jekyll/jekyll-archives
36
92
  licenses:
37
93
  - MIT
38
94
  metadata: {}
@@ -52,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
108
  version: '0'
53
109
  requirements: []
54
110
  rubyforge_project:
55
- rubygems_version: 2.2.2
111
+ rubygems_version: 2.4.1
56
112
  signing_key:
57
113
  specification_version: 4
58
114
  summary: Post archives for Jekyll.