This repository has been archived by the owner on Nov 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
251 lines (198 loc) · 5.43 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true
require 'yaml'
require 'erb'
require 'time'
require 'logger'
require 'fileutils'
require 'pry'
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG
desc "prepare for publishing"
task prep: [:build_indexes, :mkfeed]
task default: :prep
desc 'build indexes at each level in the tree'
task :build_indexes do
Dir.chdir('posts') do |postsDir|
File.write(
'index.org',
Dir['**/*.{org,md,markdown}']
.sort
.reverse
.uniq
.reject { |f| f.match(%r{index.org}) }
.map { |f| "- [[./#{f}]]" }
.join("\n")
)
end
Dir['posts/*/'].each do |year|
Dir.chdir(year) do |dir|
logger.debug "In dir #{dir}"
File.write(
'index.org',
Dir['./**/*.{org,md,markdown}']
.sort
.reverse
.uniq
.reject {|f| f.match(%r{index.org}) }
.map { |f| "- [[#{f}]]".tap { |l| logger.debug "link: #{l}" } }
.join("\n")
)
end
end
# don't need indexes for the leaf directories
end
desc 'create atom feed of most recent updates'
task :mkfeed do
feed = {
id: "https://github.com/tamouse/swaac",
title: "Tamouse's Software as a Craft blog",
}
posts = Dir['posts/**/*.{org,md,markdown}'].map do |file|
next if file =~ /index.org/
entry = Entry.new(file).entry
{ entry: entry }
end.compact.sort_by{|entry| entry[:entry][:updated]}.reverse.take(10)
feed[:updated] = posts.first[:entry][:updated]
feed[:entries] = posts
feed_xml = Feed2Xml.new(feed).xml
File.write("docs/feed.xml", feed_xml)
logger.info "Wrote docs/feed.xml"
end
desc 'convert markdown files to org with pandoc'
task :pandoc do
Dir['posts/*.markdown', 'posts/*.md'].each do |file|
outfile = file.sub(/\.(markdown|md)/, '.org')
logger.info("Converting #{file} to #{outfile}")
system "pandoc -s --highlight=zenburn --wrap=none -o #{outfile} #{file}"
end
end
desc 'move org files into posts/YYYY/MM/ directories from posts/'
task :move_posts do
org_posts = Dir['posts/*.org']
org_posts.each do |post|
m = post.match(%r{\Aposts\/(\d{4})-(\d{2})-(\d{2})-(.*)\z})
year = m[1]
month = m[2]
day = m[3]
title = m[4]
logger.debug "post: #{post}, year: #{year}, month: #{month}, day: #{day}, title: #{title}"
dir = "posts/#{year}/#{month}"
file = "#{dir}/#{title}"
logger.debug "dir: #{dir}, file: #{file}"
FileUtils.mkdir_p(dir)
FileUtils.cp(post, file, verbose: true)
end
end
desc 'rewrite markdown so it can be parsed by pandoc'
task :rewrite, :infile do |t, args|
infile = args[:infile]
logger.info("processing #{infile}")
text = File.read(infile)
FileUtils.mv(infile, "#{infile}.bak")
(_, fm, body) = text.split(/^---$/m)
fm_hash = YAML.load(fm)
fm_hash.delete("layout")
keywords = (Array(fm_hash.delete("categories")) + Array(fm_hash.delete("tags"))).uniq.sort
outfile = %Q{# #{fm_hash.delete("title")}
- published date: #{fm_hash.delete("date")}
- keywords: #{keywords}
- source: #{fm_hash.delete("source")}
}
fm_hash.each do |(k, v)|
outfile += "- #{k}: #{v}\n"
end
outfile += "\n"
modified_body = body.gsub(/{% highlight (.*?) %}/, '```\1')
.gsub(/{% endhighlight %}/, '```')
outfile += modified_body
File.write(infile, outfile)
logger.info("wrote #{infile}")
end
class Entry
attr_accessor :entry
def initialize(file)
@entry = {
id: createid(file),
updated: moddate(file),
title: findtitle(file),
content: getcontent(file),
author: {
name: "Tamara Temple",
email: "[email protected]"
}
}
end
def createid(file)
"https://github.com/tamouse/swaac/blob/master/#{file}"
end
def moddate(file)
File.mtime(file)
end
def findtitle(file)
lines = getcontent(file).split("\n")
title = lines.grep(/^#+TITLE/).first.to_s.sub(/^#+TITLE +/, '')
title = title.empty? ? lines.grep(/^\* /).first.to_s.sub(/^\* +/, '') : title
title = title.empty? ? File.basename(file, '.*') : title
title
end
def getcontent(file)
@contents ||= File.read(file)
end
end
class Feed2Xml
attr_reader :feed
def initialize(feed)
@feed = feed
end
def xml
header +
hash2xml(feed, root: "feed")
end
def header
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
end
def hash2xml(h, options={})
root = options[:root]
indent = options[:indent] || 0
out = ""
if (root)
out += "#{" "*indent}<#{root == "feed" ? "feed xmlns=\"http://www.w3.org/2005/Atom\"" : root}>\n"
end
indent += 2
h.each do |(k, v)|
case v
when Array
out += array2xml(v, indent: indent, root: k)
when Hash
out += hash2xml(v, indent: indent, root: k)
when Time
out += "#{" "*indent}<#{k}>#{v.xmlschema}</#{k}>\n"
else
out += "#{" "*indent}<#{k}>#{ERB::Util.h(v)}</#{k}>\n"
end
end
indent -= 2
if (root)
out += "#{" "*indent}</#{root}>\n"
end
out
end
def array2xml(a, options={})
root = options[:root]
indent = options[:indent]
out = ""
a.each do |entry|
case entry
when Array
out += array2xml(entry, indent: indent)
when Hash
out += hash2xml(entry, indent: indent)
when Time
out += "#{" "*indent}<item>#{Time.xmlschema(entry)}</item>\n"
else
out += "#{" "*indent}<item>#{ERB::Util.h(entry)}</item>\n"
end
end
out
end
end