repl.it
@CSharpIsGud/

C# html parser thingy

C#

No description

fork
loading
Files
  • main.cs
  • code.html
  • main.exe
main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Html.Trees;
using Html;

namespace Html
{

    enum HType // Future use maybe?, some html tags have specific attributes like <br> which is catostrophic to this, as it does not need a closing tag
    {
        Unknown = -1,
        p = 0
    }

    class HtmlParser
    {

        /// <summary>
        /// Enum representation for a token
        /// </summary>
        public enum TokEnum
        {
            Char = -1,
            Bracket_Open = 0,
            Bracket_Close = 1,
            Slash = 2,
            Space = 3,
            NewLine = 4
        }

        private class Tokenizer
        {
            private readonly string str;

            /// <summary>
            /// Token Class
            /// </summary>
            public class Tok
            {
                public TokEnum Type;
                public string Value;
                

                public Tok( TokEnum t, string v)
                {
                    Type = t;
                    Value = v;
                }
            }



            private List<Tok> result = new List<Tok>();

            /// <summary>
            /// Split the input into tokens
            /// </summary>
            public List<Tok> Exec()
            {
                foreach (char c in str)
                {
                   
                    switch (c)
                    {
                        case '<':
                            result.Add(new Tok(TokEnum.Bracket_Open, "<"));
                            break;
                        case '>':
                            result.Add(new Tok(TokEnum.Bracket_Close, ">"));
                            break;
                        case ' ':
                            result.Add(new Tok(TokEnum.Space, " "));
                            break;
                        case '\n':
                            result.Add(new Tok(TokEnum.NewLine, "\n"));
                            break;
                        case '/':
                            result.Add(new Tok(TokEnum.Slash, "/"));
                            break;
                        default:
                            result.Add(new Tok(TokEnum.Char, c.ToString()));
                            break;
                    }
                }

                return result;

            }

            public Tokenizer(string s) {
              str = s;
            }
        }

        /// <summary>
        /// Html To Parse
        /// </summary>
        public string Html;

        /// <summary>
        /// Parse Html
        /// </summary>
        private HtmlTree Parse(string str)
        {

            Tokenizer tok = new Tokenizer(str);

            HtmlTree tr = new HtmlTree();

            bool intag = false;
            bool AfterName = false;
            bool NeedsClose = false;
            bool Ignore = false;
            string Tag = "";
            string Name = "";
            string Attr = "";
            List<string> atrs = new List<string>();

            foreach (Tokenizer.Tok t in tok.Exec())
            {
                switch (t.Type)
                {
                    case TokEnum.Bracket_Open:
                        AfterName = false;
                        intag = true;
                        Name = "";
                        break;

                    case TokEnum.Space:
                    if (intag)
                        AfterName = true;
                        if (intag)
                        {
                            if (Attr != "")
                            {
                                atrs.Add(Attr);
                                Attr = "";
                            }
                        }
                        else
                        {
                            if (NeedsClose && ! Ignore)
                            {
                                Tag += t.Value;
                            }
                            if (intag)
                                Name += t.Value;
                        }
                        break;

                    case TokEnum.Slash:
                        Ignore = true;
                        break;

                    case TokEnum.Bracket_Close:
                        intag = false;
                        AfterName = false;
                        if (Attr != "")
                        {
                            atrs.Add(Attr);
                            Attr = "";
                         }
                        if (NeedsClose)
                        {
                            tr.Add(new HtmlObject(HtmlType.p, Name, atrs, Tag));
                            Tag = ""; // NeedClose will be ignored for the infamous non close needing tag, And children will eventually be implemented to allow things like elements inside of divs
                            Ignore = false;
                            NeedsClose = false;
                            atrs = new List<string>();
                        }
                        else
                        {
                            NeedsClose = true;
                        }
                        
                        break;

                    default: 
                        if (NeedsClose && ! Ignore)
                        {
                            Tag += t.Value;
                        }
                        if (intag)
                            Name += t.Value;
                        if (AfterName)
                            Attr += t.Value;
                        break;
                }
            }

            return tr;
        }


        public HtmlTree Exec(string h = null)
        {
            if (h != null)
            {
                return Parse(h);
            }
            else
            {
                return Parse(Html);
            }
        }

        public HtmlParser(string h) {
          Html = h;
        }

        public HtmlParser()
        {

        }
    }
}

namespace Html.Trees
{
    /// <summary>
    /// Keep a tree of the DOM
    /// </summary>
    class HtmlTree
    {
        public List<HtmlObject> ObjList = new List<HtmlObject>();

        public void Add(HtmlObject obj) => ObjList.Add(obj);
    }
    
    // Unused for now
    enum HtmlType
    {
        Unknown = -1,
        p = 0
    }

    /// <summary>
    /// An html object
    /// </summary>
    class HtmlObject
    {

        /// <summary>
        /// Type of object
        /// </summary>
        public HtmlType HType;

        public List<string> Atrs;

        public string TagName;

        /// <summary>
        /// What is inside the object
        /// </summary>
        public string Content;

        public HtmlObject(HtmlType t)
        {
            HType = t;
        }

        public HtmlObject(HtmlType t, string name, List<string> Ats, string con)
        {
            TagName = name;
            HType = t;
            Content = con;
            Atrs = Ats;
        }
    }

    /// <summary>
    /// Traverse an html tree
    /// </summary>
    class HtmlTreeTraverser
    {
        private HtmlTree tree;

        public void All(dynamic f)
        {
            foreach (HtmlObject o in tree.ObjList)
            {
                f(o);
            }
        }

        public HtmlTreeTraverser(HtmlTree t)
        {
            tree = t;
        }
    }
}

class MainClass {
  public static void Main (string[] args) {
    HtmlTree tr = new HtmlParser().Exec(new StreamReader(File.OpenRead("./code.html")).ReadToEnd());

    foreach (HtmlObject o in tr.ObjList) {
      Console.WriteLine("------------------------------");
      Console.WriteLine($"Name: {o.TagName}");
      Console.WriteLine($"Content: {o.Content}");
      Console.WriteLine("------------------------------");
    }
  }
}
?