-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_gloss.py
executable file
·65 lines (55 loc) · 1.79 KB
/
create_gloss.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
#!/usr/bin/python3
"""
Glossary Page Builder: Takes a text file list of key subject terms
and their definitions (tab-delimited) and builds the glossary
list as an HTML file. Has internal tags the key terms will be linked to.
"""
import argparse
from collections import OrderedDict
# no include tag until we solve missing include file problem
from pylib.html_tags import ulist, str_to_valid_id, include_tag
from pylib.misc import filenm_from_key
ARG_ERROR = 1 # type: int
IO_ERROR = 2 # type: int
exit_error = False # type: bool
file_nm = None
INDENT1 = " " # type: str
INDENT2 = INDENT1 + INDENT1 # type: str
INDENT3 = INDENT2 + INDENT1 # type: str
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("txt_file", help="text file to be parsed")
args = arg_parser.parse_args()
txt_file = args.txt_file
d = OrderedDict() # type Dict[str]
try:
with open(txt_file, 'r') as f:
line_no = 1
try:
# place terms/defs in dictionary
for line in f:
term = line.strip().split("\t") # tab delimited
d[term[0]] = term[1]
line_no += 1
except IndexError:
print("Index error: check line " + str(line_no))
except IOError:
print("Couldn't read " + txt_file)
exit(IO_ERROR)
gloss_list = []
for key in d:
key_id = str_to_valid_id(key)
gloss_item = '<span class="hilight" id="'
gloss_item += key_id + '">' + key + '</span>: '
gloss_item += d[key]
gloss_item += "<br />\n"
file_name = filenm_from_key(key)
gloss_item += include_tag(file_name + ".txt")
gloss_list.append(gloss_item)
s = ulist(css_class="nested", l=gloss_list) # noqa E741
# write to standard out:
print(s)
if exit_error:
exit(ARG_ERROR)
else:
exit(0)