-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreplace_string.py
More file actions
100 lines (84 loc) · 3.39 KB
/
replace_string.py
File metadata and controls
100 lines (84 loc) · 3.39 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
"""
Find and replace string in the StatProofBook
_
This script goes through all proofs and definitions and
replaces a string str1 with another string str2.
Author: Joram Soch, BCCN Berlin
E-Mail: joram.soch@bccn-berlin.de
First edit: 2020-04-14 17:48:00
Last edit: 2020-10-08 08:18:00
"""
# Import modules
#-----------------------------------------------------------------------------#
import os
import re
import BookTools as spbt
# Define settings
#-----------------------------------------------------------------------------#
str1r= '$n \\times 1$' # should be identical to str1, unless str1
str1 = '$n \\times 1$' # contains regexp-problematic characters
str2 = '$n$-dimensional'
# Set repository directory
#-----------------------------------------------------------------------------#
rep_dir = spbt.get_rep_dir('offline')
# Start replacement
#-----------------------------------------------------------------------------#
print('\n-> Replace "' + str1 + '" by "' + str2 + '":')
file_found = False
# Browse through proofs
#-----------------------------------------------------------------------------#
files = os.listdir(rep_dir + '/P/')
for file in files:
if '.md' in file:
# Read proof file
#---------------------------------------------------------------------#
file_obj = open(rep_dir + '/P/' + file, 'r')
file_txt = file_obj.readlines()
file_obj.close()
# Search proof text
#---------------------------------------------------------------------#
str_found = False
for i, line in enumerate(file_txt):
if line.find(str1) > -1:
str_found = True
file_txt[i] = re.sub(re.escape(str1r), str2, line)
# Write proof file
#---------------------------------------------------------------------#
if str_found:
print(' - in "/P/' + file + '"')
file_new = open(rep_dir + '/P/' + file, 'w')
for line in file_txt:
file_new.write(line)
file_new.close()
file_found = True
# Browse through definitions
#-----------------------------------------------------------------------------#
files = os.listdir(rep_dir + '/D/')
for file in files:
if '.md' in file:
# Read definition file
#---------------------------------------------------------------------#
file_obj = open(rep_dir + '/D/' + file, 'r')
file_txt = file_obj.readlines()
file_obj.close()
# Search definition text
#---------------------------------------------------------------------#
str_found = False
for i, line in enumerate(file_txt):
if line.find(str1) > -1:
str_found = True
file_txt[i] = re.sub(re.escape(str1r), str2, line)
# Write definition file
#---------------------------------------------------------------------#
if str_found:
print(' - in "/D/' + file + '"')
file_new = open(rep_dir + '/D/' + file, 'w')
for line in file_txt:
file_new.write(line)
file_new.close()
file_found = True
# Finalize replacement
#-----------------------------------------------------------------------------#
if not file_found:
print(' - not found in files.')