-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadata.cs
More file actions
111 lines (97 loc) · 3.78 KB
/
Metadata.cs
File metadata and controls
111 lines (97 loc) · 3.78 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
using System;
using System.Runtime.CompilerServices;
using Contentstack.Utils.Enums;
using HtmlAgilityPack;
[assembly: InternalsVisibleTo("Contentstack.Utils.Tests")]
namespace Contentstack.Utils.Models
{
public struct Metadata
{
/// <summary>
/// This will specify the type of Embedded object
/// </summary>
public EmbedItemType ItemType;
/// <summary>
/// This will specify the style type of Embedded object
/// </summary>
public StyleType StyleType;
/// <summary>
/// Uid of Embedded object
/// </summary>
public string ItemUid;
/// <summary>
/// Content type for the Embedded object
/// </summary>
public string ContentTypeUid;
/// <summary>
/// Text wrapped in embed tag
/// </summary>
public string Text;
/// <summary>
/// Attributes collection for embed tag
/// </summary>
public object attributes;
/// <summary>
/// Html string of embed tag
/// </summary>
internal string OuterHTML;
public static implicit operator Metadata(HtmlNode node)
{
StyleType styleType;
if (node.Attributes["sys-style-type"] == null || !(Enum.TryParse(node.Attributes["sys-style-type"].Value, true, out styleType)))
{
styleType = StyleType.Block;
}
EmbedItemType embedItemType;
if (node.Attributes["type"] == null || !(Enum.TryParse(node.Attributes["type"].Value, true, out embedItemType)))
{
embedItemType = EmbedItemType.Entry;
}
return new Metadata() {
Text = node.InnerText ?? "",
OuterHTML = node.OuterHtml ?? "",
StyleType = styleType,
ItemType = embedItemType,
ItemUid = node.Attributes["data-sys-entry-uid"] != null ? node.Attributes["data-sys-entry-uid"].Value : (node.Attributes["data-sys-asset-uid"] != null ? node.Attributes["data-sys-asset-uid"].Value : ""),
ContentTypeUid = node.Attributes["data-sys-content-type-uid"] != null ? node.Attributes["data-sys-content-type-uid"].Value : "",
attributes = node.Attributes
};
}
public static implicit operator Metadata(Node node)
{
StyleType styleType;
if (!node.attrs.ContainsKey("display-type") || !(Enum.TryParse((string)node.attrs["display-type"], true, out styleType)))
{
styleType = StyleType.Block;
}
EmbedItemType embedItemType;
if (!node.attrs.ContainsKey("type") || !(Enum.TryParse((string)node.attrs["type"], true, out embedItemType)))
{
embedItemType = EmbedItemType.Entry;
}
string text = "";
if (node.children != null && node.children.Count > 0 && node.children[0].GetType() == typeof(TextNode))
{
text = ((TextNode)node.children[0]).text;
}
string itemUID = "";
if (node.attrs.ContainsKey("entry-uid"))
{
itemUID = (string)node.attrs["entry-uid"];
}else if (node.attrs.ContainsKey("asset-uid"))
{
itemUID = (string)node.attrs["asset-uid"];
}
return new Metadata()
{
Text = text,
OuterHTML = "",
StyleType = styleType,
ItemType = embedItemType,
ItemUid = itemUID,
ContentTypeUid = node.attrs.ContainsKey("content-type-uid") ? (string)node.attrs["content-type-uid"] : "",
attributes = node.attrs
};
}
}
}