symbolic_mathjax_preprocessor.js 1.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* This file defines a processor in between the student's math input
   (AsciiMath) and what is read by MathJax. It allows for our own
   customizations, such as use of the syntax "a_b__x" in superscripts, or
   possibly coloring certain variables, etc&.

   It is used in the <textline> definition like the following:

     <symbolicresponse expect="a_b^c + b_x__d" size="30">
       <textline math="1"
         preprocessorClassName="SymbolicMathjaxPreprocessor"
	 preprocessorSrc="/static/js/capa/symbolic_mathjax_preprocessor.js"/>
     </symbolicresponse>
*/
14 15
window.SymbolicMathjaxPreprocessor = function() {
    this.fn = function(eqn) {
16
    // flags and config
17
        var superscriptsOn = true;
18

19
        if (superscriptsOn) {
20 21 22
      // find instances of "__" and make them superscripts ("^") and tag them
      // as such. Specifcally replace instances of "__X" or "__{XYZ}" with
      // "^{CHAR$1}", marking superscripts as different from powers
23

24 25
      // a zero width space--this is an invisible character that no one would
      // use, that gets passed through MathJax and to the server
26 27
            var c = '\u200b';
            eqn = eqn.replace(/__(?:([^\{])|\{([^\}]+)\})/g, '^{' + c + '$1$2}');
28

29 30
      // NOTE: MathJax supports '\class{name}{mathcode}' but not for asciimath
      // input, which is too bad. This would be preferable to this char tag
31
        }
32

33 34
        return eqn;
    };
35
};