forked from ascholerChemeketa/ThinkCPP2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixlabels.py
More file actions
50 lines (46 loc) · 1.72 KB
/
fixlabels.py
File metadata and controls
50 lines (46 loc) · 1.72 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
import os
import re
import shutil
def makeLabel(name):
name = name.lower()
name = name.replace("and ", "")
name = name.replace("the ", "")
name = name.replace("in ", "")
name = name.replace("of ", "")
name = name.replace("an ", "")
name = re.sub(r"\s{2,}", " ", name)
name = re.sub(r"[^\w\s]+", "", name)
name = name.replace(" ", "-")
return name
for item in os.listdir('.'):
if os.path.isfile(os.path.join(".", item)):
file = os.path.join(".", item)
if ".tex" not in file:
continue
if not (re.search(r'ch\d', file) or re.search(r'app\w', file)):
continue
print(file)
shutil.copy2(file, os.path.join("backups", file))
outLines = []
with open(file, 'r') as f:
lines = f.readlines()
curChapter = ""
for i, line in enumerate(lines):
outLines.append(line)
if re.search(r'chapter\*?{', line):
title = re.search(r'chapter\*?{(.+?)}', line).group(1)
print(" chapter " + title)
label = makeLabel(title)
curChapter = label
print(" label " + label)
outLines.append("\\label{" + label + "}\n")
if re.search(r'section\*?{', line):
title = re.search(r'section\*?{(.+?)}', line).group(1)
print(" section " + title)
label = curChapter + "_" + makeLabel(title)
print(" label " + label)
outLines.append("\\label{" + label + "}\n")
text = "".join(outLines)
# print(text)
with open(file, 'w') as f:
f.write(text)