jekyll-archives 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e5597290843350c0f349a0625e966c5b253d914
4
+ data.tar.gz: 3b2f8221b82d532d32f2dc038554176d65a380a5
5
+ SHA512:
6
+ metadata.gz: 17b68ddb19f27ce71fa0bc4fd41cd5da92cbc51f11e74bfdb77b30b63629cd307f0312c4f6740aea94da38d1893e4a7122d4e4966dc1ce99235922baedb62b0f
7
+ data.tar.gz: e9c28eb8ac183bee610d471e1b304ba6d2aec20c90654b1df7be20b3fff9948cda26941b87f70ca6f4e2eace331b189ab49306a2aaf620b317dcf87588b2cc1a
@@ -0,0 +1,83 @@
1
+ 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/'
14
+ }
15
+ }
16
+
17
+ def initialize(config = nil)
18
+ @config = Utils.deep_merge_hashes(DEFAULTS, config['jekyll-archives'])
19
+ end
20
+
21
+ def generate(site)
22
+ @site = site
23
+ @posts = site.posts
24
+ @archives = []
25
+
26
+ @site.config['jekyll-archives'] = @config
27
+
28
+ read_archives
29
+ @site.pages += @archives
30
+ end
31
+
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])
36
+ end
37
+ categories.each do |category|
38
+ @archives << Archive.new(@site, category[1], "category", category[0])
39
+ end
40
+ years.each do |year|
41
+ @archives << Archive.new(@site, year[1], "year", year[0])
42
+ end
43
+ end
44
+
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
66
+
67
+ def tags
68
+ post_attr_hash('tags')
69
+ end
70
+
71
+ def categories
72
+ post_attr_hash('categories')
73
+ end
74
+
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
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,111 @@
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
38
+
39
+ # The template of the permalink.
40
+ #
41
+ # Returns the template String.
42
+ def template
43
+ site.config['jekyll-archives']['permalinks'][type]
44
+ end
45
+
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
51
+
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
64
+
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
78
+
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
+ }]
86
+
87
+ Utils.deep_merge_hashes(data, further_data)
88
+ end
89
+
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
100
+
101
+ # Returns the object as a debug String.
102
+ def inspect
103
+ "#<Jekyll:Archive @type=#{@type.to_s} @name=#{@name}>"
104
+ end
105
+
106
+ # Returns the Boolean of whether this Page is HTML or not.
107
+ def html?
108
+ true
109
+ end
110
+ end
111
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-archives
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alfred Xing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Automatically generate post archives by dates, tags, and categories.
28
+ email: support@github.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/jekyll-archives.rb
34
+ - lib/jekyll-archives/archive.rb
35
+ homepage: https://github.com/alfredxing/jekyll-archives
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 2.2.2
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Post archives for Jekyll.
59
+ test_files: []