-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.py
executable file
·114 lines (82 loc) · 2.83 KB
/
export.py
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
#!/usr/bin/env python
import argparse
import org
import re
from datetime import datetime
import shutil
import os
state_file_name = '.last_export.orgexport'
def main():
ap = argparse.ArgumentParser()
ap.add_argument('inputfile')
args = ap.parse_args()
#do not try to import notes from your ebookreader from before 1970
last_export = datetime(year=1970, month=1, day=1)
last_export_lines = []
state_file = os.path.join(os.path.dirname(args.inputfile), state_file_name)
if os.path.exists(state_file):
with open(state_file, 'r') as f:
lines = f.read()
lines = lines.split('\n')
last_export_lines = [l for l in lines if l]
last_export = datetime.strptime(last_export_lines[-1], org.date_format)
with open(args.inputfile, 'r') as inputfile:
text = inputfile.read()
note_sep = '''
-----------------------------------
'''
notes = text.split(note_sep)
note_re = re.compile(r'\n'.join([
r'(?P<title>.+)'
, r'((?P<type>Lesezeichen|Markierung|Notiz)[ ]+auf Seite[ ]+(?P<page>\d+): )'
#note is optional
+ r'''((?P<note>(?:.|\n)*)
)?''' + r'"(?P<quote>(?:.|\n)*)"'
, r'''Hinzugefügt am (?P<day>\d{2}).(?P<month>\d{2}).(?P<year>\d{4}) \| (?P<hour>\d{1,2}):(?P<minute>\d{2})
''']))
types = dict(
Markierung='highlighted'
, Notiz='note'
, Lesezeichen='marker'
)
dont_export = ['marker']
for note in notes:
if not note:
continue
#print('---------"' + note + '"')
m = note_re.match(note)
assert(m)
d = {}
for k in ('year', 'month', 'day', 'hour', 'minute'):
d[k] = int(m.group(k))
created = datetime(**d)
if created < last_export:
continue
book = m.group('title')
page = m.group('page')
typ = types[m.group('type')]
if typ in dont_export:
continue
quote = m.group('quote')
title = quote
title = org.wrap(title, '"')
if typ == 'note':
title = m.group('note')
title.replace('\n', ' ')
n = 20
content = ''
title_words = title.split()
if len(title_words) > n:
#content += '[…] ' + org.wrap(' '.join(title_words[n:])) + '\n\n'
title = ' '.join(title_words[:n])
title += ' […]'
content += '\n#+begin_quote\n' + quote + '\n#+end_quote\n'
content += '\nFrom "' + book + '"' + ', p.' + page + '\n'
s = org.headline(title, content, created, tags=[typ])
print(s)
print('\n')
last_export_lines.append(datetime.now().strftime(org.date_format))
with open(state_file, 'w+') as f:
f.write('\n'.join(last_export_lines))
if __name__ == '__main__':
main()