bunto-archives 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6050195a3fe52e29423dcdd36058cddfcc0d9ea
4
+ data.tar.gz: 7c52c9fbcea17983036817714d7825d681b7dece
5
+ SHA512:
6
+ metadata.gz: 5ce6f5f453f44ad450d2841b17dad353a7addae01c58cbacd98c9d80b341ad2fdce61bfcc4b9883eea54ef6d476b00a72762e6676b99596bb31cb09806393b6b
7
+ data.tar.gz: 1dc223990d4b05217504224502c3355d83b0642eb9f0cf9fcca1c851ad81a5d916cb951751adb07a89e16385ac1ada0c1cf270cf4569970c7d4e582ba7c443d5
@@ -0,0 +1,152 @@
1
+ require 'bunto'
2
+
3
+ module Bunto
4
+ module Archives
5
+ # Internal requires
6
+ autoload :Archive, 'bunto-archives/archive'
7
+ autoload :VERSION, 'bunto-archives/version'
8
+
9
+ if (Bunto.const_defined? :Hooks)
10
+ Bunto::Hooks.register :site, :after_reset do |site|
11
+ # We need to disable incremental regen for Archives to generate with the
12
+ # correct content
13
+ site.regenerator.instance_variable_set(:@disabled, true)
14
+ end
15
+ end
16
+
17
+ class Archives < Bunto::Generator
18
+ safe true
19
+
20
+ DEFAULTS = {
21
+ 'layout' => 'archive',
22
+ 'enabled' => [],
23
+ 'permalinks' => {
24
+ 'year' => '/:year/',
25
+ 'month' => '/:year/:month/',
26
+ 'day' => '/:year/:month/:day/',
27
+ 'tag' => '/tag/:name/',
28
+ 'category' => '/category/:name/'
29
+ }
30
+ }
31
+
32
+ def initialize(config = nil)
33
+ if config['bunto-archives'].nil?
34
+ @config = DEFAULTS
35
+ else
36
+ @config = Utils.deep_merge_hashes(DEFAULTS, config['bunto-archives'])
37
+ end
38
+ end
39
+
40
+ def generate(site)
41
+ @site = site
42
+ @posts = site.posts
43
+ @archives = []
44
+
45
+ @site.config['bunto-archives'] = @config
46
+
47
+ read
48
+ render
49
+ write
50
+
51
+ @site.keep_files ||= []
52
+ @archives.each do |archive|
53
+ @site.keep_files << archive.relative_path
54
+ end
55
+ @site.config["archives"] = @archives
56
+ end
57
+
58
+ # Read archive data from posts
59
+ def read
60
+ read_tags
61
+ read_categories
62
+ read_dates
63
+ end
64
+
65
+ def read_tags
66
+ if enabled? "tags"
67
+ tags.each do |title, posts|
68
+ @archives << Archive.new(@site, title, "tag", posts)
69
+ end
70
+ end
71
+ end
72
+
73
+ def read_categories
74
+ if enabled? "categories"
75
+ categories.each do |title, posts|
76
+ @archives << Archive.new(@site, title, "category", posts)
77
+ end
78
+ end
79
+ end
80
+
81
+ def read_dates
82
+ years.each do |year, posts|
83
+ @archives << Archive.new(@site, { :year => year }, "year", posts) if enabled? "year"
84
+ months(posts).each do |month, posts|
85
+ @archives << Archive.new(@site, { :year => year, :month => month }, "month", posts) if enabled? "month"
86
+ days(posts).each do |day, posts|
87
+ @archives << Archive.new(@site, { :year => year, :month => month, :day => day }, "day", posts) if enabled? "day"
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ # Checks if archive type is enabled in config
94
+ def enabled?(archive)
95
+ @config["enabled"] == true || @config["enabled"] == "all" || if @config["enabled"].is_a? Array
96
+ @config["enabled"].include? archive
97
+ end
98
+ end
99
+
100
+ # Renders the archives into the layouts
101
+ def render
102
+ payload = @site.site_payload
103
+ @archives.each do |archive|
104
+ archive.render(@site.layouts, payload)
105
+ end
106
+ end
107
+
108
+ # Write archives to their destination
109
+ def write
110
+ @archives.each do |archive|
111
+ archive.write(@site.dest)
112
+ end
113
+ end
114
+
115
+ def tags
116
+ @site.post_attr_hash('tags')
117
+ end
118
+
119
+ def categories
120
+ @site.post_attr_hash('categories')
121
+ end
122
+
123
+ # Custom `post_attr_hash` method for years
124
+ def years
125
+ hash = Hash.new { |h, key| h[key] = [] }
126
+
127
+ # In Bunto 3, Collection#each should be called on the #docs array directly.
128
+ if Bunto::VERSION >= '3.0.0'
129
+ @posts.docs.each { |p| hash[p.date.strftime("%Y")] << p }
130
+ else
131
+ @posts.each { |p| hash[p.date.strftime("%Y")] << p }
132
+ end
133
+ hash.values.each { |posts| posts.sort!.reverse! }
134
+ hash
135
+ end
136
+
137
+ def months(year_posts)
138
+ hash = Hash.new { |h, key| h[key] = [] }
139
+ year_posts.each { |p| hash[p.date.strftime("%m")] << p }
140
+ hash.values.each { |posts| posts.sort!.reverse! }
141
+ hash
142
+ end
143
+
144
+ def days(month_posts)
145
+ hash = Hash.new { |h, key| h[key] = [] }
146
+ month_posts.each { |p| hash[p.date.strftime("%d")] << p }
147
+ hash.values.each { |posts| posts.sort!.reverse! }
148
+ hash
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,169 @@
1
+ module Bunto
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['bunto-archives']
35
+
36
+ # Generate slug if tag or category (taken from bunto/bunto/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
+ end
51
+
52
+ # The template of the permalink.
53
+ #
54
+ # Returns the template String.
55
+ def template
56
+ @config['permalinks'][type]
57
+ end
58
+
59
+ # The layout to use for rendering
60
+ #
61
+ # Returns the layout as a String
62
+ def layout
63
+ if @config['layouts'] && @config['layouts'][type]
64
+ @config['layouts'][type]
65
+ else
66
+ @config['layout']
67
+ end
68
+ end
69
+
70
+ # Returns a hash of URL placeholder names (as symbols) mapping to the
71
+ # desired placeholder replacements. For details see "url.rb".
72
+ def url_placeholders
73
+ if @title.is_a? Hash
74
+ @title.merge({ :type => @type })
75
+ else
76
+ { :name => @slug, :type => @type }
77
+ end
78
+ end
79
+
80
+ # The generated relative url of this page. e.g. /about.html.
81
+ #
82
+ # Returns the String url.
83
+ def url
84
+ @url ||= URL.new({
85
+ :template => template,
86
+ :placeholders => url_placeholders,
87
+ :permalink => nil
88
+ }).to_s
89
+ rescue ArgumentError
90
+ raise ArgumentError.new "Template \"#{template}\" provided is invalid."
91
+ end
92
+
93
+ # Add any necessary layouts to this post
94
+ #
95
+ # layouts - The Hash of {"name" => "layout"}.
96
+ # site_payload - The site payload Hash.
97
+ #
98
+ # Returns nothing.
99
+ def render(layouts, site_payload)
100
+ payload = Utils.deep_merge_hashes({
101
+ "page" => to_liquid
102
+ }, site_payload)
103
+
104
+ do_layout(payload, layouts)
105
+ end
106
+
107
+ # Convert this Convertible's data to a Hash suitable for use by Liquid.
108
+ #
109
+ # Returns the Hash representation of this Convertible.
110
+ def to_liquid(attrs = nil)
111
+ further_data = Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
112
+ [attribute, send(attribute)]
113
+ }]
114
+
115
+ Utils.deep_merge_hashes(data, further_data)
116
+ end
117
+
118
+ # Produce a title object suitable for Liquid based on type of archive.
119
+ #
120
+ # Returns a String (for tag and category archives) and nil for
121
+ # date-based archives.
122
+ def title
123
+ if @title.is_a? String
124
+ @title
125
+ end
126
+ end
127
+
128
+ # Produce a date object if a date-based archive
129
+ #
130
+ # Returns a Date.
131
+ def date
132
+ if @title.is_a? Hash
133
+ args = @title.values.map { |s| s.to_i }
134
+ Date.new(*args)
135
+ end
136
+ end
137
+
138
+ # Obtain destination path.
139
+ #
140
+ # dest - The String path to the destination dir.
141
+ #
142
+ # Returns the destination file path String.
143
+ def destination(dest)
144
+ path = Bunto.sanitized_path(dest, URL.unescape_path(url))
145
+ path = File.join(path, "index.html") if url =~ /\/$/
146
+ path
147
+ end
148
+
149
+ # Obtain the write path relative to the destination directory
150
+ #
151
+ # Returns the destination relative path String.
152
+ def relative_path
153
+ path = URL.unescape_path(url).gsub(/^\//, '')
154
+ path = File.join(path, "index.html") if url =~ /\/$/
155
+ path
156
+ end
157
+
158
+ # Returns the object as a debug String.
159
+ def inspect
160
+ "#<Bunto:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
161
+ end
162
+
163
+ # Returns the Boolean of whether this Page is HTML or not.
164
+ def html?
165
+ true
166
+ end
167
+ end
168
+ end
169
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bunto-archives
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alfred Xing
8
+ - Suriyaa Kudo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ name: bunto
21
+ prerelease: false
22
+ type: :runtime
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ name: rake
35
+ prerelease: false
36
+ type: :development
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ name: rdoc
49
+ prerelease: false
50
+ type: :development
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ name: shoulda
63
+ prerelease: false
64
+ type: :development
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ name: minitest
77
+ prerelease: false
78
+ type: :development
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Automatically generate post archives by dates, tags, and categories.
85
+ email:
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - lib/bunto-archives.rb
91
+ - lib/bunto-archives/archive.rb
92
+ homepage: https://github.com/bunto/bunto-archives
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Post archives for Bunto.
116
+ test_files: []