-
Notifications
You must be signed in to change notification settings - Fork 227
/
author_generator.rb
114 lines (94 loc) · 3.48 KB
/
author_generator.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
# encoding: utf-8
#
module Jekyll
# The AuthorIndex class creates a single author page for all their posts.
class AuthorIndex < Page
# Initializes a new AuthorIndex.
#
# +base+ is the String path to the <source>.
# +author_dir+ is the String path between <source> and the author folder.
# +author+ is the author currently being processed.
def initialize(site, base, author_dir, author)
@site = site
@base = base
@dir = author_dir
@name = 'index.html'
self.process(@name)
# Read the YAML data from the layout page.
self.read_yaml(File.join(base, '_layouts'), 'author_index.html')
self.data['author'] = author
# Set the title for this page.
title_prefix = 'Author: '
self.data['title'] = "#{title_prefix}#{author}"
# Set the meta-description for this page.
meta_description_prefix = 'author: '
self.data['description'] = "#{meta_description_prefix}#{author}"
end
end
# The Site class is a built-in Jekyll class with access to global site config information.
class Site
def authors
self.config['authors'].keys
end
# Creates an instance of authorIndex for each author page, renders it, and
# writes the output to a file.
#
# +author_dir+ is the String path to the author folder.
# +author+ is the author currently being processed.
def write_author_index(author_dir, author)
index = AuthorIndex.new(self, self.source, author_dir, author)
index.render(self.layouts, site_payload)
index.write(self.dest)
# Record the fact that this page has been added, otherwise Site::cleanup will remove it.
self.pages << index
end
# Loops through the list of author pages and processes each one.
def write_author_indexes
if self.layouts.key? 'author_index'
dir = self.config['author_dir'] || 'author'
self.authors.each do |author|
self.config['current_author'] = author
self.write_author_index(File.join(dir, author.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase), author)
end
# Throw an exception if the layout couldn't be found.
else
throw "No 'author_index' layout found."
end
end
end
# Jekyll hook - the generate method is called by jekyll, and generates all of the author pages.
class GenerateAuthors < Generator
safe true
priority :low
def generate(site)
if ENV["PRODUCTION"] == "YES"
site.write_author_indexes
end
end
end
# Adds some extra filters used during the author creation process.
module Filters
# Outputs a list of authors as comma-separated <a> links. This is used
# to output the author list for each post on a author page.
#
# +authors+ is the list of authors to format.
#
# Returns string
#
def author_link(author)
dir = @context.registers[:site].config['author_dir']
"<a class='author' href='/#{dir}/#{author.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase}/'>#{item}</a>"
end
# Outputs the post.date as formatted html, with hooks for CSS styling.
#
# +date+ is the date object to format as HTML.
#
# Returns string
def date_to_html_string(date)
result = '<span class="month">' + date.strftime('%b').upcase + '</span> '
result += date.strftime('<span class="day">%d</span> ')
result += date.strftime('<span class="year">%Y</span> ')
result
end
end
end