Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PageMetaData.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
6
7namespace Waher.Content.Html
8{
16 public class PageMetaData
17 {
18 private ImageInformation[] images = null;
19 private AudioInformation[] audio = null;
20 private VideoInformation[] video = null;
21 private string[] localeAlternate = null;
22 private string title = null;
23 private string type = null;
24 private string url = null;
25 private string description = null;
26 private string determiner = null;
27 private string locale = null;
28 private string siteName = null;
29
37 public PageMetaData()
38 {
39 }
40
49 {
50 List<ImageInformation> Images = null;
51 List<AudioInformation> Audio = null;
52 List<VideoInformation> Video = null;
53 List<string> LocaleAlternate = null;
54 ImageInformation LastImage = null;
55 AudioInformation LastAudio = null;
56 VideoInformation LastVideo = null;
57 string Name;
58 string Value;
59
60 if (!(Doc.Meta is null))
61 {
62 foreach (Meta Meta in Doc.Meta)
63 {
65 continue;
66
67 Name = Value = null;
68
70 {
71 foreach (HtmlAttribute Attr in Meta.Attributes)
72 {
73 switch (Attr.Name)
74 {
75 case "property":
76 case "name":
77 Name = Attr.Value;
78 break;
79
80 case "content":
81 Value = Attr.Value;
82 break;
83 }
84 }
85 }
86
87 if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Value))
88 continue;
89
90 switch (Name)
91 {
92 case "og:title":
93 this.title = Value;
94 break;
95
96 case "twitter:title":
97 if (string.IsNullOrEmpty(this.title))
98 this.title = Value;
99 break;
100
101 case "og:type":
102 this.type = Value;
103 break;
104
105 case "twitter:card":
106 if (string.IsNullOrEmpty(this.type))
107 this.type = Value;
108 break;
109
110 case "og:url":
111 this.url = Value;
112 break;
113
114 case "og:image":
115 case "og:image:url":
116 case "twitter:image":
117 if (!(Images is null))
118 {
119 LastImage = null;
120
121 foreach (ImageInformation Image in Images)
122 {
123 if (Image.Url == Value)
124 {
125 LastImage = Image;
126 break;
127 }
128 }
129
130 if (!(LastImage is null))
131 break;
132 }
133
134 LastImage = new ImageInformation()
135 {
136 Url = Value
137 };
138
139 if (Images is null)
140 Images = new List<ImageInformation>();
141
142 Images.Add(LastImage);
143 break;
144
145 case "og:image:secure_url":
146 if (!(LastImage is null))
147 LastImage.SecureUrl = Value;
148 break;
149
150 case "og:image:type":
151 if (!(LastImage is null))
152 LastImage.ContentType = Value;
153 break;
154
155 case "og:image:width":
156 if (!(LastImage is null) && int.TryParse(Value, out int i))
157 LastImage.Width = i;
158 break;
159
160 case "og:image:height":
161 if (!(LastImage is null) && int.TryParse(Value, out i))
162 LastImage.Height = i;
163 break;
164
165 case "og:image:alt":
166 case "twitter:image:alt":
167 if (!(LastImage is null))
168 LastImage.Description = Value;
169 break;
170
171 case "og:audio":
172 LastAudio = new AudioInformation()
173 {
174 Url = Value
175 };
176
177 if (Audio is null)
178 Audio = new List<AudioInformation>();
179
180 Audio.Add(LastAudio);
181 break;
182
183 case "og:audio:secure_url":
184 if (!(LastAudio is null))
185 LastAudio.SecureUrl = Value;
186 break;
187
188 case "og:audio:type":
189 if (!(LastAudio is null))
190 LastAudio.ContentType = Value;
191 break;
192
193 case "og:description":
194 this.description = Value;
195 break;
196
197 case "twitter:description":
198 case "description":
199 if (string.IsNullOrEmpty(this.description))
200 this.description = Value;
201 break;
202
203 case "og:determiner":
204 this.determiner = Value;
205 break;
206
207 case "og:locale":
208 this.locale = Value;
209 break;
210
211 case "og:locale:alternate":
212 if (LocaleAlternate is null)
213 LocaleAlternate = new List<string>();
214
215 LocaleAlternate.Add(Value);
216 break;
217
218 case "og:site_name":
219 this.siteName = Value;
220 break;
221
222 case "og:video":
223 LastVideo = new VideoInformation()
224 {
225 Url = Value
226 };
227
228 if (Video is null)
229 Video = new List<VideoInformation>();
230
231 Video.Add(LastVideo);
232 break;
233
234 case "og:video:secure_url":
235 if (!(LastVideo is null))
236 LastVideo.SecureUrl = Value;
237 break;
238
239 case "og:video:type":
240 if (!(LastVideo is null))
241 LastVideo.ContentType = Value;
242 break;
243
244 case "og:video:width":
245 if (!(LastVideo is null) && int.TryParse(Value, out i))
246 LastVideo.Width = i;
247 break;
248
249 case "og:video:height":
250 if (!(LastVideo is null) && int.TryParse(Value, out i))
251 LastVideo.Height = i;
252 break;
253 }
254 }
255 }
256
257 if (string.IsNullOrEmpty(this.title) && !(Doc.Title is null))
258 this.title = Doc.Title.InnerText.Trim();
259
260 this.images = Images?.ToArray();
261 this.audio = Audio?.ToArray();
262 this.video = Video?.ToArray();
263 this.localeAlternate = LocaleAlternate?.ToArray();
264 }
265
270 {
271 get => this.images;
272 set => this.images = value;
273 }
274
279 {
280 get => this.audio;
281 set => this.audio = value;
282 }
283
288 {
289 get => this.video;
290 set => this.video = value;
291 }
292
296 public string[] LocaleAlternate
297 {
298 get => this.localeAlternate;
299 set => this.localeAlternate = value;
300 }
301
305 public string Title
306 {
307 get => this.title;
308 set => this.title = value;
309 }
310
314 public string Type
315 {
316 get => this.type;
317 set => this.type = value;
318 }
319
323 public string Url
324 {
325 get => this.url;
326 set => this.url = value;
327 }
328
332 public string Description
333 {
334 get => this.description;
335 set => this.description = value;
336 }
337
341 public string Determiner
342 {
343 get => this.determiner;
344 set => this.determiner = value;
345 }
346
350 public string Locale
351 {
352 get => this.locale;
353 set => this.locale = value;
354 }
355
359 public string SiteName
360 {
361 get => this.siteName;
362 set => this.siteName = value;
363 }
364
366 public override bool Equals(object obj)
367 {
369 int i, c;
370
371 if ((this is null) ^ (Meta is null))
372 return false;
373
374 if (this is null)
375 return true;
376
377 if (this.title != Meta.title ||
378 this.type != Meta.type ||
379 this.url != Meta.url ||
380 this.description != Meta.description ||
381 this.determiner != Meta.determiner ||
382 this.locale != Meta.locale ||
383 this.siteName != Meta.siteName)
384 {
385 return false;
386 }
387
388 if ((this.localeAlternate is null) ^ (Meta.localeAlternate is null))
389 return false;
390
391 if (!(this.localeAlternate is null))
392 {
393 if ((c = this.localeAlternate.Length) != Meta.localeAlternate.Length)
394 return false;
395
396 for (i = 0; i < c; i++)
397 {
398 if (this.localeAlternate[i] != Meta.localeAlternate[i])
399 return false;
400 }
401 }
402
403 if ((this.images is null) ^ (Meta.images is null))
404 return false;
405
406 if (!(this.images is null))
407 {
408 if ((c = this.images.Length) != Meta.images.Length)
409 return false;
410
411 for (i = 0; i < c; i++)
412 {
413 if (!this.images[i].Equals(Meta.images[i]))
414 return false;
415 }
416 }
417
418 if ((this.audio is null) ^ (Meta.audio is null))
419 return false;
420
421 if (!(this.audio is null))
422 {
423 if ((c = this.audio.Length) != Meta.audio.Length)
424 return false;
425
426 for (i = 0; i < c; i++)
427 {
428 if (!this.audio[i].Equals(Meta.audio[i]))
429 return false;
430 }
431 }
432
433 if ((this.video is null) ^ (Meta.video is null))
434 return false;
435
436 if (!(this.video is null))
437 {
438 if ((c = this.video.Length) != Meta.video.Length)
439 return false;
440
441 for (i = 0; i < c; i++)
442 {
443 if (!this.video[i].Equals(Meta.video[i]))
444 return false;
445 }
446 }
447
448 return true;
449 }
450
452 public override int GetHashCode()
453 {
454 int Result = 0;
455
456 if (!(this.title is null))
457 Result = this.title.GetHashCode();
458
459 if (!(this.type is null))
460 Result ^= Result << 5 ^ this.type.GetHashCode();
461
462 if (!(this.url is null))
463 Result ^= Result << 5 ^ this.url.GetHashCode();
464
465 if (!(this.description is null))
466 Result ^= Result << 5 ^ this.description.GetHashCode();
467
468 if (!(this.determiner is null))
469 Result ^= Result << 5 ^ this.determiner.GetHashCode();
470
471 if (!(this.locale is null))
472 Result ^= Result << 5 ^ this.locale.GetHashCode();
473
474 if (!(this.siteName is null))
475 Result ^= Result << 5 ^ this.siteName.GetHashCode();
476
477 if (!(this.localeAlternate is null))
478 {
479 foreach (string s in this.localeAlternate)
480 Result ^= Result << 5 ^ s.GetHashCode();
481 }
482
483 if (!(this.images is null))
484 {
485 foreach (ImageInformation Obj in this.images)
486 Result ^= Result << 5 ^ Obj.GetHashCode();
487 }
488
489 if (!(this.audio is null))
490 {
491 foreach (AudioInformation Obj in this.audio)
492 Result ^= Result << 5 ^ Obj.GetHashCode();
493 }
494
495 if (!(this.video is null))
496 {
497 foreach (VideoInformation Obj in this.video)
498 Result ^= Result << 5 ^ Obj.GetHashCode();
499 }
500
501 return Result;
502 }
503
504 }
505}
string Name
Attribute name.
string Value
Attribute value.
Title Title
First TITLE element of document, if found, null otherwise.
Definition: HtmlDocument.cs:96
IEnumerable< Meta > Meta
META elements found in document, or null if none found.
IEnumerable< HtmlAttribute > Attributes
Attributes, or null if none.
Definition: HtmlElement.cs:92
bool HasAttributes
If the element has attributes.
Definition: HtmlElement.cs:82
Audio information, as defined by the Open Graph protocol.
Image information, as defined by the Open Graph protocol.
Video information, as defined by the Open Graph protocol.
Contains meta-data about a page.
Definition: PageMetaData.cs:17
PageMetaData()
Contains meta-data about a page.
Definition: PageMetaData.cs:37
ImageInformation[] Images
Image representing page.
string Description
A description of the page.
override bool Equals(object obj)
PageMetaData(HtmlDocument Doc)
Contains meta-data about a page.
Definition: PageMetaData.cs:48
string Determiner
A word that appears before the title.
string Url
Canonical URL of page.
string Locale
Locale of information.
string SiteName
Name of web site publishing page
string[] LocaleAlternate
Alternate locales