From 5cb0e0c05227a390050091595e361942d58de2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Salvador=20de=20la=20Puente=20Gonz=C3=A1lez?= Date: Wed, 8 Jul 2020 19:33:06 +0200 Subject: [PATCH] Polish postprocess HTML script and update dependencies --- Makefile | 6 ++++-- requirements-dev.txt | 1 + scripts/postprocess_html.py | 33 +++++++++++++++++++++++++++++++++ scripts/process_build.py | 25 ------------------------- 4 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 scripts/postprocess_html.py delete mode 100644 scripts/process_build.py diff --git a/Makefile b/Makefile index 66752b191..a383c4217 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: help book clean serve +BUIILD_DIR := "./_build" + help: @echo "Please use 'make ' where is one of:" @echo " install to install the necessary dependencies for jupyter-book to build" @@ -16,7 +18,7 @@ install: book: jupyter-book build ./ - python3 scripts/process_build.py + python3 scripts/postprocess_html.py $(BUIILD_DIR) runall: jupyter-book run ./content @@ -29,7 +31,7 @@ serve: build: jupyter-book build ./ --overwrite - python3 scripts/process_build.py + python3 scripts/postprocess_html.py $(BUIILD_DIR) site: build bundle exec jekyll build diff --git a/requirements-dev.txt b/requirements-dev.txt index 462cac059..4f2ee25ec 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1 +1,2 @@ jupyter-book==0.6.5 +beautifulsoup4 diff --git a/scripts/postprocess_html.py b/scripts/postprocess_html.py new file mode 100644 index 000000000..8f1587d46 --- /dev/null +++ b/scripts/postprocess_html.py @@ -0,0 +1,33 @@ +# 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 ') + 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) + diff --git a/scripts/process_build.py b/scripts/process_build.py deleted file mode 100644 index e9bacb99f..000000000 --- a/scripts/process_build.py +++ /dev/null @@ -1,25 +0,0 @@ -# This script santises the sympy latex in the HTML files prior to building the site -import os -from bs4 import BeautifulSoup -base_dir = "./_build" - -def sanitize_latex(filepath): - is_modified = False - soup = BeautifulSoup(open(filepath), 'html.parser') - for latex in soup.find_all('div', {'class':'output_latex'}): - if "{{" in latex.string: - sanitized_latex = latex.string.replace("{{", "{ {") - latex.string = sanitized_latex - is_modified = True - if is_modified: - with open(filepath, "w") as f: - f.write(str(soup)) - -for directory in os.listdir(base_dir): - dir_path = os.path.join(base_dir, directory) - if os.path.isdir(dir_path): - for f in os.listdir(dir_path): - if str(f).endswith(".html"): - filepath = os.path.join(dir_path, f) - sanitize_latex(filepath) -