forked from qiskit-community/qiskit-textbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostprocess_html.py
33 lines (30 loc) · 1.11 KB
/
postprocess_html.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
# This script santises the sympy latex in the HTML files prior to building the site
import os
import sys
from bs4 import BeautifulSoup
def sanitize_latex(filepath):
'''
Replace {{ with { { inside LaTeX outputs not to confuse Jekyll about what
should be interpolated.
'''
is_modified = False
soup = BeautifulSoup(open(filepath), 'html.parser')
for latex in soup.find_all('div', {'class': 'output_latex'}):
if r'{{' in latex.string:
sanitized_latex = latex.string.replace(r'{{', '{ {')
latex.string = sanitized_latex
is_modified = True
if is_modified:
print(f'sanitize_latex: `{filepath}`')
with open(filepath, 'w') as f:
f.write(str(soup))
if __name__ == '__main__':
if len(sys.argv) < 2:
sys.exit('Usage: python3 postprocess_html.py <build-dir>')
base_dir = sys.argv[1]
for (dirpath, _, filenames) in os.walk(base_dir):
for name in filenames:
if not name.endswith('.html'):
continue
filepath = os.path.join(dirpath, name)
sanitize_latex(filepath)