-
Notifications
You must be signed in to change notification settings - Fork 10
/
page_collections.rb
204 lines (171 loc) · 5.28 KB
/
page_collections.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module Jekyll
module PageCollections
VERSION = "1.0.0"
class PageCollectionConfiguration
attr_reader :name
def initialize(site, name)
@site_config = site.config
@name = name
@config = (@site_config['page_collections'].find { |c|
name = if c.is_a?(Hash)
c.keys.first
else
c.to_s
end
name == @name
}[@name]) || {}
end
def permalink_style
(@config['permalink'] || @site_config['permalink']).to_sym
end
def containing_dir
@config['source'] || "_#{@name}"
end
end
# A collection of CollectionPages
class PageCollection
ATTRIBUTES_FOR_LIQUID = %w[
name
pages
categories
tags
]
attr_accessor :name, :pages, :categories, :tags
def initialize(name)
@name = name
@pages = []
@categories = Hash.new { |hash, key| hash[key] = [] }
@tags = Hash.new { |hash, key| hash[key] = [] }
end
def add_page(page)
@pages << page
page.categories.each { |c| @categories[c] << page }
page.tags.each { |c| @tags[c] << page }
end
def add_pages(pages)
pages.each { |page| self.add_page(page) }
end
def to_liquid(attrs = nil)
Hash[(attrs || self.class::ATTRIBUTES_FOR_LIQUID).map { |attribute|
[attribute, send(attribute)]
}]
end
end
class CollectionPage < Post
def initialize(site, source, dir, name, config)
@config = config
super(site, source, dir, name)
end
def containing_dir(source, dir)
return File.join(source, dir, @config.containing_dir)
end
def relative_path
File.join(@dir, @config.containing_dir, @name)
end
def inspect
"<CollectionPage: #{self.id}"
end
def template
style = case @config.permalink_style
when :pretty
"/:categories/:year/:month/:day/:title/"
when :none
"/:categories/:title.html"
when :date
"/:categories/:year/:month/:day/:title.html"
when :ordinal
"/:categories/:year/:y_day/:title.html"
else
@config.permalink_style.to_s
end
"/#{@config.name}#{style}"
end
def next
pages = self.site.data['page_collections'][@config.name].pages
pos = pages.index(self)
if pos && pos < pages.length-1
pages[pos+1]
else
nil
end
end
def previous
pages = self.site.data['page_collections'][@config.name].pages
pos = pages.index(self)
if pos && pos > 0
pages[pos-1]
else
nil
end
end
# Methods from Page
def uses_relative_permalinks
false
end
def html?
output_ext == '.html'
end
end
class PageCollectionGenerator < Generator
def generate(site)
collections = site.config['page_collections'] || []
site.data['page_collections'] = {}
collections.each do |collection|
name = if collection.is_a?(Hash)
collection.keys.first
else
collection.to_s
end
config = PageCollectionConfiguration.new(site, name)
pages = read_content(site, config, CollectionPage).sort
page_collection = PageCollection.new(name)
page_collection.add_pages(pages)
site.pages.concat(pages)
site.data['page_collections'][name] = page_collection
end
end
private
def read_content(site, config, klass)
dir = ''
site.get_entries(dir, config.containing_dir).map do |entry|
klass.new(site, site.source, dir, entry, config) if klass.valid?(entry)
end.reject do |entry|
entry.nil?
end
end
end
class PageUrl < Jekyll::Tags::PostUrl
TagName = 'page_url'
Syntax = /^([\w-]+)\s+((?:.+\/)*\d+-\d+-\d+-.*)$/
def initialize(tag_name, markup, tokens)
if markup && markup.strip =~ Syntax
@collection = $1
post_name = $2
else
raise SyntaxError.new("Syntax Error in '#{TagName}' - Valid syntax: #{TagName} <collection> <page>")
end
super(tag_name, post_name, tokens)
end
def render(context)
site = context.registers[:site]
if site.data['page_collections'] && site.data['page_collections'][@collection]
site.data['page_collections'][@collection].pages.each do |p|
if @post == p
return p.url
end
end
raise ArgumentError.new <<-eos
Could not find page "#{@orig_post}" in collection "#{@collection}" in tag '#{TagName}'.
Make sure the page exists and the name is correct.
eos
else
raise ArgumentError.new <<-eos
Could not find page collection "#{@collection}" in tag '#{TagName}'
Make sure the collection exists and the name is correct.
eos
end
end
end
end
end
Liquid::Template.register_tag(Jekyll::PageCollections::PageUrl::TagName, Jekyll::PageCollections::PageUrl)