-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
183 lines (155 loc) · 4.62 KB
/
Rakefile
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
require "bundler/setup"
Bundler.require(:default)
require "rake/clean"
BUILD_DIR = "_site"
CLOBBER.include BUILD_DIR
Commit =
Struct.new(:hash, :author, :date, :title, :body, :index) do
def self.all
@all ||=
`git log --pretty`.split(/^commit /)
.drop(1)
.each_with_index
.map do |string, index|
lines = string.lines.map(&:strip)
hash = lines[0]
author = lines[1].sub(/Author:\s*/, "")
date = lines[2].sub(/Date:\s*/, "")
title = lines[4].strip
body = lines.drop(6).map(&:strip).join("\n")
Commit.new(hash, author, date, title, body, index)
end
end
def self.for_feed
all.first(50)
end
def task_name
"#{BUILD_DIR}/#{hash}/index.html"
end
def relative_url
"/#{hash}/"
end
def github_url
"https://github.com/danott/allow-empty/commit/#{hash}/#comments"
end
def render(rendering_collection:)
ERB.new(File.read("commit.erb")).result(self.binding)
end
def render_body
Kramdown::Document.new(
body,
{
auto_ids: false,
hard_wrap: false,
input: "GFM",
syntax_highlighter: "rouge",
typographic_symbols: {
hellip: "..."
}
}
).to_html
end
# Implied sort: the "previous commit" is the one authored immediately before this one
def prev_commit
incremented_index = index + 1
Commit.all[incremented_index]
end
# Implied sort: the "next commit" is the one authored immediately after this one
def next_commit
incremented_index = index - 1
Commit.all[incremented_index] unless incremented_index.negative?
end
def time
Time.parse(date)
end
end
# Build the entire website
task build: [
"#{BUILD_DIR}/index.html",
"#{BUILD_DIR}/feed.xml",
"#{BUILD_DIR}/style.css"
]
# Build feed.xml
task "#{BUILD_DIR}/feed.xml" => Commit.for_feed.map(&:task_name) do |task|
FileUtils.mkdir_p(BUILD_DIR)
File.write(task.name, Feed.new(Commit.for_feed).render)
end
# Build index.html
task "#{BUILD_DIR}/index.html" => Commit.all.map(&:task_name) do |task|
title = "allow-empty"
html = Layout.new(title, Collection.new(Commit.all).render).render
FileUtils.mkdir_p(BUILD_DIR)
File.write(task.name, html)
end
# Copy style.css
task "#{BUILD_DIR}/style.css" => "style.css" do |task|
FileUtils.mkdir_p(BUILD_DIR)
FileUtils.cp("style.css", task.name)
end
# Rake rule to generate a page for each commit
rule ".html" do |task|
commit = Commit.all.find { |candidate| candidate.task_name == task.name }
title = commit.title
html = Layout.new(title, commit.render(rendering_collection: false)).render
FileUtils.mkdir_p(File.dirname(task.name))
File.write(task.name, html)
end
# Run a web server to preview the site in development
desc "Run a web server hosting #{BUILD_DIR}"
task :serve do
server = WEBrick::HTTPServer.new(Port: 3000, DocumentRoot: BUILD_DIR)
trap("INT") { server.stop }
server.start
end
Layout =
Struct.new(:title, :body) do
def render
ERB.new(File.read("layout.erb")).result(binding)
end
end
Feed =
Struct.new(:commits) do
def channel_link
"https://allow-empty.danott.website"
end
def channel_title
"allow-empty"
end
def render
RSS::Maker.make("2.0") do |maker|
maker.channel.author = commits.first.author
maker.channel.updated = commits.map(&:time).max.to_s
maker.channel.about = channel_link
maker.channel.link = channel_link
maker.channel.title = channel_title
maker.channel.description = channel_title
commits.each do |commit|
maker.items.new_item do |item|
item.description = commit.render_body + footer(commit)
item.link = channel_link + commit.relative_url
item.title = commit.title
item.updated = commit.time.to_s
end
end
end
end
def footer(commit)
address = %w[danott hey.com].join("@")
website_url = channel_link + commit.relative_url
website_url_without_protocol = website_url.delete_prefix("https://")
<<~HTML
<hr />
<p>
Thanks for reading via RSS!
Wanna talk about it?
You can <a href="#{commit.github_url}">comment on GitHub</a> or <a href="mailto:#{address}?subject=Re: #{website_url_without_protocol}">reply via email</a>.
</p>
HTML
end
end
Collection =
Struct.new(:commits) do
def render
ERB.new(File.read("collection.erb")).result(binding)
end
end