Example #1
0
        public string ConvertToMathMLTree(string latexExpression)
        {
            //string prefixLatex = @"\begin{document}";
            //string postfixLatex = @"\end{document}";
            //string resultLatexExpression = string.Format("{0}{1}{2}", prefixLatex, latexExpression, postfixLatex);
            string resultLatexExpression = latexExpression;
            var    parser = new LatexParser(resultLatexExpression, this);

            try
            {
                LatexExpression root = parser.Root;

                CallEventHandler(AfterDocumentWasParsed);

                String        linkToMMLDDT = "<!DOCTYPE math SYSTEM 'http://www.w3.org/Math/DTD/mathml1/mathml.dtd'>";
                StringBuilder sb           = new StringBuilder(linkToMMLDDT);

                Output = root.Convert();
                return(Output);
            }
            catch (Exception exp)
            {
                return(exp.Message);
            }
        }
Example #2
0
        private void ThreadConvert(object obj)
        {
            var             parser = new LatexParser(_sourceText, this);
            LatexExpression root;

            try
            {
                //try
                //{
                root = parser.Root;

                /*}
                 * // ReSharper disable RedundantCatchClause
                 #pragma warning disable 168
                 * catch (Exception e)
                 #pragma warning restore 168
                 * {
                 #if DEBUG
                 *  throw;
                 #else
                 *  Log.Error("Failed to parse the document", e);
                 *  return;
                 #endif
                 * }*/
                // ReSharper restore RedundantCatchClause


                CallEventHandler(AfterDocumentWasParsed);

                String        linkToMMLDDT = "<!DOCTYPE math SYSTEM 'http://www.w3.org/Math/DTD/mathml1/mathml.dtd'>";
                StringBuilder sb           = new StringBuilder(linkToMMLDDT);

                Output = root.Convert();
                //Console.WriteLine(Output);
                CallEventHandler(BeforeXmlFormat);
                XDocument xml;
                sb.Append(Output);
                xml = XDocument.Parse(sb.ToString());

                if (ValidateResult)
                {
                    #region Validate
                    CallEventHandler(BeforeValidate);
                    var settings = new XmlReaderSettings
                    {
                        ProhibitDtd    = false,
                        ValidationType = ValidationType.DTD
                    };
                    // ReSharper disable ConvertToConstant.Local
                    bool validationSuccessful = true;
                    // ReSharper restore ConvertToConstant.Local
                    settings.ValidationEventHandler += (s, e) =>
                    {
#if DEBUG
                        throw e.Exception;
#else
                        Log.Debug("DTD Validator - " + e.Message, e.Exception);
                        validationSuccessful = false;
#endif
                    };
                    var reader = xml.CreateReader();
                    while (reader.Read())
                    {
                    }
                    // ReSharper disable ConditionIsAlwaysTrueOrFalse
                    if (validationSuccessful)
                    // ReSharper restore ConditionIsAlwaysTrueOrFalse
                    {
                        CallEventHandler(ValidationSuccess);
                    }

                    #endregion
                }
            }
            catch (Exception e)
            {
                if (ExceptionEvent != null)
                {
                    ExceptionEvent(this, new ExceptionEventArgs(e.Message));
                }
            }
        }