/
migrate.rb
352 lines (284 loc) · 9.22 KB
/
migrate.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
require './lib/zettel'
require 'set'
require 'net/http'
require 'json'
require 'date'
require 'rubygems/text'
include Gem::Text
class Migrator
DST = '../../wiki/'
def initialize
@unique_ids = Set.new
end
def file_crawl(src)
search_path = "#{src}**/*.{md,txt}"
count = Dir.glob(search_path).count
puts "## Working #{src}...\n#{Dir.glob(search_path).count} files in #{src}"
Dir.glob(search_path).each_with_index do |file, index|
puts "[#{index+1}/#{count}]: #{File.basename(file)...}"
yield file
end
end
def ensure_uniqueness(zettel)
if @unique_ids.include? zettel.id then
# we've got a dup
puts "🔴 Duplicate ID! #{zettel.title}: #{zettel.id}"
zettel.set(:id, zettel.id.to_i + 1)
puts "trying #{zettel.id}..."
self.ensure_uniqueness(zettel)
else
if zettel.id.size > 12 then
puts "#{zettel.id} 🟡 ID is too long! #{zettel.title}"
elsif zettel.id.size < 12 then
puts "#{zettel.id} 🟡 ID is too short! #{zettel.title}"
end
@unique_ids << zettel.id
end
end
def old_zettel()
file_crawl('../zk/') do |old_zettel|
zettel = Zettel.new()
zettel.enable_alias = true
content = File.read(old_zettel)
if content.match(/^\w+: .+$/) then
metadata, *content = content.split(/\n\n/)
if content.class == Array then
content = content.join("\n\n")
end
metadata
.split(/\n/)
.map{|metadata|
key, value = metadata.scan(/^(\w+): (.*)$/).flatten
if key == 'tags' then
value = value.split(', ')
end
zettel.set(key, value)
}
end
if !zettel.id then
# try in the filename?
zettel.set('id', old_zettel.scan(/\d{12}-/)[0].gsub('-', ''))
end
if !zettel.title then
title, *content = content.split(/\n/)
title = title.gsub(/^#+ /, '')
zettel.set(:title, title)
content = content.join("\n")
end
zettel.body = content
ensure_uniqueness(zettel)
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
def books()
file_crawl('../booknotes/') do |booknote|
unless
[
/The War of Art/, # already has a note
/You and Me Forever/,
/Fall/,
/Deep Work/,
/Your Family God's Way/
].any?{|pattern| booknote.match? pattern } # blocklist
then
filename = File.basename(booknote)
zettel = Zettel.new()
zettel.enable_alias = true
content = File.read(booknote)
title, *content = content.split(/\n/)
title = title.gsub(/^#+ /, '')
content = content.join("\n")
zettel.add_tag('booknote')
zettel.add_tag('book')
zettel.body = content.strip
query = filename.gsub(/\..{2,3}$/, '')
title = query.split(" by ").first.strip
author = query.split(" by ").last.strip
begin
date = Date.parse(content)
rescue StandardError
date = File.birthtime(booknote)
end
books =
JSON.parse(Net::HTTP.get_response(
URI("https://www.googleapis.com/books/v1/volumes?q=#{URI.encode(query)}")).body)
book = books["items"].min_by do |b|
levenshtein_distance(b["volumeInfo"]["title"], title)
end["volumeInfo"]
zettel.set(:date, date.strftime("%a, %e %b %Y %T"))
zettel.set(:id, date.strftime("%Y%m%d%H%M").ljust(12, "0"))
ensure_uniqueness(zettel)
zettel.set(:title, title)
zettel.set(:subtitle, book['subtitle'])
zettel.set(:author, book['authors'].join(', ')) # close enough to MLA
zettel.set(:publisher, book['publisher'])
zettel.set(:identifer, book["industryIdentifiers"][0]["identifier"])
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
end
def links()
file_crawl('../links/') do |link|
zettel = Zettel.new
content = File.read(link)
filename = File.basename(link)
datestring =
filename
.scan(/\w+ \d+, \d\d\d\d/)
.flatten
.first
# handle backwards filenames
if datestring.nil? || datestring.empty? then
date = File.birthtime(link)
else
date = DateTime.parse(datestring)
end
title = content.lines.reject{|l| l.match(/tags:.*/) || l.strip.empty? }.first.strip
link = URI.extract(content)
.filter{|url| url =~ /\A#{URI::regexp(['http', 'https'])}\z/}
.first
zettel.set(:id, date.strftime("%Y%m%d%H%M").ljust(12, "0"))
ensure_uniqueness(zettel)
zettel.set(:date, date.strftime("%a, %e %b %Y %T"))
zettel.add_tag('links')
zettel.set(:title, title)
content =
content
.gsub(/^#{title}$/, '')
.gsub(/\w+ \d+, \d\d\d\d at \d\d:\d\d../, "")
.gsub(/via Instapaper /, "")
.gsub(/^#{URI.regexp}/m, "")
.strip
content = content + "\n\n[#{title}](#{link})"
zettel.body = content.strip
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
def diary()
file_crawl('../diary/') do |entry|
unless
[
/diary.md/, # hub, unneeded here
/2018-02-11/, # generated links file
/2018-06-22/, # campus email research
].any?{|pattern| entry.match? pattern } # blocklist
then
zettel = Zettel.new
content = File.read(entry)
filename = File.basename(entry)
datestring =
filename
.scan(/(\d+)/)
.flatten
.map{ |n| n.to_i }
date = DateTime.new(*datestring)
content
.scan(/:([a-zA-Z\-_]+)/)
.flatten
.map{ |t| '#' + t.gsub(":", "").gsub('-', '_').downcase }
.uniq
.each{ |tag| zettel.add_tag(tag) }
zettel.add_tag('#journal')
title = content.scan(/^#+ .*/).first.to_s.gsub(/^#+ /, "")
if content.match(/^## Notes/) then zettel.add_tag('#meeting') end
zettel.set(:id, date.strftime("%Y%m%d%H%M").ljust(12, "0"))
ensure_uniqueness(zettel)
zettel.set(:date, date.strftime("%a, %e %b %Y %T"))
zettel.set(:title, title)
zettel.body = content
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
end
def notes()
file_crawl('../notes/') do |entry|
unless
[
/bible_study/
].any?{|pattern| entry.match? pattern } # blocklist
then
zettel = Zettel.new
content = File.read(entry)
filename = File.basename(entry)
date = File.ctime(entry)
content
.scan(/:([a-zA-Z\-_]+)/)
.flatten
.map{ |t| '#' + t.gsub(":", "").gsub('-', '_').downcase }
.uniq
.each{ |tag| zettel.add_tag(tag) }
File.dirname(entry)
.gsub("../notes", "")
.split("/")
.reject{ |s| s.empty? }
.each { |dir| zettel.add_tag("##{dir.gsub(" ", "_")}") }
title = content.match(/^# .*/).to_s.gsub("# ", "")
title = File.basename(entry, ".*")
zettel.set(:id, date.strftime("%Y%m%d%H%M").ljust(12, "0"))
ensure_uniqueness(zettel)
zettel.set(:date, date.strftime("%a, %e %b %Y %T"))
zettel.set(:title, title)
zettel.body = content
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
end
def bible_study()
file_crawl('../notes/bible_study/') do |entry|
unless
[
].any?{|pattern| entry.match? pattern } # blocklist
then
zettel = Zettel.new
content = File.read(entry)
filename = File.basename(entry)
date = File.ctime(entry)
content
.scan(/:([a-zA-Z\-_]+)/)
.flatten
.map{ |t| '#' + t.gsub(":", "").gsub('-', '_').downcase }
.uniq
.each{ |tag| zettel.add_tag(tag) }
zettel.add_tag('journal')
zettel.add_tag('devotional')
title = "#{File.basename(entry, ".*")}-devotional"
zettel.set(:id, date.strftime("%Y%m%d%H%M").ljust(12, "0"))
ensure_uniqueness(zettel)
zettel.set(:date, date.strftime("%a, %e %b %Y %T"))
zettel.set(:title, title)
zettel.body = content
File.write("#{DST}/#{zettel.render_filename()}", zettel.render())
end
end
end
def test
# Writing some tests
test = Zettel.new
test.set('id', '20200998955')
test.set('title', 'If you can read this...')
test.set('tags', ['tag1', 'tag2', '#tag3'])
test.set('isbn', '0192u319231098')
test.body = "Then the tests are working."
puts "<< #{test.render_filename} >>"
puts test.render
end
def run()
# clean out the folder
Dir.each_child(DST){ |f| File.delete(File.join(DST, f)) unless f.match("obsidian") }
# Pull in each source:
# - new zk
old_zettel()
# - booknotes (include subfolders)
books()
# - links
links()
# - diary
diary()
# - notes (include subfolders)
notes()
# - devotional notes (created in Byword, there may be duplicates in Diary)
bible_study()
end
end
migrator = Migrator.new()
migrator.run()