1 /* 2 blahtex: a TeX to MathML converter designed with MediaWiki in mind 3 blahtexml: an extension of blahtex with XML processing in mind 4 http://gva.noekeon.org/blahtexml 5 6 Copyright (c) 2006, David Harvey 7 All rights reserved. 8 9 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 10 11 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 12 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 13 * Neither the names of the authors nor the names of their affiliation may be used to endorse or promote products derived from this software without specific prior written permission. 14 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 */ 17 18 #ifndef BLAHTEX_MANAGER_H 19 #define BLAHTEX_MANAGER_H 20 21 #include <string> 22 #include <vector> 23 #include <memory> 24 #include <set> 25 #include "Misc.h" 26 #include "MathmlNode.h" 27 #include "LayoutTree.h" 28 #include "ParseTree.h" 29 30 namespace blahtex 31 { 32 33 // The Manager class coordinates all the bits and pieces required to convert 34 // the given TeX input into MathML and purified TeX output, including 35 // tokenising, texvc-compatiblity macros, building the parse and layout 36 // trees, deciding which LaTeX packages to include, converting mathvariant 37 // to MathML version 1 fonts. 38 // 39 // The Manager class could be used as an interface between the blahtex core 40 // and an external program; alternatively, the Interface class (see 41 // Interface.h) provides a simpler interface. 42 43 class Manager 44 { 45 public: 46 Manager(); 47 48 // ProcessInput generates a parse tree and a layout tree from the 49 // supplied input. 50 // 51 // If texvcCompatibility is set, then ProcessInput will append a series 52 // of macros to emulate various non-standard commands that texvc 53 // recognises (see gTexvcCompatibilityMacros). This corresponds to the 54 // command line option "--texvc-compatible-commands". 55 void ProcessInput( 56 const std::wstring& input, 57 bool texvcCompatibility = false, 58 bool displayStyle = false 59 ); 60 61 // GenerateMathml generates a XML tree containing MathML markup. 62 // Returns the root node. 63 std::auto_ptr<MathmlNode> GenerateMathml( 64 const MathmlOptions& options 65 ) const; 66 67 // GeneratePurifiedTex returns a string containing a complete TeX file 68 // (including any required \usepackage commands) that could be fed to 69 // LaTeX to produce a graphical version of the input. 70 std::wstring GeneratePurifiedTex( 71 const PurifiedTexOptions& options 72 ) const; 73 74 // GeneratePurifiedTexOnly returns a string containing only 75 // the equation in LaTeX 76 std::wstring GeneratePurifiedTexOnly() const; 77 78 // A few accessor functions. GetParseTree()79 const ParseTree::MathNode* GetParseTree() const 80 { 81 return mParseTree.get(); 82 } 83 GetLayoutTree()84 const LayoutTree::Node* GetLayoutTree() const 85 { 86 return mLayoutTree.get(); 87 } 88 89 private: 90 // These store the parse tree and layout tree generated by ProcessInput. 91 std::auto_ptr<ParseTree::MathNode> mParseTree; 92 std::auto_ptr<LayoutTree::Node> mLayoutTree; 93 94 // This flag is set if the user has requested "strict spacing" rules 95 // (see SpacingControl) via the magic "\strictspacing" command. 96 bool mStrictSpacingRequested; 97 98 // There are a handful of errors that get picked up during the layout 99 // tree building phase, but which we want to return as MathML-related 100 // errors; i.e. we can still run PNG generation. If one of these 101 // happens, we cache it in mDelayedMathmlError, and return it when 102 // someone tries to GenerateMathml(). 103 // FIX: this is a bit hacky and badly designed. 104 // Come back and fix it up one day. 105 bool mHasDelayedMathmlError; 106 Exception mDelayedMathmlError; 107 108 // gStandardMacros is a string which, in effect, gets inserted at the 109 // beginning of any input string handled by ProcessInput. It contains 110 // a sequence of macro definitions ("\newcommand"s) which set up some 111 // standard TeX synonyms. 112 static std::wstring gStandardMacros; 113 114 // gTexvcCompatibilityMacros is similar; it contains definitions for 115 // commands recognised by texvc but that are not standard TeX/LaTeX/ 116 // AMS-LaTeX. (See also the texvcCompatibility flag.) 117 static std::wstring gTexvcCompatibilityMacros; 118 119 // Tokenised version of gStandardMacros and gTexvcCompatibilityMacros 120 // (computed only once, when first used): 121 static std::vector<Token> gStandardMacrosTokenised; 122 static std::vector<Token> gTexvcCompatibilityMacrosTokenised; 123 }; 124 125 } 126 127 #endif 128 129 // end of file @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 130