-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-to-standard.rb
127 lines (111 loc) · 2.78 KB
/
simple-to-standard.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
require 'securerandom'
require 'json'
require 'date'
class Note
attr_reader :uuid, :tags
def initialize(note_json)
# puts note_json["id"] # Uncomment to see what note we fail on.
@uuid = SecureRandom.uuid
@title, @body = split_title(note_json['content'])
@tags = note_json["tags"] || []
@created_at = note_json["creationDate"]
@updated_at = note_json["lastModified"]
end
def as_json
{
created_at: @created_at,
updated_at: @updated_at,
uuid: @uuid,
content_type: "Note",
content: {
title: @title,
text: @body,
references: [],
appData: {
'org.standardnotes.sn' => {
client_updated_at: @updated_at
}
}
}
}
end
private
def split_title(content)
split = content.index("\r\n")
if split
title = content[0...split]
body_split = if content[split...split + 4] == "\r\n\r\n"
split + 4
else
split + 2
end
body = content[body_split..-1]
return [title, body]
else
[content, '']
end
end
end
class Tag
attr_reader :uuid
def initialize(name)
@uuid = SecureRandom.uuid
@name = name
@references = []
end
def add_reference(note_uuid)
@references << note_uuid
end
def as_json
{
uuid: @uuid,
content_type: 'Tag',
content: {
title: @name,
references: @references.map do |ref|
{
content_type: 'Note',
uuid: ref
}
end
}
}
end
end
class Tags
def initialize
@tags = {}
import_tag_name = "#{Date.today.to_s}-import"
@import_tag = Tag.new(import_tag_name)
@tags[import_tag_name] = @import_tag
end
def from_note(note)
@import_tag.add_reference(note.uuid)
note.tags.each do |tag_name|
@tags[tag_name] = Tag.new(tag_name) unless @tags[tag_name]
@tags[tag_name].add_reference(note.uuid)
end
end
def as_json
@tags.values.map(&:as_json)
end
end
def main
tags = Tags.new
notes = []
trashed_notes = []
import_json = JSON.parse(File.read('notes.json', encoding: 'UTF-8'))
import_json["activeNotes"].each do |note_json|
note = Note.new(note_json)
tags.from_note(note)
notes << note.as_json
end
# If you'd like to import deleted notes, take a look at export_json["trashedNotes"]
output_json = JSON.pretty_generate({
items: tags.as_json + notes + trashed_notes
})
File.write('standardified-notes.json', output_json, encoding: 'UTF-8')
puts
puts "standardified-notes.json generated with #{notes.count} notes."
end
main() if __FILE__ == $0