-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.rb
executable file
·84 lines (70 loc) · 2.63 KB
/
categories.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
module Jekyll
class CategoryPage < Page
def initialize(params = {})
@site = params[:site]
@base = params[:base]
@dir = params[:dir]
@name = params[:name]
puts "Adding page #{@dir}/#{@name}"
self.process(@name)
self.read_yaml(File.join(@base, '_layouts'), "#{params[:layout]}.html")
self.data['title'] = params[:title]
self.data['posts'] = params[:posts]
self.data['pagination'] = params[:pagination]
self.data['category'] = params[:category]
end
end
class CategoryPages
def initialize(name)
@name = name
@posts = []
@tags = Hash.new{|hash, key| hash[key] = []}
@archive = Hash.new{|hash, key| hash[key] = []}
end
def <<(post)
@posts << post
post.tags.each {|tag| @tags[tag] << post}
@archive[post.date.year] << post
end
def pages(site, base)
ret = []
layout = (site.layouts.key? @name)? @name : 'category'
@tags.each do |tag, posts|
name = "#{tag.downcase.tr(' ', '-').delete('&', '?', '/', '=', '%')}.html"
params = {site: site, base: base, category: @name,
dir: "#{@name}/tag", layout: layout,
name: name, title: tag,
posts: posts, pagination: {}}
ret << CategoryPage.new(params)
end
@archive.each do |year, posts|
params = {site: site, base: base, category: @name,
dir: "#{@name}/#{year}", layout: layout,
name: 'index.html', title: year,
posts: posts, pagination: {}}
ret << CategoryPage.new(params)
end
title = site.config["#{@name}_title"] || @name
params = {site: site, base: base, category: @name,
dir: @name, layout: layout,
name: 'index.html', title: title,
posts: @posts.reverse, pagination: {}}
ret << CategoryPage.new(params)
ret
end
end
class CategoryPageGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'category'
categories = Hash.new{|hash, key| hash[key] = CategoryPages.new(key)}
site.posts.each do |post|
post.categories.each {|category| categories[category] << post}
end
categories.each_value do |category|
category.pages(site, site.source).each {|page| site.pages << page}
end
end
end
end
end