-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·58 lines (46 loc) · 1.49 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Usage: main.py ANNOTATION_FILE
Convert annotation file to Markdown
Arguments:
ANNOTATION_FILE File to import
Options:
-h --help
"""
from docopt import docopt
import csv
import os
if __name__ == '__main__':
arguments = docopt(__doc__)
annotation_file = arguments['ANNOTATION_FILE']
filename = os.path.basename(arguments['ANNOTATION_FILE'])
raw_filename = filename.split('.')[0]
print("Converting {0}".format(filename))
# Create exports folder if doesn't exist
folder_name = 'exports'
if not os.path.exists(folder_name):
os.makedirs(folder_name)
annotations = []
# Import
with open(annotation_file) as file:
# Check if CSV has a header
has_header = csv.Sniffer().has_header(file.read(1024))
# Rewind file
file.seek(0)
reader = csv.reader(file, delimiter=';')
if has_header:
next(reader)
for row in reader:
annotation = {}
annotation['page'] = row[3]
annotation['quote'] = row[4]
annotations.append(annotation)
# Export
export_location = '{0}.md'.format(raw_filename)
export_file = open(folder_name + '/' + export_location, 'w')
for annotation in annotations:
quote = annotation['quote']
page = annotation['page']
quote = quote.strip().replace('\n', '\n> ')
export_file.write("> {0} *(p.{1})*\n\n".format(quote, page))
export_file.close()