forked from malcolmw/tex2docx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_fragile.py
27 lines (22 loc) · 856 Bytes
/
make_fragile.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
import glob
import os
import re
regex_keep = (r'\\protect\{(.*?\})\}',)
dir_in = '/Users/malcolmwhite/Desktop/White_et_al_2019a_rev1'
dir_out = '/Users/malcolmwhite/Desktop/White_et_al_2019a_fragile'
def process_file(file, dir_out):
print(f'Processing {file}')
file_basename = os.path.split(file)[1]
with open(file) as infile:
data = infile.read()
# Keep sections
for pattern in regex_keep:
match = re.search(pattern, data)
while match:
print(f'Replacing "{match.group()}" with "{match.groups()[0]}"')
data = data.replace(match.group(), match.groups()[0])
match = re.search(pattern, data)
with open(os.path.join(dir_out, file_basename), 'w') as outfile:
outfile.write(data)
for file in glob.glob(os.path.join(dir_in, '*.tex')):
process_file(file, dir_out)