Commit 36b44253 by kimth

MathJax preprocessor comes from external plugin

parent 5a9a53d9
...@@ -382,10 +382,10 @@ class LoncapaProblem(object): ...@@ -382,10 +382,10 @@ class LoncapaProblem(object):
original_path = sys.path original_path = sys.path
for script in scripts: for script in scripts:
sys.path = original_path + self._extract_system_path(script) sys.path = original_path + self._extract_system_path(script)
stype = script.get('type') stype = script.get('type')
if stype: if stype:
if 'javascript' in stype: if 'javascript' in stype:
continue # skip javascript continue # skip javascript
......
### ###
### version of textline.html which does dynammic math ### version of textline.html which does dynamic math
### ###
<section class="text-input-dynamath"> <section class="text-input-dynamath textinputdynamath capa_inputtype" id="inputtype_${id}">
% if state == 'unsubmitted': % if state == 'unsubmitted':
<div class="unanswered" id="status_${id}"> <div class="unanswered" id="status_${id}">
% elif state == 'correct': % elif state == 'correct':
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
style="display:none;" style="display:none;"
% endif % endif
/> />
<div class="script_placeholder" data-src="/static/js/mathjax_preprocessor_for_6002x.js"/>
<p class="status"> <p class="status">
% if state == 'unsubmitted': % if state == 'unsubmitted':
unanswered unanswered
......
...@@ -7,11 +7,6 @@ class @Problem ...@@ -7,11 +7,6 @@ class @Problem
@url = @el.data('url') @url = @el.data('url')
@render() @render()
# TODO: Each course should be able to customize the MathJax preprocessor
# through a generic plug-in
if (@id).search('6.002x') >= 0
@mathjax_preprocessor = @mathjax_preprocessor_for_6002x
$: (selector) -> $: (selector) ->
$(selector, @el) $(selector, @el)
...@@ -305,47 +300,12 @@ class @Problem ...@@ -305,47 +300,12 @@ class @Problem
if jax = MathJax.Hub.getAllJax(target)[0] if jax = MathJax.Hub.getAllJax(target)[0]
eqn = $(element).val() eqn = $(element).val()
if @mathjax_preprocessor if window.mathjax_preprocessor
eqn = @mathjax_preprocessor(eqn) eqn = window.mathjax_preprocessor(eqn)
MathJax.Hub.Queue ['Text', jax, eqn], MathJax.Hub.Queue ['Text', jax, eqn],
[@updateMathML, jax, element] [@updateMathML, jax, element]
# TODO: The 6.002x preprocessor should not be baked into capa/display.coffee, but should be a plug-in
mathjax_preprocessor_for_6002x: (eqn) ->
###
Translate 6.002x conventions for subscripts to standard Latex, e.g.
'R3' --> 'R_{3}'
'vGS' --> 'v_{GS}'
'K/2*(vIN-VT)^2' --> 'K/2*(v_{IN}-V_{T})^2'
... and also escape specific 6.002x-related keywords from MathJax,
such as 'in' (parsed by MathJax as the set symbol)
###
# Default keywords are taken from capa/calc.py
default_keywords = ['sin', 'cos', 'tan', 'sqrt', 'log10', 'log2', 'ln', 'arccos', 'arcsin', 'arctan', 'abs', 'pi']
# Escape keywords are strings that have special meaning in 6.002x that should not be processed by Jax
escape_keywords = ['in']
# First, perform subscript insertion, but watch out for keywords
replace_subscript = (match) ->
if match in default_keywords or match in escape_keywords
return match
else
return match[0] + '_{' + match.substr(1) + '}'
eqn = eqn.replace(/[A-Za-z]\w+/g, replace_subscript)
# Second, escape 6.002x-specific keywords from MathJax
replace_escape_keyword = (match) ->
return '"' + match + '"' # Force MathJax plain text
for escape_keyword in escape_keywords
escape_keyword_patt = RegExp('\\b'+escape_keyword+'\\b')
eqn = eqn.replace(escape_keyword_patt, replace_escape_keyword)
return eqn
updateMathML: (jax, element) => updateMathML: (jax, element) =>
try try
$("##{element.id}_dynamath").val(jax.root.toMathML '') $("##{element.id}_dynamath").val(jax.root.toMathML '')
...@@ -361,6 +321,10 @@ class @Problem ...@@ -361,6 +321,10 @@ class @Problem
@answers = @inputs.serialize() @answers = @inputs.serialize()
inputtypeSetupMethods: inputtypeSetupMethods:
textinputdynamath: (element) =>
@mathjax_preprocessor = window.mathjax_preprocessor
javascriptinput: (element) => javascriptinput: (element) =>
data = $(element).find(".javascriptinput_data") data = $(element).find(".javascriptinput_data")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment