-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
235 lines (184 loc) · 7.15 KB
/
manage.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import click
import re
import shutil
from pathlib import Path
import subprocess as sub
import platform
import fileinput
import configparser
def has_changed_since_last_publish(file_name: str) -> bool:
src = "raws"
dst = "web/articles"
src_file_md = Path(src) / (file_name + ".md")
src_file_nb = Path(src) / (file_name + ".ipynb")
dst_file = Path(dst) / (file_name + ".md")
# Check if the file exists in `dst`
if not dst_file.exists():
return True
# Compare modification time
if src_file_md.exists():
return src_file_md.stat().st_mtime > dst_file.stat().st_mtime
elif src_file_nb.exists():
return src_file_nb.stat().st_mtime > dst_file.stat().st_mtime
else:
raise FileNotFoundError(f"{file_name} does not exist in {src}")
@click.command()
@click.option("--file_path", type=str, default=None)
def main(file_path: str):
file = Path(file_path)
if file.is_file():
config = configparser.ConfigParser()
config.read("config.ini")
file_name = file.stem
if not has_changed_since_last_publish(file_name):
print(f"Skipping {file} (article is up-to-date).")
return
else:
print(f"Processing {file}...")
if file.suffix == ".md":
# Copy the article as is
shutil.copy(file_path, f"{config['Path']['articles']}/{file_name}.md")
# Write the brief
with open(
f"{config['Path']['briefs']}/{file_name}.md", "w", encoding="utf-8"
) as f:
f.write(get_first_400_characters(file))
f.write("...\n")
elif file.suffix == ".ipynb":
if platform.system() == "Windows":
python = "py"
elif platform.system() == "Linux":
python = "python3"
# Run black on the notebook
print(" - Running black on {file}")
sub.run([python, "-m", "black", file])
# Retarget file to the temporary (Markdown) one
file = Path(f"{config['Path']['raws']}/tmp/{file_name}.md")
# Prepare the temporary markdown file
sub.run(
[
python,
"-m",
"nbconvert",
"--to",
"markdown",
f"--output-dir={config['Path']['raws']}/tmp",
f"{config['Path']['raws']}/{file_name}.ipynb",
]
)
# Copy the article as is
shutil.copy(file, f"{config['Path']['articles']}/{file_name}.md")
# Write the brief
with open(
f"{config['Path']['briefs']}/{file_name}.md", "w", encoding="utf-8"
) as f:
f.write(get_first_400_characters(file))
f.write("...\n")
# Erase the previously generated figures
remove_files_with_pattern(config["Path"]["media"], f"{file_name}_*.png")
# Copy all temporary figures to the media directory
png_files = Path(f"{config['Path']['raws']}/tmp/{file_name}_files").glob(
"*.png"
)
for f in png_files:
shutil.copy(f, Path(config["Path"]["media"]))
# Delete the temporary files
shutil.rmtree(f"{config['Path']['raws']}/tmp")
# Update the temporary media links
retarget_temporary_media_files(
Path(f"{config['Path']['articles']}/{file_name}.md")
)
# Copy all non-temporary figures to the media directory
png_files = Path(f"{config['Path']['raws']}/media").glob(f"{file_name}_*.png")
for f in png_files:
shutil.copy(f, Path(config["Path"]["media"]))
# Update the non-temporary media links
retarget_non_temporary_media_files(
Path(f"{config['Path']['articles']}/{file_name}.md")
)
latex_to_image(Path(f"{config['Path']['articles']}/{file_name}.md"), config)
def remove_files_with_pattern(directory_path, pattern):
directory = Path(directory_path)
for file in directory.glob(pattern):
file.unlink()
def retarget_temporary_media_files(path: Path):
print(f" - Retargetting temporary media files for {path}")
with fileinput.FileInput(path, inplace=True) as file:
for line in file:
# Nbconvert generates images with 
print(
line.replace(
f",
end="",
)
def retarget_non_temporary_media_files(path: Path):
print(f" - Retargetting non-temporary media files for {path}")
with fileinput.FileInput(path, inplace=True) as file:
for line in file:
if ",
end="",
)
elif ",
end="",
)
else:
print(
line,
end="",
)
def get_first_400_characters(file: Path) -> str:
content = file.read_text()
# Remove hyperlinks
content = re.sub(r"\[(.*?)\]\((.*?)\)", r"\1", content)
# Get the first 400 characters (approximately)
first_400_characters = content[:400]
return first_400_characters
def replace_matches(
match: re.match, counter: int, filename: str, config: configparser.ConfigParser
) -> str:
tex = match.strip("%%latex\n$")
print(f" - LaTeX string to be replaced with an image: {tex}")
# Image filename
img_filename = f"{filename}_latex_{counter:02d}"
# Generate the image of the LaTeX expression
sub.run(
[
"bash",
"./tex2png.sh",
tex,
img_filename,
]
)
# Move the image to the correct location
Path(f"{config['Path']['media']}/{img_filename}.png").unlink(missing_ok=True)
shutil.move(f"tmp/{img_filename}.png", config["Path"]["media"])
# Change the LaTeX with the image reference for the Markdown file
replacement = f"\n\n\n\n\n\n"
print(f" - LaTeX string replaced with: {replacement}")
return replacement
def latex_to_image(file: Path, config: configparser.ConfigParser):
print(f" - Converting LaTeX to images for file {file}")
content = file.read_text()
pattern = r"%%latex\n\$([^$]+)\$"
matches = re.findall(pattern, content, flags=re.DOTALL)
counter = 0
for match in matches:
replacement = replace_matches(match, counter, file.stem, config)
print(f' - Replacing "%%latex\n${match}$" with {replacement}')
content = content.replace(f"%%latex\n${match}$", replacement, 1)
counter += 1
file.write_text(content)
if __name__ == "__main__":
main()