MathJax is shutting down its CDN, as officially announced on the MathJax website and on StackExchange\'s Meta. The official announcement recommends several alternatives, inc
Here's the first approach I whipped up in Python; not sure how safe or inclusive it is. I first write a function called update_mathjax
. This uses BeautifulSoup
to find the , and then searches for a
whose
src
attribute points to the MathJax CDN. If it finds it, it replaces src
with an updated src
and rewrites the file.
from bs4 import BeautifulSoup as bs
old_cdn = 'https://cdn.mathjax.org/mathjax/latest/MathJax.js'
new_cdn = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js'
def update_mathjax(filename):
with open(filename, 'r+') as file_handle:
file_contents = file_handle.read()
soup = bs(file_contents, "html5lib")
scripts = soup.find('head').find_all('script')
for script in scripts:
if script.attrs and 'src' in script.attrs:
if script.attrs['src'][:49] == old_cdn:
q = script.attrs['src'][49:]
new_src = new_cdn + q
script.attrs['src'] = new_src
file_handle.seek(0)
file_handle.write(soup.prettify())
file_handle.truncate()
print('udated ' + filename)
With that, it's reasonably easy to traverse the directory tree and apply the function.
import os
for directory_name, subdirectory_list, filename_list in os.walk(os.getcwd()):
for filename in filename_list:
if filename[-5:] == ".html":
update_mathjax(os.path.join(directory_name, filename))