begingroup.js 3.5 KB

12345678910111213
  1. /*
  2. * /MathJax/extensions/TeX/begingroup.js
  3. *
  4. * Copyright (c) 2009-2013 The MathJax Consortium
  5. *
  6. * Part of the MathJax library.
  7. * See http://www.mathjax.org for details.
  8. *
  9. * Licensed under the Apache License, Version 2.0;
  10. * you may not use this file except in compliance with the License.
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. */MathJax.Extension["TeX/begingroup"]={version:"2.2"};MathJax.Hub.Register.StartupHook("TeX Jax Ready",function(){var b=MathJax.InputJax.TeX,e=b.Definitions,d=MathJax.Object.Subclass({macros:null,environments:null,Init:function(a,c){this.macros=a||{};this.environments=c||{}},Find:function(a,c){if(this[c][a])return this[c][a]},Def:function(a,c,b){this[b][a]=c},Undef:function(a,c){delete this[c][a]},Merge:function(a){MathJax.Hub.Insert(this.macros,a.macros);MathJax.Hub.Insert(this.environments,a.environments)},MergeGlobals:function(a){var c=this.macros,b;for(b in c)c.hasOwnProperty(b)&&c[b].global&&(a.Def(b,c[b],"macros",!0),delete c[b].global,delete c[b])},Clear:function(a){this.environments={};if(a)this.macros={};else{a=this.macros;for(var b in a)a.hasOwnProperty(b)&&!a[b].global&&delete a[b]}return this}}),g=b.nsStack=MathJax.Object.Subclass({stack:null,top:0,isEqn:!1,Init:function(a){this.isEqn=a;this.stack=[];a?this.Push(d()):this.Push(d(e.macros,e.environment))},Def:function(a,b,f,e){var d=this.top-1;if(e){for(;0<d;)this.stack[d].Undef(a,f),d--;b instanceof Array||(b=[b]);this.isEqn&&(b.global=!0)}this.stack[d].Def(a,b,f)},Push:function(a){this.stack.push(a);this.top=this.stack.length},Pop:function(){var a;1<this.top?(a=this.stack[--this.top],this.isEqn&&this.stack.pop()):this.isEqn&&this.Clear();return a},Find:function(a,b){for(var f=this.top-1;0<=f;f--){var d=this.stack[f].Find(a,b);if(d)return d}return null},Merge:function(a){a.stack[0].MergeGlobals(this);this.stack[this.top-1].Merge(a.stack[0]);a=[this.top,this.stack.length-this.top].concat(a.stack.slice(1));this.stack.splice.apply(this.stack,a);this.top=this.stack.length},Reset:function(){this.top=this.stack.length},Clear:function(a){this.stack=[this.stack[0].Clear()];this.top=this.stack.length}},{nsFrame:d});e.Add({macros:{begingroup:"BeginGroup",endgroup:"EndGroup",global:["Extension","newcommand"],gdef:["Extension","newcommand"]}},null,!0);b.Parse.Augment({BeginGroup:function(a){b.eqnStack.Push(d())},EndGroup:function(a){1<b.eqnStack.top?b.eqnStack.Pop():1===b.rootStack.top?b.Error(["ExtraEndMissingBegin","Extra %1 or missing \\begingroup",a]):(b.eqnStack.Clear(),b.rootStack.Pop())},csFindMacro:function(a){return b.eqnStack.Find(a,"macros")||b.rootStack.Find(a,"macros")},envFindName:function(a){return b.eqnStack.Find(a,"environments")||b.rootStack.Find(a,"environments")}});b.rootStack=g();b.eqnStack=g(!0);b.prefilterHooks.Add(function(){b.rootStack.Reset();b.eqnStack.Clear(!0)});b.postfilterHooks.Add(function(){b.rootStack.Merge(b.eqnStack)});MathJax.Hub.Register.StartupHook("TeX newcommand Ready",function(){e.Add({macros:{global:"Global",gdef:["Macro","\\global\\def"]}},null,!0);b.Parse.Augment({setDef:function(a,c){c.isUser=!0;b.eqnStack.Def(a,c,"macros",this.stack.env.isGlobal);delete this.stack.env.isGlobal},setEnv:function(a,c){c.isUser=!0;b.eqnStack.Def(a,c,"environments")},Global:function(a){var c=this.i,d=this.GetCSname(a);this.i=c;"let"!==d&&"def"!==d&&"newcommand"!==d&&b.Error(["GlobalNotFollowedBy","%1 not followed by \\let, \\def, or \\newcommand",a]);this.stack.env.isGlobal=!0}})});MathJax.Hub.Startup.signal.Post("TeX begingroup Ready")});MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/begingroup.js");