Skip to content

Commit

Permalink
Polish postprocess HTML script and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
delapuente committed Jul 8, 2020
1 parent cccd216 commit 5cb0e0c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.PHONY: help book clean serve

BUIILD_DIR := "./_build"

help:
@echo "Please use 'make <target>' where <target> is one of:"
@echo " install to install the necessary dependencies for jupyter-book to build"
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
jupyter-book==0.6.5
beautifulsoup4
33 changes: 33 additions & 0 deletions scripts/postprocess_html.py
Original file line number Diff line number Diff line change
@@ -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 <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)

25 changes: 0 additions & 25 deletions scripts/process_build.py

This file was deleted.

0 comments on commit 5cb0e0c

Please sign in to comment.