-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_reference.py
53 lines (46 loc) · 1.3 KB
/
add_reference.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
#coding: utf-8
"""
Script to add reference which are extracted from the infile
and will append to the bottom of the rst file.
"""
class Reference(object):
def __init__(self, infile):
self.infile = infile
def add_reference(self):
from easy_writing import Helper as LinkHelper
a = LinkHelper()
links = a.extract(self.infile)
append = "\n\n"
i = 1
for link in links:
append += "%s. `%s`_ \n" % (str(i), link)
i += 1
fh = open(self.infile, "r")
cont = fh.read()
fh.close()
cont += append
fh = open(self.infile, "w")
fh.write(cont)
fh.close()
print "Done."
@classmethod
def handle_arg(cls):
import optparse
usage = """\
python add_reference.py [infile]
"""
parser = optparse.OptionParser(usage=usage)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.print_help()
parser.exit(msg="\nNeed ONE argument as the path to handle\n")
return args[0]
if __name__ == "__main__":
# add reference
path = Reference.handle_arg()
a = Reference(path)
a.add_reference()
# add links
from easy_writing import Helper
b = Helper()
b.handle_links(path)