forked from JarnoRFB/thesis-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacronyms2glossaries.py
executable file
·42 lines (36 loc) · 1.26 KB
/
acronyms2glossaries.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
"""
This script takes a file with acronyms in the form of
"Coordinated Universal Time (UTC)"
and prints them into the format for the LaTeX package gloassaries
"\newacronym{utc}{UTC}{Coordinated Universal Time}".
Usage:
run $ python acronyms2glossaries <file_with_acronyms>
in your shell. If no file is supplied, acornyms.txt is assumed as
default.
"""
import sys
import re
try:
filename = sys.argv[1]
except IndexError:
filename = 'acronyms.txt'
glossary_entries = []
glossaries_format = '\\newacronym{{{}}}{{{}}}{{{}}}'
acronym_regex = re.compile('(.+?)\s\((.+)\)')
with open(filename, 'r') as acronym_file:
for line in acronym_file:
# Search the line for the acronym and its abbreviation.
parsed = acronym_regex.match(line)
if parsed:
description = parsed.group(1)
abbreviation = parsed.group(2)
glossary_entry = glossaries_format.format(
abbreviation.lower(),
abbreviation,
description,
)
glossary_entries.append(glossary_entry)
else:
raise ValueError("%s is malformed" % line)
for entry in glossary_entries:
print(entry)