Inject MathJax JS to render math in locally-saved instructions

You still need the Internet to render math when you open instructions
later.
This commit is contained in:
Yuri Bochkarev 2016-06-10 10:21:48 +03:00
parent 6bb46e9a0d
commit 0626cbdf41
3 changed files with 20 additions and 5 deletions

View file

@ -22,7 +22,7 @@ from .define import (OPENCOURSE_SUPPLEMENT_URL,
OPENCOURSE_ONDEMAND_COURSE_MATERIALS,
OPENCOURSE_VIDEO_URL,
INSTRUCTIONS_CSS,
INSTRUCTIONS_HTML_INJECTION,
IN_MEMORY_EXTENSION,
IN_MEMORY_MARKER)
@ -173,7 +173,7 @@ class CourseraOnDemand(object):
@type soup: BeautifulSoup
"""
# 1. Inject basic CSS style
css_soup = BeautifulSoup(INSTRUCTIONS_CSS)
css_soup = BeautifulSoup(INSTRUCTIONS_HTML_INJECTION)
soup.head.append(css_soup)
# 2. Replace <text> with <p>

View file

@ -53,6 +53,7 @@ import shutil
import subprocess
import sys
import time
import codecs
from distutils.version import LooseVersion as V
@ -313,7 +314,7 @@ def parse_on_demand_syllabus(session, page, reverse=False, intact_fnames=False,
course_name = dom['slug']
logging.info('Parsing syllabus of on-demand course. '
'This may take some time, be patient ...')
'This may take some time, please be patient ...')
modules = []
json_modules = dom['courseMaterial']['elements']
course = CourseraOnDemand(session=session, course_id=dom['id'])
@ -551,7 +552,7 @@ def download_lectures(downloader,
if not skip_download:
if url.startswith(IN_MEMORY_MARKER):
page_content = url[len(IN_MEMORY_MARKER):]
with open(lecfn, 'w') as file_object:
with codecs.open(lecfn, 'w', 'utf-8') as file_object:
file_object.write(page_content)
elif:
if not url.startswith('mailto:'):

View file

@ -151,7 +151,7 @@ IN_MEMORY_EXTENSION = 'html'
IN_MEMORY_MARKER = '#inmemory#'
#: CSS that is usen to prettify instructions
INSTRUCTIONS_CSS = '''
INSTRUCTIONS_HTML_INJECTION = '''
<style>
pre {
display: block;
@ -165,4 +165,18 @@ pre {
border: 1px solid #ccc;
}
</style>
<script type="text/javascript" async
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$$','$$'], ['$','$'] ],
displayMath: [ ["\\\\[","\\\\]"] ],
processEscapes: true
}
});
</script>
'''