11/*********************************************************************
22 *
33 * mj-page-svg.js
4- *
4+ *
55 * Implements an API to MathJax in node.js so that MathJax can be
66 * used server-side to generate SVG, MathML, or images (the latter
77 * requires an external library, batik, to do the svg to png
8- * conversion). This API accepts HTML snippets that have their
8+ * conversion). This API accepts HTML snippets that have their
99 * math content converted to SVG's.
1010 *
1111 * ----------------------------------------------------------------------
@@ -65,6 +65,7 @@ var MathJaxConfig; // configuration for when starting MathJax
6565var MathJax ; // filled in once MathJax is loaded
6666var serverStarted = false ; // true when the MathJax DOM has been created
6767var serverReady = false ; // true when MathJax has done its initial typeset (loaded all components)
68+ var serverBusy = false ; // true when MathJax is working on a job in the que
6869var timer ; // used to reset MathJax if it runs too long
6970
7071var document , window , content , html ; // the DOM elements
@@ -130,11 +131,11 @@ function ConfigureMathJax() {
130131
131132 delete MathJax . Hub . config . styles ; // don't need any styles
132133 MathJax . Hub . Startup . MenuZoom = function ( ) { } ; // don't load menu or zoom code
133-
134+
134135 //
135136 // When creating stylesheets, no need to wait for them
136137 // to become active, so just do the callback
137- //
138+ //
138139 MathJax . Ajax . timer . create = function ( callback , node ) {
139140 callback = MathJax . Callback ( callback ) ;
140141 callback ( this . STATUS . OK ) ;
@@ -154,7 +155,7 @@ function ConfigureMathJax() {
154155 MathJax . Message . Clear = function ( ) { } ;
155156 MathJax . Message . Remove = function ( ) { } ;
156157 MathJax . Message . Init = function ( ) { } ;
157-
158+
158159 //
159160 // Trap Math Processing Errors
160161 //
@@ -177,7 +178,7 @@ function ConfigureMathJax() {
177178 MathJax . Hub . Register . MessageHook ( "TeX Jax - parse error" , function ( message ) {
178179 AddError ( "TeX parse error" , message [ 1 ] ) ;
179180 } ) ;
180-
181+
181182 //
182183 // Adjust the SVG output jax
183184 //
@@ -260,7 +261,7 @@ function ConfigureMathJax() {
260261 state . SVGdelay = false ;
261262 }
262263 } ) ;
263-
264+
264265 //
265266 // TEXT boxes use getBBox, which isn't implemented, so
266267 // use a monspace font and fake the size. Since these
@@ -314,7 +315,7 @@ function ConfigureMathJax() {
314315 serverReady = true ;
315316 MathJax . Hub . Queue ( StartQueue ) ;
316317 } ) ;
317-
318+
318319 }
319320 } ;
320321
@@ -398,7 +399,7 @@ function GetIMG() {
398399 var n = 0 , nodes = document . getElementsByClassName ( "MathJax_SVG" ) ;
399400 for ( var i = nodes . length - 1 ; i >= 0 ; i -- ) {
400401 var svg = nodes [ i ] . getElementsByTagName ( "svg" ) [ 0 ] ;
401- var css = svg . style . cssText +
402+ var css = svg . style . cssText +
402403 " width:" + svg . getAttribute ( "width" ) + "; height:" + svg . getAttribute ( "height" ) ;
403404 svg . style . cssText = "" ; // this will be handled by the <img> tag instead
404405 svg . setAttribute ( "xmlns" , "http://www.w3.org/2000/svg" ) ;
@@ -440,6 +441,7 @@ function GetPreviews() {
440441// do the next queued expression
441442//
442443function TypesetDone ( result , selector ) {
444+ serverBusy = false ;
443445 if ( timer ) { clearTimeout ( timer ) ; timer = null }
444446 if ( errors . length ) { result . errors = errors }
445447 if ( data . renderer === "None" ) { content . innerHTML = "<p></p>" }
@@ -532,9 +534,13 @@ function TypesetDone(result,selector) {
532534// Start typesetting the queued expressions
533535//
534536function StartQueue ( ) {
537+ serverBusy = true ;
535538 data = callback = null ; // clear existing equation, if any
536539 errors = [ ] ; // clear any errors
537- if ( ! queue . length ) return ; // return if nothing to do
540+ if ( ! queue . length ) { // return if nothing to do
541+ serverBusy = false ;
542+ return ;
543+ }
538544
539545 var result = { } , $$ = window . Array ;
540546
@@ -546,7 +552,7 @@ function StartQueue() {
546552 data = item [ 0 ] ; callback = item [ 1 ] ;
547553 content . innerHTML = data . html ; // %%% disable <script> tags?
548554 html . setAttribute ( "xmlns:" + data . xmlns , "http://www.w3.org/1998/Math/MathML" ) ;
549-
555+
550556 //
551557 // Set the SVG and TeX parameters
552558 // according to the requested data
@@ -564,14 +570,14 @@ function StartQueue() {
564570 SVG . config . useFontCache = data . useFontCache ;
565571 SVG . config . useGlobalCache = data . useGlobalCache && ! data . imgSVG ;
566572 TEX . config . equationNumbers . autoNumber = data . equationNumbers ;
567-
573+
568574 //
569575 // Set the TeX delimiters
570576 //
571577 var delimiters = $$ ( $$ ( '$' , '$' ) , $$ ( '\\(' , '\\)' ) ) ;
572578 if ( ! data . singleDollars ) { delimiters . shift ( ) }
573579 HUB . Config ( { tex2jax : { inlineMath : delimiters } } ) ;
574-
580+
575581 //
576582 // Register the preprocessors and get the jax selector
577583 //
@@ -625,6 +631,7 @@ function RestartMathJax() {
625631 serverReady = false ;
626632 }
627633 serverStarted = true ;
634+ serverBusy = false ;
628635 GetWindow ( ) ;
629636 ConfigureMathJax ( ) ;
630637 StartMathJax ( ) ;
@@ -648,7 +655,7 @@ exports.typeset = function (data,callback) {
648655 } }
649656 queue . push ( [ options , callback ] ) ;
650657 if ( ! serverStarted ) { RestartMathJax ( ) }
651- if ( serverReady ) StartQueue ( ) ;
658+ if ( serverReady && ! serverBusy ) StartQueue ( ) ;
652659}
653660
654661//
0 commit comments