forked from Phayax/dance-cards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
162 lines (128 loc) · 5.23 KB
/
split.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import subprocess
import shutil
from pathlib import Path
from multiprocessing import Pool
#from tqdm import tqdm
#from dances import DANCES
from tqdm import tqdm
#PATCH = "% CONTENT HERE"
BASE_TEX_FILE = "./cards.tex"
TEMP_DIR = "./split/"
def sanitize_filenames(name):
name = name.replace("\\", "")
name = name.replace("/", "")
name = name.replace("\"", "")
name = name.replace("'", "")
name = name.replace(r"{", "")
name = name.replace(r"}", "")
name = name.replace(" ", "_")
name = name.replace("normalsize", "")
name = name.replace("scriptsize", "")
name = name.replace("nicefrac", "")
return name
def dance_contains_tex_code(content: str) -> bool:
# check if it is the last empty page:
# in that case return false
if r"\ClearShipoutPictureBG" in content:
return False
lines = content.split("\n")
lines = [line.strip() for line in lines]
lines = [line for line in lines if not line.startswith("%")]
lines = [line for line in lines if line != ""]
if len(lines) > 0:
return True
else:
return False
def extract_filename(dance):
# remove leading and trailing whitespaces so we can easily search
# for the first line break
dance = dance.strip("\n")
ret = dance.find(r"\dancename")
if ret >= 0:
# searching for the open bracket of \dancename>{<
start = dance.find(r"{", ret) + 1
# searching for the end of the line
end = dance.find("\n", ret)
raw_name = dance[start:end]
name = sanitize_filenames(raw_name)
return name + ".tex"
else:
raise ValueError("No dancename found in: \n" + dance)
def patch_tex_content(base_tex_file, target_tex_file, content):
with open(base_tex_file, "r") as f:
file_content = f.readlines()
file_content = [line for line in file_content if not line.strip().startswith("%")]
# join the lines (keeping newlines)
file_content = "".join(file_content)
ret = file_content.find(PATCH)
print(ret, file_content[ret-20:ret+20])
file_content = file_content[:ret] + content + file_content[ret + len(PATCH):]
file_content = file_content
# the encoding here is important because windows is annoying
with open(target_tex_file, "w", encoding="utf-8") as f:
f.write(file_content)
def split_file(base_tex_file: Path, target_file_path: Path):
with open(base_tex_file, "r", encoding="utf-8") as f:
file_content = f.readlines()
file_content = [line for line in file_content if not line.strip().startswith(r"%")]
# ignore lines that specify the compilespeed option
# \fastrue or \fastfalse
file_content = [line for line in file_content if not line.strip().startswith(r"\fast")]
# join the lines (keeping newlines)
file_content = "".join(file_content)
header, content = file_content.split(r"\begin{document}")
header += r"\begin{document}"
dance_content, end = content.split(r"\end{document}")
end = "\n" + r"\end{document}" + end
dances = dance_content.split(r"\newpage")
for idx, dance in enumerate(dances):
# first check if there is content once all spaces and comments are removed:
if not dance_contains_tex_code(dance):
continue
name = extract_filename(dance)
# add an index so we know in which order they appear in the main document.
dance_file = target_file_path / f"{idx:03d}_{name}"
with open(dance_file, "w", encoding="utf-8") as f:
dance_text = header + dance + end
f.write(dance_text)
print(f"\t{dance_file.name}")
def compile_dance(filename, working_dir):
ret = subprocess.run(["latexmk", filename], cwd=working_dir)
return ret
def compile_dances_parallel():
dance_list = [file for file in p.iterdir() if file.is_file and file.suffix == ".tex"]
pool = Pool()
joblist = []
for file in dance_list:
pool.apply_async(compile_dance, )
def compile_dance_serial():
dance_list = [file for file in p.iterdir() if file.is_file and file.suffix == ".tex"]
for file in (pbar := tqdm(sorted(dance_list))):
pbar.set_postfix_str("%s" % file.name)
ret = subprocess.run(["latexmk", file.name], cwd=p, capture_output=True)
if ret.returncode == 0:
pdf_path = p / ".build" / f"{file.stem}.pdf"
shutil.copy(pdf_path, single_pdf_target / pdf_path.name)
else:
print(f"Warning latexmk returned with non-zero Returncode {ret.returncode} on file {file}!")
# once we're done remove all files from build dir
#for file in p.iterdir():
# file.unlink()
if __name__ == '__main__':
# remove potential old build data
if Path(TEMP_DIR).exists():
shutil.rmtree(Path(TEMP_DIR))
print("="*60)
print("Splitting tex file.")
print("Generated files:")
print("-"*30)
# create build directory
p = Path(TEMP_DIR)
p.mkdir(exist_ok=False)
shutil.copy(".latexmkrc", str(p / ".latexmkrc"))
shutil.copytree(Path("./img"), p / "img")
split_file(base_tex_file=Path(BASE_TEX_FILE), target_file_path=p)
single_pdf_target = Path("./single/")
single_pdf_target.mkdir(exist_ok=True, parents=False)
print("="*60)
# then call latexmk on build-single-temp