-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEPRECATED_evaluate_boolean_toggles.py
117 lines (94 loc) · 3.97 KB
/
DEPRECATED_evaluate_boolean_toggles.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
import glob
import re
import sys
"""
Pandoc doesn't interpret "\iftoggle{}"
so this script finds the definition in main.tex
and then rewrites .tex files use the definitions
To define a toggle,
\newtoggle{name}
to use the toggle,
\togglefalse{name}
and the use looks like
\iftoggle{name}{<true>}{<false>}
"""
arguments = sys.argv[1:]
count = len(arguments)
if count==0:
print("ERROR")
print("requires 1 argument:")
print(" latex/main.tex")
print("example:")
print("evaluate_boolean_toggles.py latex/main.tex")
exit()
if count>1:
print("only takes one argument")
folder_containing_main = '/'.join(arguments[0].split('/')[:-1])
file_name_for_main = arguments[0].split('/')[-1]
print("folder:",folder_containing_main)
print(".tex file:", file_name_for_main)
main_file = folder_containing_main+"/"+file_name_for_main
with open(main_file,'r') as file_handle:
file_content_as_list = file_handle.readlines()
list_of_toggles=[]
for file_line in file_content_as_list:
if file_line.strip().startswith("%"):
continue # go to next iteration in loop
if file_line.strip().startswith("\\newtoggle"):
list_of_toggles.append(file_line.strip().replace("\\newtoggle{","").replace("}",""))
toggle_values={}
for toggle in list_of_toggles:
#print("current toggle: ",toggle)
for file_line in file_content_as_list:
if file_line.strip().startswith("%"):
continue # go to next iteration in loop
if ("{"+toggle+"}" in file_line) and (file_line.startswith("\\toggle")):
boolean_value_of_toggle=file_line.strip().replace("{"+toggle+"}","").replace("\\toggle","")
if boolean_value_of_toggle=="false":
toggle_values[toggle]=False
elif boolean_value_of_toggle=="true":
toggle_values[toggle]=True
#print(toggle_values)
list_of_files = glob.glob(folder_containing_main+"/*.tex")
list_of_replacements = []
#print("main_file=",main_file)
for toggle_name, toggle_bool_value in toggle_values.items():
#print("toggle=",toggle_name,"value=",toggle_bool_value)
for file_name in list_of_files:
if file_name==main_file:
continue # go to next iteration in loop
#print(file_name)
with open(file_name,'r') as file_handle:
file_content_as_list = file_handle.readlines()
for file_line in file_content_as_list:
if file_line.strip().startswith("%"):
continue # go to next iteration in loop
if toggle_name in file_line:
#print(file_line.strip()) # \iftoggle{showminitoc}{\minitoc}{}
up_to_choices = file_line.strip().replace("\iftoggle{"+toggle_name+"}{","")
#print(" up_to_choices=",up_to_choices)
if_true = up_to_choices.split("}{")[0].strip()
if_false = up_to_choices.split("}{")[1][:-1].strip() # always drop the last character, }
#print(" if true:", if_true)
#print(" if false:", if_false)
#print(" replace")
#print("\iftoggle{"+toggle_name+"}{"+if_true+"}{"+if_false+"}")
#print(" with")
if toggle_bool_value:
#print(if_true)
replace_with=if_true
else:
#print(if_false)
replace_with=if_false
list_of_replacements.append(
{"file name": file_name,
"to replace": "\iftoggle{"+toggle_name+"}{"+if_true+"}{"+if_false+"}",
"replace with": replace_with})
for replacement_dict in list_of_replacements:
print(replacement_dict)
with open(replacement_dict["file name"], 'r') as file_handle:
file_content = file_handle.read()
revised=file_content.replace(replacement_dict["to replace"],replacement_dict["replace with"])
with open(replacement_dict["file name"], 'w') as file_handle:
file_handle.write(revised)