|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | + |
| 4 | +import org |
| 5 | + |
| 6 | +import re |
| 7 | + |
| 8 | +from datetime import datetime |
| 9 | + |
| 10 | +import shutil |
| 11 | +import os |
| 12 | + |
| 13 | +state_file_name = '.last_export.orgexport' |
| 14 | + |
| 15 | +def main(): |
| 16 | + |
| 17 | + |
| 18 | + ap = argparse.ArgumentParser() |
| 19 | + ap.add_argument('inputfile') |
| 20 | + |
| 21 | + args = ap.parse_args() |
| 22 | + |
| 23 | + #do not try to import notes from your ebookreader from before 1970 |
| 24 | + last_export = datetime(year=1970, month=1, day=1) |
| 25 | + last_export_lines = [] |
| 26 | + state_file = os.path.join(os.path.dirname(args.inputfile), state_file_name) |
| 27 | + |
| 28 | + if os.path.exists(state_file): |
| 29 | + with open(state_file, 'r') as f: |
| 30 | + lines = f.read() |
| 31 | + lines = lines.split('\n') |
| 32 | + last_export_lines = [l for l in lines if l] |
| 33 | + last_export = datetime.strptime(last_export_lines[-1], org.date_format) |
| 34 | + |
| 35 | + with open(args.inputfile, 'r') as inputfile: |
| 36 | + text = inputfile.read() |
| 37 | + |
| 38 | + note_sep = ''' |
| 39 | +----------------------------------- |
| 40 | +
|
| 41 | +''' |
| 42 | + |
| 43 | + notes = text.split(note_sep) |
| 44 | + |
| 45 | + note_re = re.compile(r'\n'.join([ |
| 46 | + r'(?P<title>.+)' |
| 47 | + , r'((?P<type>Lesezeichen|Markierung|Notiz)[ ]+auf Seite[ ]+(?P<page>\d+): )' |
| 48 | + #note is optional |
| 49 | + + r'''((?P<note>(?:.|\n)*) |
| 50 | +)?''' + r'"(?P<quote>(?:.|\n)*)"' |
| 51 | + , 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}) |
| 52 | +'''])) |
| 53 | + |
| 54 | + types = dict( |
| 55 | + Markierung='highlighted' |
| 56 | + , Notiz='note' |
| 57 | + , Lesezeichen='marker' |
| 58 | + ) |
| 59 | + |
| 60 | + dont_export = ['marker'] |
| 61 | + |
| 62 | + for note in notes: |
| 63 | + if not note: |
| 64 | + continue |
| 65 | + #print('---------"' + note + '"') |
| 66 | + m = note_re.match(note) |
| 67 | + |
| 68 | + assert(m) |
| 69 | + |
| 70 | + d = {} |
| 71 | + for k in ('year', 'month', 'day', 'hour', 'minute'): |
| 72 | + d[k] = int(m.group(k)) |
| 73 | + |
| 74 | + created = datetime(**d) |
| 75 | + |
| 76 | + if created < last_export: |
| 77 | + continue |
| 78 | + |
| 79 | + book = m.group('title') |
| 80 | + page = m.group('page') |
| 81 | + typ = types[m.group('type')] |
| 82 | + if typ in dont_export: |
| 83 | + continue |
| 84 | + quote = m.group('quote') |
| 85 | + |
| 86 | + title = quote |
| 87 | + title = org.wrap(title, '"') |
| 88 | + if typ == 'note': |
| 89 | + title = m.group('note') |
| 90 | + |
| 91 | + title.replace('\n', ' ') |
| 92 | + n = 20 |
| 93 | + content = '' |
| 94 | + title_words = title.split() |
| 95 | + if len(title_words) > n: |
| 96 | + #content += '[…] ' + org.wrap(' '.join(title_words[n:])) + '\n\n' |
| 97 | + title = ' '.join(title_words[:n]) |
| 98 | + title += ' […]' |
| 99 | + |
| 100 | + |
| 101 | + content += '\n#+begin_quote\n' + quote + '\n#+end_quote\n' |
| 102 | + |
| 103 | + content += '\nFrom "' + book + '"' + ', p.' + page + '\n' |
| 104 | + |
| 105 | + s = org.headline(title, content, created, tags=[typ]) |
| 106 | + print(s) |
| 107 | + print('\n') |
| 108 | + |
| 109 | + last_export_lines.append(datetime.now().strftime(org.date_format)) |
| 110 | + with open(state_file, 'w+') as f: |
| 111 | + f.write('\n'.join(last_export_lines)) |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + main() |
0 commit comments