begingroup.js 4.2 KB

1234567891011121314151617181920212223242526
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/TeX/begingroup.js
  6. *
  7. * Implements \begingroup and \endgroup commands that make local
  8. * definitions possible and are removed when the \endgroup occurs.
  9. *
  10. * ---------------------------------------------------------------------
  11. *
  12. * Copyright (c) 2011-2013 The MathJax Consortium
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */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");