-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.rb
66 lines (55 loc) · 1.57 KB
/
go.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
require 'csv';
require 'optparse'
## Options
options = {}
OptionParser.new do |parser|
parser.banner = "Usage: go.rb -i <file.csv> -o <DIR>"
parser.on("-i FILENAME", "CSV file to read from")
parser.on("-o DIR", "Directory into which to dump the files")
end.parse!(into:options)
inputFile = options[:i]
outDIR = options[:o]
if (!inputFile || !outDIR) then
puts "Missing option. Run go.rb -h for help"
exit
end
if (!outDIR.end_with?("/"))
outDIR = outDIR + "/"
end
## Here we go
previousTitle = ""
filename = 0
outFile = nil
def writeOut(file, text)
file.write(text)
file.write("\n")
end
def dumpItem(file, note, content)
writeOut file, "Note: #{note}" if note
writeOut file, "#{content}" if content
end
CSV.foreach(inputFile, headers: true) do |row|
title = row["Title"]
author = row["Author"]
created = row["Created"]
location = row["Located"]
content = row["Content"]
note = row["Notes"]
if (title == previousTitle) then
# Adding to the same book
writeOut outFile, "\n---\n"
else
# Starting a new book, so end previous book
writeOut outFile, "\n#imported/clippings" if outFile
outFile.close() if outFile
# and start the next
filename = filename + 1
outFile = File.new("#{outDIR}#{filename}.txt","w")
previousTitle = title
writeOut outFile, "\# #{title}"
writeOut outFile, "#{author}"
writeOut outFile, "#{created}\n\n" # not ideal, just a single date.
end
dumpItem(outFile, note, content)
end
outFile.close() if outFile