Esempio n. 1
0
        static void Main(string[] args)
        {
            var           mlParser = new MlParser();
            Node          root     = mlParser.Parse(XDocument.Load("E:/Libraries/Desktop/BBP/WordMathTranspiler/WordMathTranspiler.MathMLParser/Resources/TestData.xml").Root);
            CodeGenerator cGen     = new CodeGenerator();
            var           tree     = cGen.Generate(root);

            CodeCompiler.GenerateExecutable(new[] { tree });
        }
        public static string HtmlToXml(string documentHtml)
        {
            var html = string.Copy(documentHtml);
            var mlp = new MlParser();
            string searchString;
            string value;
            string tag;

            #region Парсинг рисунка, анимации, аудио, видео, ссылки на них, формулы

            searchString = "<IMG";
            while (html.Contains(searchString))
            {
                mlp.GetTagBounds(html, searchString);
                value = mlp.GetValue("src_");
                if (!value.Equals(string.Empty))
                {
                    value = value.Substring(0, value.IndexOf("\\"));
                }
                // value может быть равно Flashes, Images или string.Empty.
                // value равно string.Empty, если обрабатывается рисунок или формула.

                if (value.Equals(Warehouse.Warehouse.RelativeFlashesDirectory))
                {
                    value = mlp.GetValue("sdocument");

                    if (value.Equals("0"))
                    {
                        #region Анимация

                        tag = string.Concat("<flash src=\"" + mlp.GetValue("src_"),
                                            "\" width=\"", "835",
                                            "\" height=\"", "615",
                                            "\" view=\"0\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                    else if (value.Equals("1"))
                    {
                        #region Ссылка на анимацию

                        tag = string.Concat("<flash src=\"" + mlp.GetValue("src_"),
                                            "\" width=\"", "835",
                                            "\" height=\"", "615",
                                            "\" title=\"", mlp.GetValue("alt"),
                                            "\" view=\"1\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                }
                else if (value.Equals(Warehouse.Warehouse.RelativeImagesDirectory) || 
                    value == string.Empty)
                {
                    value = mlp.GetValue("sdocument");

                    if (value.Equals("0"))
                    {
                        #region Рисунок

                        var src = mlp.GetValue("src");
                        src = TrainingModuleXmlWriter.ExtractRelativeSrc(src);
                        var width = mlp.GetValue("width");
                        var height = mlp.GetValue("height");

                        if (width == string.Empty || height == string.Empty)
                        {
                            var src_ = Path.Combine(Warehouse.Warehouse.ProjectEditorLocation, src);
                            var imageDimension = System.Drawing.Image.FromFile(src_).PhysicalDimension;
                            width = imageDimension.Width.ToString();
                            height = imageDimension.Height.ToString();
                        }

                        tag = string.Concat("<image src=\"", src,
                                            "\" width=\"", width,
                                            "\" height=\"", height);

                        var attribute = mlp.GetValue("style");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" style=\"", attribute);
                        }

                        attribute = mlp.GetValue("title");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" title=\"", attribute);
                        }

                        attribute = mlp.GetValue("align");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" align=\"", attribute);
                        }

                        attribute = mlp.GetValue("border");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" border=\"", attribute);
                        }

                        attribute = mlp.GetValue("hspace");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" hspace=\"", attribute);
                        }

                        attribute = mlp.GetValue("vspace");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" vspace=\"", attribute);
                        }

                        tag += "\" view=\"0\" />";

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                    else if (value.Equals("1"))
                    {
                        #region Ссылка на рисунок

                        var src = mlp.GetValue("src_");
                        var width = mlp.GetValue("width_");
                        var height = mlp.GetValue("height_");

                        if (width == string.Empty || height == string.Empty)
                        {
                            var src_ = Path.Combine(Warehouse.Warehouse.ProjectEditorLocation, src);
                            var imageDimension = System.Drawing.Image.FromFile(src_).PhysicalDimension;
                            width = imageDimension.Width.ToString();
                            height = imageDimension.Height.ToString();
                        }

                        tag = string.Concat("<image title=\"", mlp.GetValue("alt"), "\" src=\"", src, "\" width=\"",
                                            width, "\" height=\"", height);

                        var attribute = mlp.GetValue("align_");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" align=\"", attribute);
                        }

                        attribute = mlp.GetValue("border_");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" border=\"", attribute);
                        }

                        attribute = mlp.GetValue("hspace_");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" hspace=\"", attribute);
                        }

                        attribute = mlp.GetValue("vspace_");
                        if (!attribute.Equals(string.Empty))
                        {
                            tag += string.Concat("\" vspace=\"", attribute);
                        }

                        tag += "\" view=\"1\" />";

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                    else if (value.Equals(string.Empty))
                    {
                        #region Формула

                        var src = mlp.GetValue("src");
                        src = TrainingModuleXmlWriter.ExtractRelativeSrc(src);

                        tag = string.Concat("<image src=\"", src,"\" align=\"",
                                            mlp.GetValue("align"), "\" longDesc=\"",
                                            mlp.GetValue("longDesc"), "\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                }
                else if (value.Equals(Warehouse.Warehouse.RelativeAudiosDirectory))
                {
                    value = mlp.GetValue("sdocument");

                    if (value.Equals("0"))
                    {
                        #region Аудио

                        tag = string.Concat("<audio src=\"" + mlp.GetValue("src_"),
                                            "\" view=\"0\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                    else if (value.Equals("1"))
                    {
                        #region Ссылка на аудио

                        tag = string.Concat("<audio src=\"" + mlp.GetValue("src_"),
                                            "\" title=\"", mlp.GetValue("alt"),
                                            "\" view=\"1\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                }
                else if (value.Equals(Warehouse.Warehouse.RelativeVideosDirectory))
                {
                    value = mlp.GetValue("sdocument");

                    if (value.Equals("0"))
                    {
                        #region Видео

                        tag = string.Concat("<video src=\"" + mlp.GetValue("src_"),
                                            "\" view=\"0\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                    else if (value.Equals("1"))
                    {
                        #region Ссылка на видео

                        tag = string.Concat("<video src=\"" + mlp.GetValue("src_"),
                                            "\" title=\"", mlp.GetValue("alt"),
                                            "\" view=\"1\" />");

                        html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                        html = html.Insert(mlp.StartIndex, tag);

                        #endregion
                    }
                }
            }

            #endregion

            #region Парсинг греческих символов

            var greekSymbols = Regex.Matches(html, "[Α-Ωα-ω¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿™•∑∏∫∂√∞ƒ≤≥≠≡…′″∃∈∋∧∨∩∪∼≈⊂⊃⊆⊇⊕⊥°→×÷∀]", RegexOptions.IgnoreCase);
            int index;

            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    var symbol = greekSymbols[i - 1].Value;
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, string.Concat("&#", char.ConvertToUtf32(symbol, 0), ";"));
                }
            }

            greekSymbols = Regex.Matches(html, "[Ë]", RegexOptions.IgnoreCase);
            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, "&Euml;");
                }
            }

            greekSymbols = Regex.Matches(html, "[Ï]", RegexOptions.IgnoreCase);
            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, "&Iuml;");
                }
            }

            greekSymbols = Regex.Matches(html, "[Æ]", RegexOptions.IgnoreCase);
            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, "&AElig;");
                }
            }

            greekSymbols = Regex.Matches(html, "[Ä]", RegexOptions.IgnoreCase);
            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, "&Auml;");
                }
            }

            greekSymbols = Regex.Matches(html, "[Þ]", RegexOptions.IgnoreCase);
            if (greekSymbols.Count != 0)
            {
                for (var i = greekSymbols.Count; i > 0; i--)
                {
                    index = greekSymbols[i - 1].Index;
                    html = html.Remove(index, 1);
                    html = html.Insert(index, "&THORN;");
                }
            }

            #endregion

            return html;
        }
        public static string XmlToHtml(string documentHtml)
        {
            var html = string.Copy(documentHtml);
            var mlp = new MlParser();
            string searchString;
            string value;
            string tag;

            #region Парсинг рисунка, ссылки на рисунок, формулы

            searchString = "<image";
            while (html.Contains(searchString))
            {
                Application.DoEvents();

                mlp.GetTagBounds(html, searchString);
                value = mlp.GetValue("view");

                if (value.Equals("0"))
                {
                    #region Рисунок

                    var src = mlp.GetValue("src");
                    var width = mlp.GetValue("width");
                    var height = mlp.GetValue("height");

                    if (width == string.Empty || height == string.Empty)
                    {
                        var src_ = Path.Combine(Warehouse.Warehouse.ProjectEditorLocation, src);
                        var imageDimension = System.Drawing.Image.FromFile(src_).PhysicalDimension;
                        width = imageDimension.Width.ToString();
                        height = imageDimension.Height.ToString();
                    }

                    tag = string.Concat("<IMG src=\"", src,
                                        "\" width=\"", width,
                                        "\" height=\"", height);

                    var attribute = mlp.GetValue("style");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" style=\"", attribute);
                    }

                    attribute = mlp.GetValue("title");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" title=\"", attribute);
                    }

                    attribute = mlp.GetValue("align");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" align=\"", attribute);
                    }

                    attribute = mlp.GetValue("border");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" border=\"", attribute);
                    }

                    attribute = mlp.GetValue("hspace");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" hspace=\"", attribute);
                    }

                    attribute = mlp.GetValue("vspace");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" vspace=\"", attribute);
                    }

                    tag += string.Concat("\" sdocument=0>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
                else if (value.Equals("1"))
                {
                    #region Ссылка на рисунок

                    var src = mlp.GetValue("src");
                    var width = mlp.GetValue("width");
                    var height = mlp.GetValue("height");

                    if (width == string.Empty || height == string.Empty)
                    {
                        var src_ = Path.Combine(Warehouse.Warehouse.ProjectEditorLocation, src);
                        var imageDimension = System.Drawing.Image.FromFile(src_).PhysicalDimension;
                        width = imageDimension.Width.ToString();
                        height = imageDimension.Height.ToString();
                    }

                    tag = string.Concat("<IMG src=\"Images\\Pic.png\"",
                                        " src_=\"", mlp.GetValue("src"),
                                        "\" alt=\"", mlp.GetValue("title"),
                                        "\" width_=\"", mlp.GetValue("width"),
                                        "\" height_=\"", mlp.GetValue("height"));

                    var attribute = mlp.GetValue("align");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" align_=\"", attribute);
                    }

                    attribute = mlp.GetValue("border");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" border_=\"", attribute);
                    }

                    attribute = mlp.GetValue("hspace");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" hspace_=\"", attribute);
                    }

                    attribute = mlp.GetValue("vspace");
                    if (!attribute.Equals(string.Empty))
                    {
                        tag += string.Concat("\" vspace_=\"", attribute);
                    }

                    tag += string.Concat("\" sdocument=1>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
                else if (value.Equals(string.Empty))
                {
                    #region Формула

                    tag = string.Concat("<IMG src=\"", mlp.GetValue("src"),"\" align=\"",
                                        mlp.GetValue("align"),"\" longDesc=\"",
                                        mlp.GetValue("longDesc"), "\" />");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
            }

            #endregion

            #region Парсинг анимации, ссылки на анимацию

            searchString = "<flash";
            while (html.Contains(searchString))
            {
                Application.DoEvents();

                mlp.GetTagBounds(html, searchString);
                value = mlp.GetValue("view");

                if (value.Equals("0"))
                {
                    #region Анимация

                    tag = string.Concat("<IMG src=\"Images\\Anim.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" sdocument=0>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
                else if (value.Equals("1"))
                {
                    #region Ссылка на анимацию

                    tag = string.Concat("<IMG src=\"Images\\Anim.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" alt=\"", mlp.GetValue("title"),
                                        "\" sdocument=1>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
            }

            #endregion

            #region Парсинг аудио, ссылки на аудио

            searchString = "<audio";
            while (html.Contains(searchString))
            {
                Application.DoEvents();

                mlp.GetTagBounds(html, searchString);
                value = mlp.GetValue("view");

                if (value.Equals("0"))
                {
                    #region Аудио

                    tag = string.Concat("<IMG src=\"Images\\Aud.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" sdocument=0>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
                else if (value.Equals("1"))
                {
                    #region Ссылка на аудио

                    tag = string.Concat("<IMG src=\"Images\\Aud.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" alt=\"", mlp.GetValue("title"),
                                        "\" sdocument=1>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
            }

            #endregion

            #region Парсинг видело, ссылки на видео

            searchString = "<video";
            while (html.Contains(searchString))
            {
                Application.DoEvents();

                mlp.GetTagBounds(html, searchString);
                value = mlp.GetValue("view");

                if (value.Equals("0"))
                {
                    #region Видео

                    tag = string.Concat("<IMG src=\"Images\\Vid.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" sdocument=0>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
                else if (value.Equals("1"))
                {
                    #region Ссылка на видео

                    tag = string.Concat("<IMG src=\"Images\\Vid.png",
                                        "\" src_=\"", mlp.GetValue("src"),
                                        "\" alt=\"", mlp.GetValue("title"),
                                        "\" sdocument=1>");

                    html = html.Remove(mlp.StartIndex, mlp.LastIndex - mlp.StartIndex + 1);
                    html = html.Insert(mlp.StartIndex, tag);

                    #endregion
                }
            }

            #endregion

            return html;
        }