forked from dunhamsteve/notesutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes2html4notion
executable file
·363 lines (331 loc) · 12.4 KB
/
notes2html4notion
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
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import os
import sqlite3
import json
import struct
import re
import zipfile
import sys
from zlib import decompress
import xml.etree.ElementTree as ET
def append(rval, a):
if isinstance(a, str):
i = len(rval)-1
if i < 0:
rval.text = (rval.text or "")+a
else:
rval[i].tail = (rval[i].tail or "")+a
elif isinstance(a, ET.Element):
rval.append(a)
elif isinstance(a, dict):
rval.attrib.update(a)
else:
raise Exception(f"unhandled type {type(a)}")
return a
def E(tag, *args):
tag, *cc = tag.split('.')
rval = ET.Element(tag)
tail = None
if cc:
rval.set('class', ' '.join(cc))
for a in args:
append(rval, a)
return rval
def uvarint(data, pos):
x = s = 0
while True:
b = data[pos]
pos += 1
x = x | ((b & 0x7f) << s)
if b < 0x80:
return x, pos
s += 7
def readbytes(data, pos):
l, pos = uvarint(data, pos)
return data[pos:pos+l], pos+l
def readstruct(fmt, l):
return lambda data, pos: (struct.unpack_from(fmt, data, pos)[0], pos+l)
readers = [uvarint, readstruct('<d', 8), readbytes, None, None, readstruct('<f', 4)]
# parses a protobuf
def parse(data, schema):
obj = {}
pos = 0
while pos < len(data):
val, pos = uvarint(data, pos)
typ = val & 7
key = val >> 3
val, pos = readers[typ](data, pos)
if key not in schema:
continue
name, repeated, typ = schema[key]
if isinstance(typ, dict):
val = parse(val, typ)
if typ == 'string':
val = val.decode('utf8')
if repeated:
val = obj.get(name, []) + [val]
obj[name] = val
return obj
# Convert note drawing to SVG
def svg(drawing):
width = drawing['bounds']['width']
height = drawing['bounds']['height']
rval = E('svg', {'width': str(width), 'height': str(height)})
inks = drawing.get('inks')
for stroke in drawing.get('strokes', []):
if stroke.get('hidden'):
continue
if 'points' in stroke:
swidth = 1
ink = inks[stroke['inkIndex']]
c = ink['color']
red = int(c['red']*255)
green = int(c['green']*255)
blue = int(c['blue']*255)
alpha = c['alpha']
if ink['identifier'] == 'com.apple.ink.marker':
swidth = 15
alpha = 0.5
color = f'rgba({red},{green},{blue},{alpha})'
path = ''
for _, x, y, *rest in struct.iter_unpack('<3f5H2B', stroke['points']):
path += f"L{x:.2f} {y:.2f}"
path = "M"+path[1:]
rval.append(E('path', {'d': "M"+path[1:], 'stroke': color, 'stroke-width': str(swidth), 'stroke-cap': 'round', 'fill': 'none'}))
if 'transform' in stroke:
rval[-1].set('transform', "matrix({a} {b} {c} {d} {tx:.2f} {ty:.2f})".format(**stroke['transform']))
return rval
def render_html(note, attachments={}):
if note is None:
return ""
styles = {0: 'h1', 1: 'h2', 4: 'pre', 100: 'li', 101: 'li', 102: 'li', 103: 'li'}
rval = E('div')
txt = note['string']
pos = 0
par = None
for run in note.get('attributeRun', []):
l = run['length']
for frag in re.findall(r'\n|[^\n]+', txt[pos:pos+l]):
if par is None: # start paragraph
pstyle = run.get('paragraphStyle', {}).get('style', -1)
indent = run.get('paragraphStyle', {}).get('indent', 0)
if pstyle > 100: # this mess handles merging todo lists
tag = ['ul', 'ul', 'ol', 'ul'][pstyle - 100]
par = rval
while indent > 0:
last = list(par)[-1]
if last.tag != tag:
break
par = last
indent -= 1
while indent >= 0:
par = append(par, E(tag))
indent -= 1
par = append(par, E('li'))
elif pstyle == 4 and rval.getchildren()[-1].tag == 'pre':
par = rval.getchildren()[-1]
append(par, "\n")
else:
par = append(rval, E(styles.get(pstyle, 'p')))
if pstyle == 103:
par.append(E('input', {"type": "checkbox"}))
if run.get('todo', {}).get('done'):
par[0].put('checked', '')
if frag == '\n':
par = None
else:
link = run.get('link')
info = run.get('attachmentInfo')
style = run.get('fontHints', 0) + 4*run.get('underline', 0) + 8*run.get('strikethrough', 0)
if style & 1:
frag = E('b', frag)
if style & 2:
frag = E('em', frag)
if style & 4:
frag = E('u', frag)
if style & 8:
frag = E('strike', frag)
if info:
attach = attachments.get(info.get('attachmentIdentifier'))
if attach and attach.get('html') is not None:
frag = attach.get('html')
append(par, frag)
pos += l
return rval
# Decode a 'CRArchive'
def process_archive(table):
objects = []
def dodict(v):
rval = {}
for e in v.get('element', []):
rval[coerce(e['key'])] = coerce(e['value'])
return rval
def coerce(o):
[(k, v)] = o.items()
if 'custom' == k:
rval = dict((table['keyItem'][e['key']], coerce(e['value'])) for e in v['mapEntry'])
typ = table['typeItem'][v['type']]
if typ == 'com.apple.CRDT.NSUUID':
return table['uuidItem'][rval['UUIDIndex']]
if typ == 'com.apple.CRDT.NSString':
return rval['self']
return rval
if k == 'objectIndex':
return coerce(table['object'][v])
if k == 'registerLatest':
return coerce(v['contents'])
if k == 'orderedSet':
elements = dodict(v['elements'])
contents = dodict(v['ordering']['contents'])
rval = []
for a in v['ordering']['array']['attachments']:
value = contents[a['uuid']]
if value not in rval and a['uuid'] in elements:
rval.append(value)
return rval
if k == 'dictionary':
return dodict(v)
if k in ('stringValue', 'unsignedIntegerValue', 'string'):
return v
raise Exception(f"unhandled type {k}")
return coerce(table['object'][0])
# Render a table to html
def render_table(table):
table = process_archive(table)
rval = E('table')
for row in table['crRows']:
tr = E('tr')
rval.append(tr)
for col in table['crColumns']:
cell = table.get('cellColumns').get(col, {}).get(row)
td = E('td', render_html(cell))
rval.append(td)
return rval
s_string = {
2: ["string", 0, "string"],
5: ["attributeRun", 1, {
1: ["length", 0, 0],
2: ["paragraphStyle", 0, {
1: ["style", 0, 0],
4: ["indent", 0, 0],
5: ["todo", 0, {
1: ["todoUUID", 0, "bytes"],
2: ["done", 0, 0]
}]
}],
5: ["fontHints", 0, 0],
6: ["underline", 0, 0],
7: ["strikethrough", 0, 0],
9: ["link", 0, "string"],
12: ["attachmentInfo", 0, {
1: ["attachmentIdentifier", 0, "string"],
2: ["typeUTI", 0, "string"]
}]
}]
}
s_doc = {2: ["version", 1, {3: ["data", 0, s_string]}]}
s_drawing = {2: ["version", 1, {3: ["data", 0, {
4: ["inks", 1, {
1: ["color", 0, {1: ["red", 0, 0], 2:["green", 0, 0], 3:["blue", 0, 0], 4:["alpha", 0, 0]}],
2:["identifier", 0, "string"]
}],
5: ["strokes", 1, {
3: ["inkIndex", 0, 0],
5:["points", 0, "bytes"],
9:["hidden", 0, 0],
10: ["transform", 0, {1: ["a", 0, 0], 2:["b", 0, 0], 3:["c", 0, 0], 4:["d", 0, 0], 5:["tx", 0, 0], 6:["ty", 0, 0]}]
}],
8: ["bounds", 0, {1: ["originX", 0, 0], 2:["originY", 0, 0], 3:["width", 0, 0], 4:["height", 0, 0]}]
}]
}
]}
# this essentially is a variant type
s_oid = {2: ["unsignedIntegerValue", 0, 0], 4: ["stringValue", 0, 'string'], 6: ["objectIndex", 0, 0]}
s_dictionary = {1: ["element", 1, {1: ["key", 0, s_oid], 2:["value", 0, s_oid]}]}
s_table = {2: ["version", 1, {3: ["data", 0, {
3: ["object", 1, {
1: ["registerLatest", 0, {2: ["contents", 0, s_oid]}],
6:["dictionary", 0, s_dictionary],
10:["string", 0, s_string],
13:["custom", 0, {
1: ["type", 0, 0],
3:["mapEntry", 1, {
1: ["key", 0, 0],
2:["value", 0, s_oid]
}]
}],
16:["orderedSet", 0, {
1: ["ordering", 0, {
1: ["array", 0, {
1: ["contents", 0, s_string],
2:["attachments", 1, {1: ["index", 0, 0], 2:["uuid", 0, 0]}]
}],
2:["contents", 0, s_dictionary]
}],
2: ["elements", 0, s_dictionary]
}]
}],
4:["keyItem", 1, "string"],
5:["typeItem", 1, "string"],
6:["uuidItem", 1, "bytes"]
}]}]}
def write(data, *path):
path = os.path.join(*path)
os.makedirs(os.path.dirname(path), exist_ok=True)
open(path, 'wb').write(data)
if __name__ == '__main__':
css = '''
.underline { text-decoration: underline; }
.strikethrough { text-decoration: line-through; }
.todo { list-style-type: none; margin-left: -20px; }
.dashitem { list-style-type: none; }
.dashitem:before { content: "-"; text-indent: -5px }
'''
dest = 'notes'
if len(sys.argv) > 1:
dest = sys.argv[1]
root = os.path.expanduser("~/Library/Group Containers/group.com.apple.notes")
dbpath = os.path.join(root, 'NoteStore.sqlite')
write(open(dbpath, 'rb').read(), dest, 'NoteStore.sqlite')
db = sqlite3.Connection(dbpath)
# process attachments first
attachments = {}
mquery = '''select a.zidentifier, a.zmergeabledata, a.ztypeuti, b.zidentifier, b.zfilename, a.zurlstring,a.ztitle
from ziccloudsyncingobject a left join ziccloudsyncingobject b on a.zmedia = b.z_pk
where a.zcryptotag is null and a.ztypeuti is not null'''
for id, data, typ, id2, fname, url, title in db.execute(mquery):
if typ == 'com.apple.drawing' and data:
doc = parse(decompress(data, 47), s_drawing)
attachments[id] = {'html': svg(doc['version'][0]['data'])}
elif typ == 'com.apple.notes.table' and data:
doc = parse(decompress(data, 47), s_table)
attachments[id] = {'html': render_table(doc['version'][0]['data'])}
elif typ == 'public.url':
# there is a preview image somewhere too, but not sure I care
attachments[id] = {'html': E('a', {'href': url}, title)}
elif fname:
fn = os.path.join('Media', id2, fname)
if typ in ['public.tiff', 'public.jpeg', 'public.png']:
attachments[id] = {'html': E('img', {'src': fn})}
else:
attachments[id] = {'html': E('a', {'href': fn}, fname)}
src = os.path.join(root, fn)
if os.path.exists(src):
write(open(src, 'rb').read(), dest, fn)
else:
fn = os.path.join('FallbackImages', id+'.jpg')
src = os.path.join(root, fn)
if os.path.exists(src):
attachments[id] = {'html': E('img', {'src': fn})}
write(open(src, 'rb').read(), dest, fn)
nquery = '''select a.zidentifier, n.zdata from zicnotedata n join ziccloudsyncingobject a on a.znotedata = n.z_pk
where n.zcryptotag is null and zdata is not null'''
for id, data in db.execute(nquery):
pb = decompress(data, 47)
doc = parse(pb, s_doc)['version'][0]['data']
title = doc['string'].split('\n')[0].replace('/', '|')
section = render_html(doc, attachments)
section.tag = 'section'
hdoc = E('html', E('head', E('style', css)), E('body', section))
write(ET.tostring(hdoc, method='html'), dest, f'{title}.html')
print(f"wrote files to {dest} directory")