Пример #1
0
        public List <string> Parse(string path)
        {
            List <string> htmlTags = new List <string>();
            TagName       tName;
            TagName       tNAme;
            Tag           tag;

            if (File.Exists(path))
            {
                string readText = File.ReadAllText(path);
                string word;
                while (readText != null)
                {
                    int startIndex = readText.IndexOf('<');
                    int endIndex   = readText.IndexOf('>');
                    try
                    {
                        word = readText.Substring(startIndex + 1, endIndex - startIndex - 1);

                        int count = 0;
                        TagName.TryParse(word, out tName);
                        if (Enum.IsDefined(typeof(TagName), tName))
                        {
                            tag = new Tag(tName);
                            count++;
                            Console.WriteLine(count);
                            if (!word.Contains('/'))
                            {
                            }
                        }
                        if (word.Contains('/'))
                        {
                            htmlTags.Add(word.TrimStart(' ', '/'));
                            TagName.TryParse(word, out tNAme);
                            if (tName.Equals(tNAme))
                            {
                                count--;
                                Console.WriteLine(count);
                            }
                        }

                        else
                        {
                            htmlTags.Add(word);
                        }
                    }
                    catch
                    {
                        path = null;
                        break;
                    }
                    readText = readText.Remove(0, endIndex + 1);
                    // Console.WriteLine(readText);
                }
            }
            return(htmlTags);
        }
Пример #2
0
        public Tag Parse2(string path)
        {
            //List<string> htmlTags = new List<string>();
            //HtmlTag rootTag = new HtmlTag();


            string readText;

            //var ahtmltags = htmlTags.Where(x => x.Contains("a"));
            if (File.Exists(path))
            {
                Tag child;
                Tag rootTag;
                readText = File.ReadAllText(path);
                string  word;
                TagName tName;
                readText = readText.Replace("\n", "");
                readText = readText.Replace("\t", "");
                readText = readText.Replace("\r", "");
                int      startIndex = readText.IndexOf('<');
                int      endIndex   = readText.IndexOf('>');
                string   tagg;
                string   attribut;
                string[] allAttribut;
                string   key   = "";
                string   value = "";
                word = readText.Substring(startIndex + 1, endIndex - startIndex - 1);
                TagName.TryParse(word, out tName);
                rootTag  = new Tag(tName);
                readText = readText.Remove(0, endIndex + 1);
                while (readText != "")
                {
                    try
                    {
                        startIndex = readText.IndexOf('<');
                        endIndex   = readText.IndexOf('>');
                        word       = readText.Substring(startIndex + 1, endIndex - startIndex - 1);
                        if (word.Contains('/'))
                        {
                            if (word[0].Equals('/'))
                            {
                                if (!(word.Equals("/html")))
                                {
                                    word = word.TrimStart(' ', '/');
                                    //word = readText.Substring(startIndex, endIndex - startIndex - 1);
                                    TagName.TryParse(word, out tName);
                                    if (Enum.IsDefined(typeof(TagName), tName))
                                    {
                                        rootTag = rootTag.parent;
                                        //Console.WriteLine(word);
                                    }
                                }
                            }
                            else
                            {
                                //word = word.TrimStart(' ', '/');

                                word = word.Substring(startIndex + 1, endIndex - startIndex - 1);

                                int spaceIndex = word.IndexOf(' ');
                                if (spaceIndex == -1)
                                {
                                    tagg = word.Substring(startIndex + 1, endIndex - startIndex - 1);
                                }
                                else
                                {
                                    tagg        = word.Substring(startIndex + 1, spaceIndex - startIndex - 1);
                                    attribut    = word.Substring(spaceIndex + 1, endIndex - spaceIndex - 1);
                                    allAttribut = attribut.Split('=');
                                    key         = allAttribut[0];
                                    value       = allAttribut[1].Trim('\"');
                                }
                                TagName.TryParse(tagg, out tName);


                                if (Enum.IsDefined(typeof(TagName), tName))
                                {
                                    child = new Tag(tName);
                                    if (spaceIndex != -1)
                                    {
                                        child.SetAttribute(key, value);
                                    }
                                    child.isSelfClosingg = true;
                                    rootTag.AddChild(child);
                                    //Console.WriteLine(word);
                                }
                            }
                        }

                        else
                        {////////////////////////////////////////////////////////////////////
                            word = word.Substring(startIndex + 1, endIndex - startIndex - 1);

                            int spaceIndex = word.IndexOf(' ');
                            if (spaceIndex == -1)
                            {
                                tagg = word.Substring(startIndex + 1, endIndex - startIndex - 1);
                            }
                            else
                            {
                                tagg        = word.Substring(startIndex + 1, spaceIndex - startIndex - 1);
                                attribut    = word.Substring(spaceIndex + 1, endIndex - spaceIndex - 1);
                                allAttribut = attribut.Split('=');
                                key         = allAttribut[0];
                                value       = allAttribut[1].Trim('\"');
                            }
                            TagName.TryParse(tagg, out tName);


                            if (Enum.IsDefined(typeof(TagName), tName))
                            {
                                child = new Tag(tName);
                                if (spaceIndex != -1)
                                {
                                    child.SetAttribute(key, value);
                                }
                                child.isSelfClosingg = true;
                                rootTag.AddChild(child);
                                //Console.WriteLine(word);
                            }
                        }



                        //////////////////////////////////////////////////////

                        /*      TagName.TryParse(word, out tName);
                         *        if (Enum.IsDefined(typeof(TagName), tName))
                         *        {
                         *
                         *            child = new Tag(tName);
                         *
                         *            rootTag.AddChild(child);
                         *            rootTag = child;
                         *            //Console.WriteLine(rootTag.TagName);
                         *
                         *        }
                         *    }*/
                        readText = readText.Remove(0, endIndex + 1);
                    }

                    catch
                    {
                        //path = null;
                        ;
                    }
                }
                return(rootTag);
            }

            return(null);
        }