Пример #1
0
 /// <summary>
 /// Starts the conversion process.
 /// </summary>
 public string ConvertToText()
 {
     var result = string.Empty;
     SectionContents = new Dictionary<SectionType, List<SectionContentsValue>>();
     SectionContents[SectionType.Numbered] = new List<SectionContentsValue>();
     SectionContents[SectionType.Unnumbered] = new List<SectionContentsValue>();
     var parser = new LatexParser(_sourceText, this);
     LatexExpression root;
     try
     {
         root = parser.Root;
     }
     catch (Exception e)
     {
         Log.Error("Failed to parse the document", e);
         return result;
     }
     result = root.Convert();
     return result;
 }
Пример #2
0
        private void ThreadConvert(object obj)
        {
            SectionContents = new Dictionary<SectionType, List<SectionContentsValue>>();
            SectionContents[SectionType.Numbered] = new List<SectionContentsValue>();
            SectionContents[SectionType.Unnumbered] = new List<SectionContentsValue>();
            var parser = new LatexParser(_sourceText, this);
            LatexExpression root;
            //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 output = root.Convert();
            CallEventHandler(BeforeXmlFormat);
            XDocument xml;
            try
            {
                xml = XDocument.Parse(output);
                xml.Save(OutputPath);
            }
            catch (Exception e)
            {
                File.WriteAllText(OutputPath, output);
            #if DEBUG
                throw;
            #else
                Log.Error(e.Message, e.InnerException);
                return;
            #endif
            }

            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
                //                {
                //                    #region Insert W3C logos
                //                    // ReSharper disable PossibleNullReferenceException
                //                    var body = xml.Element(XName.Get("html", "http://www.w3.org/1999/xhtml")).
                //                        Element(XName.Get("body", "http://www.w3.org/1999/xhtml"));
                //                    var img = new XElement(XName.Get("img", "http://www.w3.org/1999/xhtml"));
                //                    img.SetAttributeValue("src", "http://www.w3.org/Icons/valid-xhtml11-blue.png");
                //                    img.SetAttributeValue("alt", "Valid XHTML 1.1");
                //                    body.Add(img);
                //                    img = new XElement(XName.Get("img", "http://www.w3.org/1999/xhtml"));
                //                    img.SetAttributeValue("src", "http://www.w3.org/Icons/valid-css-blue.png");
                //                    img.SetAttributeValue("alt", "Valid CSS");
                //                    body.Add(img);
                //                    img = new XElement(XName.Get("img", "http://www.w3.org/1999/xhtml"));
                //                    img.SetAttributeValue("src", "http://www.w3.org/Icons/valid-mathml20-blue.png");
                //                    img.SetAttributeValue("alt", "Valid MATHML 2.0");
                //                    body.Add(img);
                //                    // ReSharper restore PossibleNullReferenceException
                //                    #endregion
                //                }
                xml.Save(OutputPath);
                #endregion
            }
            var destFile = Path.Combine(Path.GetDirectoryName(OutputPath), "styles.css");
            if (destFile != "styles.css")
            {
                File.Copy("styles.css", destFile, true);
            }
        }
        /// <summary>
        /// Starts the snippet conversion process.
        /// </summary>
        public string ConvertSnippet()
        {
            var parser = new LatexParser(_sourceText, this);

            LatexExpression root;
            root = parser.Root;

            return root.Convert();
        }