1- import { TeX } from "mathjax3/input/tex.js" ;
2- import { CHTML } from "mathjax3/output/chtml.js" ;
3- import { SVG } from "mathjax3/output/svg.js" ;
4- import { HTMLMathItem } from "mathjax3/handlers/html/HTMLMathItem.js" ;
5- import { HTMLDocument } from "mathjax3/handlers/html/HTMLDocument.js" ;
6- import { handleRetriesFor } from "mathjax3/util/Retries.js" ;
7- import { browserAdaptor } from "mathjax3/adaptors/browserAdaptor.js" ;
1+ import { TeX } from '../mathjax3/input/tex.js' ;
2+ import { CHTML } from '../mathjax3/output/chtml.js' ;
3+ import { SVG } from '../mathjax3/output/svg.js' ;
4+ import { HTMLMathItem } from '../mathjax3/handlers/html/HTMLMathItem.js' ;
5+ import { HTMLDocument } from '../mathjax3/handlers/html/HTMLDocument.js' ;
6+ import { handleRetriesFor } from '../mathjax3/util/Retries.js' ;
7+ import { browserAdaptor } from '../mathjax3/adaptors/browserAdaptor.js' ;
8+ import { SerializedMmlVisitor } from '../mathjax3/core/MmlTree/SerializedMmlVisitor.js' ;
89
9- import { ConfigurationHandler } from 'mathjax3/input/tex/Configuration.js' ;
10- import ' mathjax3/input/tex/AllPackages.js';
10+ import { ConfigurationHandler } from '../ mathjax3/input/tex/Configuration.js' ;
11+ import { AllPackages } from '../ mathjax3/input/tex/AllPackages.js';
1112
12- let chtml = new CHTML ( ) ;
13- let svg = new SVG ( ) ;
14-
15- let docs = {
16- CHTML : new HTMLDocument ( document , browserAdaptor ( ) , { OutputJax : chtml } ) ,
17- SVG : new HTMLDocument ( document , browserAdaptor ( ) , { OutputJax : svg } )
13+ const jax = {
14+ TeX : new TeX ( ) ,
15+ CHTML : new CHTML ( ) ,
16+ SVG : new SVG ( )
17+ } ;
18+ const docs = {
19+ CHTML : new HTMLDocument ( document , browserAdaptor ( ) , { InputJax : jax . TeX , OutputJax : jax . CHTML } ) ,
20+ SVG : new HTMLDocument ( document , browserAdaptor ( ) , { InputJax : jax . TeX , OutputJax : jax . SVG } )
1821} ;
19- document . head . appendChild ( chtml . styleSheet ( docs . CHTML ) ) ;
20- document . head . appendChild ( svg . styleSheet ( docs . SVG ) ) ;
22+
23+ const visitor = new SerializedMmlVisitor ( ) ;
24+ const toMml = ( node => visitor . visitTree ( node , Lab . doc . document ) ) ;
2125
2226const Lab = window . Lab = {
2327 tex : document . getElementById ( 'tex' ) ,
2428 output : document . getElementById ( 'output' ) ,
29+ mathml : document . getElementById ( 'mathml' ) ,
2530 display : true ,
31+ showMML : false ,
32+ packages : { } ,
2633 renderer : 'CHTML' ,
2734 doc : docs . CHTML ,
28- TeX : null ,
29- packages : { } ,
35+ jax : jax . CHTML ,
36+ mml : '' ,
3037
3138 Typeset ( ) {
3239 this . output . innerHTML = '' ;
3340 let text = this . output . appendChild ( document . createTextNode ( '' ) ) ;
41+ if ( ! document . getElementById ( this . renderer + '-styles' ) ) {
42+ document . head . appendChild ( this . jax . styleSheet ( this . doc ) ) ;
43+ }
3444
3545 let TeX = this . tex . value ;
36- let math = new HTMLMathItem ( TeX , this . TeX , this . display ) ;
46+ let math = new HTMLMathItem ( TeX , jax . TeX , this . display ) ;
3747 math . setMetrics ( ...this . metrics ) ;
3848 math . start = { node : text , n : 0 , delim : '' } ;
3949 math . end = { node : text , n : 0 , delim : '' } ;
4050 this . mathItem = math ;
4151
52+ jax . TeX . parseOptions . tags . reset ( ) ;
53+
4254 handleRetriesFor ( function ( ) {
43- math . compile ( this . doc ) ;
55+ math . compile ( ) ;
56+ this . mml = toMml ( math . root ) ;
57+ this . mathml . innerHTML = '' ;
58+ if ( this . showMML ) Lab . mathml . appendChild ( document . createTextNode ( this . mml ) ) ;
4459 math . typeset ( this . doc ) ;
45- math . updateDocument ( this . doc ) ;
60+ math . updateDocument ( this . doc ) ;
4661 } . bind ( this ) ) . catch ( err => { console . log ( "Error: " + err . message ) ; console . log ( err . stack ) } ) ;
4762 } ,
4863
@@ -51,6 +66,7 @@ const Lab = window.Lab = {
5166 '?' ,
5267 this . renderer . charAt ( 0 ) ,
5368 ( this . display ? 1 : 0 ) ,
69+ ( this . showMML ? 1 : 0 ) ,
5470 this . getPackageFlags ( ) ,
5571 encodeURIComponent ( this . tex . value )
5672 ] . join ( '' ) ;
@@ -73,32 +89,41 @@ const Lab = window.Lab = {
7389
7490 Packages ( ) {
7591 let div = document . getElementById ( 'package' ) ;
76- for ( let key of ConfigurationHandler . keys ( ) ) {
92+ for ( let key of Array . from ( ConfigurationHandler . keys ( ) ) . sort ( ) ) {
7793 if ( key === 'empty' || key === 'extension' ) continue ;
7894 let checkbox = document . createElement ( 'input' ) ;
79- checkbox . type = " checkbox" ;
95+ checkbox . type = ' checkbox' ;
8096 checkbox . name = key ;
8197 checkbox . value = key ;
8298 checkbox . id = 'package-' + key ;
99+ checkbox . onchange = function ( ) { Lab . newPackages ( ) } ;
83100 if ( key === 'base' ) checkbox . checked = true ;
84101 let label = document . createElement ( 'label' ) ;
85102 label . htmlFor = 'package-' + key ;
86103 label . appendChild ( document . createTextNode ( key [ 0 ] . toUpperCase ( ) + key . slice ( 1 ) ) ) ;
87104 checkbox . appendChild ( label ) ;
88- div . appendChild ( checkbox ) ;
89- div . appendChild ( label ) ;
105+ let span = div . appendChild ( document . createElement ( 'span' ) ) ;
106+ span = span . appendChild ( document . createElement ( 'span' ) ) ;
107+ span . appendChild ( checkbox ) ;
108+ span . appendChild ( label ) ;
90109 this . packages [ key ] = 'package-' + key ;
91110 }
92111 } ,
93-
112+
94113 newPackages ( ) {
95- this . TeX = new TeX ( { packages : this . getPackages ( ) } ) ;
96- docs . CHTML = new HTMLDocument ( document , browserAdaptor ( ) , { InputJax : this . TeX , OutputJax : chtml } ) ;
97- docs . SVG = new HTMLDocument ( document , browserAdaptor ( ) , { InputJax : this . TeX , OutputJax : svg } ) ;
114+ jax . TeX = new TeX ( { packages : this . getPackages ( ) } ) ;
115+ docs . CHTML . inputJax = [ jax . TeX ] ;
116+ docs . SVG . inputJax = [ jax . TeX ] ;
98117 this . doc = docs [ this . renderer ] ;
118+ jax . TeX . setAdaptor ( this . doc . adaptor ) ;
119+ jax . TeX . setMmlFactory ( this . doc . mmlFactory ) ;
99120 Lab . Typeset ( ) ;
100121 } ,
101-
122+
123+ sendToMathML ( ) {
124+ window . location = 'MML-Lab.html?' + this . renderer . charAt ( 0 ) + encodeURIComponent ( this . mml ) ;
125+ } ,
126+
102127 setDisplay ( checked ) {
103128 this . display = checked ;
104129 this . Typeset ( ) ;
@@ -107,10 +132,16 @@ const Lab = window.Lab = {
107132 setRenderer ( value ) {
108133 this . renderer = value ;
109134 this . doc = docs [ value ] ;
135+ this . jax = jax [ value ] ;
110136 this . Typeset ( ) ;
111137 } ,
112138
113- checkKey : function ( textarea , event ) {
139+ setMathML ( checked ) {
140+ this . showMML = checked ;
141+ this . Typeset ( ) ;
142+ } ,
143+
144+ checkKey ( textarea , event ) {
114145 if ( ! event ) event = window . event ;
115146 var code = event . which || event . keyCode ;
116147 if ( ( event . ctrlKey || event . metaKey || event . shiftKey || event . altKey ) &&
@@ -121,24 +152,26 @@ const Lab = window.Lab = {
121152 }
122153 } ,
123154
124- Init ( ) {
125- let text = this . output . appendChild ( document . createTextNode ( '' ) ) ;
126- let test = chtml . getTestElement ( text , true ) ;
127- let { em, ex, containerWidth, lineWidth, scale} = chtml . measureMetrics ( test ) ;
155+ Init ( ) {
156+ let test = jax . CHTML . getTestElement ( this . output ) ;
157+ let { em, ex, containerWidth, lineWidth, scale} = jax . CHTML . measureMetrics ( test ) ;
128158 this . metrics = [ em , ex , containerWidth , lineWidth , scale ] ;
129159 this . output . removeChild ( test ) ;
130160 } ,
131161
132162 Load ( ) {
133- const n = Lab . getPackageFlags ( ) . length ;
134163 const data = decodeURIComponent ( window . location . search . substr ( 1 ) ) ;
135- this . tex . value = data . substr ( n + 2 ) . trim ( ) ;
164+ const n = Lab . getPackageFlags ( ) . length ;
165+ this . tex . value = data . substr ( n + 3 ) . trim ( ) ;
166+ this . showMML = data . charAt ( 2 ) === '1' ;
136167 this . display = data . charAt ( 1 ) === '1' ;
137168 this . renderer = { C : 'CHTML' , S : 'SVG' } [ data . charAt ( 0 ) ] ;
138169 this . doc = docs [ this . renderer ] ;
170+ this . jax = jax [ this . renderer ] ;
139171 document . getElementById ( 'renderer' ) . value = this . renderer ;
140172 document . getElementById ( 'display' ) . checked = this . display ;
141- const flags = data . substr ( 2 , n ) ;
173+ document . getElementById ( 'showMML' ) . checked = this . showMML ;
174+ const flags = data . substr ( 3 , n ) ;
142175 let i = 0 ;
143176 for ( const key in Lab . packages ) {
144177 if ( flags . charAt ( i ++ ) === '1' ) {
@@ -151,5 +184,5 @@ const Lab = window.Lab = {
151184
152185Lab . Packages ( ) ;
153186Lab . Init ( ) ;
154- if ( window . location . search !== "" ) Lab . Load ( ) ;
187+ if ( window . location . search !== '' ) Lab . Load ( ) ;
155188Lab . newPackages ( ) ;
0 commit comments