-
Notifications
You must be signed in to change notification settings - Fork 14
/
author_generator.rb
159 lines (137 loc) · 5.07 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
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
# encoding: utf-8
#
# Jekyll author page generator.
# http://recursive-design.com/projects/jekyll-plugins/
#
# Version: 0.1.4 (201101061053)
#
# Copyright (c) 2010 Dave Perrett, http://recursive-design.com/
# Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
#
# A generator that creates author pages for jekyll sites.
#
# Included filters :
# - author_links: Outputs the list of author as comma-separated <a> links.
# - date_to_html_string: Outputs the post.date as formatted html, with hooks for CSS styling.
#
# Available _config.yml settings :
# - author_dir: The subfolder to build author pages in (default is 'authors').
# - author_title_prefix: The string used before the author name in the page title (default is
# 'Author: ').
module Jekyll
# The AuthorIndex class creates a single author page for the specified author.
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'
puts "author #{author} @dir #{author_dir}"
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 = site.config['author_title_prefix'] || 'author: '
self.data['title'] = "#{title_prefix}#{author}"
# Set the meta-description for this page.
meta_description_prefix = site.config['author_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
# 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
author_dirs_written = []
if Jekyll::VERSION < '3.0.0'
post_docs = self.posts
else
post_docs = self.posts.docs
end
if self.layouts.key? 'author_index'
dir = self.config['author_dir'] || 'authors'
post_docs.each do |post|
post_authors = post.data["author"]
if String.try_convert(post_authors)
post_authors = [ post_authors ]
end
post_authors.each do |author|
author_dir = File.join(dir, author.downcase.gsub(' ', '-'))
if not author_dirs_written.include?(author_dir)
author_dirs_written.push(author_dir)
self.write_author_index(author_dir, author)
end
end unless post_authors.nil?
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 GenerateAuthor < Generator
safe true
priority :high
def generate(site)
site.write_author_indexes
#puts "site.authors #{site.authors}"
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.
#
# +author+ is the list of author to format.
#
# Returns string
#
def author_links(authors)
dir = @context.registers[:site].config['author_dir'] || "authors"
if String.try_convert(authors)
authors = [ authors ]
end
authors = authors.map do |author|
"<a class='author' href='/#{dir}/#{author.downcase.gsub(' ', '-')}/'>#{author}</a>"
end
case authors.length
when 0
""
when 1
authors[0].to_s
else
"#{authors[0...-1].join(', ')}, #{authors[-1]}"
end
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