-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathPageMetaData.cs
More file actions
503 lines (416 loc) · 10.5 KB
/
PageMetaData.cs
File metadata and controls
503 lines (416 loc) · 10.5 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using Waher.Content.Html.Elements;
using Waher.Content.Html.OpenGraph;
using Waher.Runtime.Collections;
namespace Waher.Content.Html
{
/// <summary>
/// Contains meta-data about a page.
///
/// Protocols supported:
/// The Open Graph protocol: http://ogp.me/
/// Twitter Cards: https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup
/// </summary>
public class PageMetaData
{
private ImageInformation[] images = null;
private AudioInformation[] audio = null;
private VideoInformation[] video = null;
private string[] localeAlternate = null;
private string title = null;
private string type = null;
private string url = null;
private string description = null;
private string determiner = null;
private string locale = null;
private string siteName = null;
/// <summary>
/// Contains meta-data about a page.
///
/// Protocols supported:
/// The Open Graph protocol: http://ogp.me/
/// Twitter Cards: https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/markup
/// </summary>
public PageMetaData()
{
}
/// <summary>
/// Contains meta-data about a page.
///
/// Protocols supported:
/// The Open Graph protocol: http://ogp.me/
/// </summary>
/// <param name="Doc">HTML Document</param>
public PageMetaData(HtmlDocument Doc)
{
ChunkedList<ImageInformation> Images = null;
ChunkedList<AudioInformation> Audio = null;
ChunkedList<VideoInformation> Video = null;
ChunkedList<string> LocaleAlternate = null;
ImageInformation LastImage = null;
AudioInformation LastAudio = null;
VideoInformation LastVideo = null;
string Name;
string Value;
if (!(Doc.Meta is null))
{
foreach (Meta Meta in Doc.Meta)
{
if (!Meta.HasAttributes)
continue;
Name = Value = null;
if (Meta.HasAttributes)
{
foreach (HtmlAttribute Attr in Meta.Attributes)
{
switch (Attr.FullName)
{
case "property":
case "name":
Name = Attr.Value;
break;
case "content":
Value = Attr.Value;
break;
}
}
}
if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Value))
continue;
switch (Name)
{
case "og:title":
this.title = Value;
break;
case "twitter:title":
if (string.IsNullOrEmpty(this.title))
this.title = Value;
break;
case "og:type":
this.type = Value;
break;
case "twitter:card":
if (string.IsNullOrEmpty(this.type))
this.type = Value;
break;
case "og:url":
this.url = Value;
break;
case "og:image":
case "og:image:url":
case "twitter:image":
if (!(Images is null))
{
LastImage = null;
foreach (ImageInformation Image in Images)
{
if (Image.Url == Value)
{
LastImage = Image;
break;
}
}
if (!(LastImage is null))
break;
}
LastImage = new ImageInformation()
{
Url = Value
};
if (Images is null)
Images = new ChunkedList<ImageInformation>();
Images.Add(LastImage);
break;
case "og:image:secure_url":
if (!(LastImage is null))
LastImage.SecureUrl = Value;
break;
case "og:image:type":
if (!(LastImage is null))
LastImage.ContentType = Value;
break;
case "og:image:width":
if (!(LastImage is null) && int.TryParse(Value, out int i))
LastImage.Width = i;
break;
case "og:image:height":
if (!(LastImage is null) && int.TryParse(Value, out i))
LastImage.Height = i;
break;
case "og:image:alt":
case "twitter:image:alt":
if (!(LastImage is null))
LastImage.Description = Value;
break;
case "og:audio":
LastAudio = new AudioInformation()
{
Url = Value
};
if (Audio is null)
Audio = new ChunkedList<AudioInformation>();
Audio.Add(LastAudio);
break;
case "og:audio:secure_url":
if (!(LastAudio is null))
LastAudio.SecureUrl = Value;
break;
case "og:audio:type":
if (!(LastAudio is null))
LastAudio.ContentType = Value;
break;
case "og:description":
this.description = Value;
break;
case "twitter:description":
case "description":
if (string.IsNullOrEmpty(this.description))
this.description = Value;
break;
case "og:determiner":
this.determiner = Value;
break;
case "og:locale":
this.locale = Value;
break;
case "og:locale:alternate":
if (LocaleAlternate is null)
LocaleAlternate = new ChunkedList<string>();
LocaleAlternate.Add(Value);
break;
case "og:site_name":
this.siteName = Value;
break;
case "og:video":
LastVideo = new VideoInformation()
{
Url = Value
};
if (Video is null)
Video = new ChunkedList<VideoInformation>();
Video.Add(LastVideo);
break;
case "og:video:secure_url":
if (!(LastVideo is null))
LastVideo.SecureUrl = Value;
break;
case "og:video:type":
if (!(LastVideo is null))
LastVideo.ContentType = Value;
break;
case "og:video:width":
if (!(LastVideo is null) && int.TryParse(Value, out i))
LastVideo.Width = i;
break;
case "og:video:height":
if (!(LastVideo is null) && int.TryParse(Value, out i))
LastVideo.Height = i;
break;
}
}
}
if (string.IsNullOrEmpty(this.title) && !(Doc.Title is null))
this.title = Doc.Title.InnerText.Trim();
this.images = Images?.ToArray();
this.audio = Audio?.ToArray();
this.video = Video?.ToArray();
this.localeAlternate = LocaleAlternate?.ToArray();
}
/// <summary>
/// Image representing page.
/// </summary>
public ImageInformation[] Images
{
get => this.images;
set => this.images = value;
}
/// <summary>
/// Audio content
/// </summary>
public AudioInformation[] Audio
{
get => this.audio;
set => this.audio = value;
}
/// <summary>
/// Video content
/// </summary>
public VideoInformation[] Video
{
get => this.video;
set => this.video = value;
}
/// <summary>
/// Alternate locales
/// </summary>
public string[] LocaleAlternate
{
get => this.localeAlternate;
set => this.localeAlternate = value;
}
/// <summary>
/// Title of page.
/// </summary>
public string Title
{
get => this.title;
set => this.title = value;
}
/// <summary>
/// Type of page.
/// </summary>
public string Type
{
get => this.type;
set => this.type = value;
}
/// <summary>
/// Canonical URL of page.
/// </summary>
public string Url
{
get => this.url;
set => this.url = value;
}
/// <summary>
/// A description of the page.
/// </summary>
public string Description
{
get => this.description;
set => this.description = value;
}
/// <summary>
/// A word that appears before the title.
/// </summary>
public string Determiner
{
get => this.determiner;
set => this.determiner = value;
}
/// <summary>
/// Locale of information.
/// </summary>
public string Locale
{
get => this.locale;
set => this.locale = value;
}
/// <summary>
/// Name of web site publishing page
/// </summary>
public string SiteName
{
get => this.siteName;
set => this.siteName = value;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
PageMetaData Meta = obj as PageMetaData;
int i, c;
if ((this is null) ^ (Meta is null))
return false;
if (this is null)
return true;
if (this.title != Meta.title ||
this.type != Meta.type ||
this.url != Meta.url ||
this.description != Meta.description ||
this.determiner != Meta.determiner ||
this.locale != Meta.locale ||
this.siteName != Meta.siteName)
{
return false;
}
if ((this.localeAlternate is null) ^ (Meta.localeAlternate is null))
return false;
if (!(this.localeAlternate is null))
{
if ((c = this.localeAlternate.Length) != Meta.localeAlternate.Length)
return false;
for (i = 0; i < c; i++)
{
if (this.localeAlternate[i] != Meta.localeAlternate[i])
return false;
}
}
if ((this.images is null) ^ (Meta.images is null))
return false;
if (!(this.images is null))
{
if ((c = this.images.Length) != Meta.images.Length)
return false;
for (i = 0; i < c; i++)
{
if (!this.images[i].Equals(Meta.images[i]))
return false;
}
}
if ((this.audio is null) ^ (Meta.audio is null))
return false;
if (!(this.audio is null))
{
if ((c = this.audio.Length) != Meta.audio.Length)
return false;
for (i = 0; i < c; i++)
{
if (!this.audio[i].Equals(Meta.audio[i]))
return false;
}
}
if ((this.video is null) ^ (Meta.video is null))
return false;
if (!(this.video is null))
{
if ((c = this.video.Length) != Meta.video.Length)
return false;
for (i = 0; i < c; i++)
{
if (!this.video[i].Equals(Meta.video[i]))
return false;
}
}
return true;
}
/// <inheritdoc/>
public override int GetHashCode()
{
int Result = 0;
if (!(this.title is null))
Result = this.title.GetHashCode();
if (!(this.type is null))
Result ^= Result << 5 ^ this.type.GetHashCode();
if (!(this.url is null))
Result ^= Result << 5 ^ this.url.GetHashCode();
if (!(this.description is null))
Result ^= Result << 5 ^ this.description.GetHashCode();
if (!(this.determiner is null))
Result ^= Result << 5 ^ this.determiner.GetHashCode();
if (!(this.locale is null))
Result ^= Result << 5 ^ this.locale.GetHashCode();
if (!(this.siteName is null))
Result ^= Result << 5 ^ this.siteName.GetHashCode();
if (!(this.localeAlternate is null))
{
foreach (string s in this.localeAlternate)
Result ^= Result << 5 ^ s.GetHashCode();
}
if (!(this.images is null))
{
foreach (ImageInformation Obj in this.images)
Result ^= Result << 5 ^ Obj.GetHashCode();
}
if (!(this.audio is null))
{
foreach (AudioInformation Obj in this.audio)
Result ^= Result << 5 ^ Obj.GetHashCode();
}
if (!(this.video is null))
{
foreach (VideoInformation Obj in this.video)
Result ^= Result << 5 ^ Obj.GetHashCode();
}
return Result;
}
}
}