Skip to content

Commit f7ab78d

Browse files
committed
Initial commit
0 parents  commit f7ab78d

13 files changed

Lines changed: 431 additions & 0 deletions

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[Bb]in/
2+
[Oo]bj/
3+
*.suo
4+
*.user
5+
*.userprefs
6+
_ReSharper.*
7+
*.ReSharper.user
8+
*.resharper.user
9+
.vs/
10+
.vscode/
11+
Backup/
12+
*.lock.json
13+
*.nuget.props
14+
*.nuget.targets
15+
*.orig
16+
packages/
17+
.DS_Store
18+
*.config
19+
*.nupkg
20+
*.nuspec
21+
*.trx
22+
*/TestResults/
23+
*/app.config

Contentstack.Utils.sln

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contentstack.Utils", "Contentstack.Utils\Contentstack.Utils.csproj", "{FC7C55F2-86F9-49B0-AA18-2B211230521A}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contentstack.Utils.Tests", "Contentstack.Utils.Tests\Contentstack.Utils.Tests.csproj", "{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{FC7C55F2-86F9-49B0-AA18-2B211230521A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{FC7C55F2-86F9-49B0-AA18-2B211230521A}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{FC7C55F2-86F9-49B0-AA18-2B211230521A}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{FC7C55F2-86F9-49B0-AA18-2B211230521A}.Release|Any CPU.Build.0 = Release|Any CPU
18+
{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{EB2B5E23-E45F-4C6C-BF98-FE3971DE4250}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
EndGlobal
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="Interfaces\" />
9+
<Folder Include="Models\" />
10+
<Folder Include="Enums\" />
11+
<Folder Include="Extensions\" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<PackageReference Include="HtmlAgilityPack" Version="1.11.28" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Contentstack.Utils.Enums
2+
{
3+
public enum EmbedItemType
4+
{
5+
/// <summary>
6+
/// This will specify the type of Embedded object as Asset
7+
/// </summary>
8+
Asset,
9+
10+
/// <summary>
11+
/// This will specify the type of Embedded object as Entry
12+
/// </summary>
13+
Entry
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
namespace Contentstack.Utils.Enums
3+
{
4+
public enum StyleType
5+
{
6+
/// <summary>
7+
/// This will specify the style type of Embedded object as Block
8+
/// </summary>
9+
Block,
10+
11+
/// <summary>
12+
/// This will specify the style type of Embedded object as Inline
13+
/// </summary>
14+
Inline,
15+
16+
/// <summary>
17+
/// This will specify the style type of Embedded object as Link
18+
/// </summary>
19+
Link,
20+
21+
/// <summary>
22+
/// This will specify the style type of Embedded object as Display
23+
/// </summary>
24+
Display,
25+
26+
/// <summary>
27+
/// This will specify the style type of Embedded object as Download
28+
/// </summary>
29+
Download
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Contentstack.Utils.Models;
2+
using HtmlAgilityPack;
3+
4+
namespace Contentstack.Utils.Extensions
5+
{
6+
public delegate void MetadataCallBack(Metadata metadata);
7+
8+
public static class HtmlDocumentExtension
9+
{
10+
public static void FindEmbeddedObject(this HtmlDocument htmlDocument, MetadataCallBack callBack)
11+
{
12+
var htmlNode = htmlDocument.DocumentNode.SelectNodes("//*[contains(@class, 'embedded-asset') or contains(@class, 'embedded-entry')]");
13+
if (htmlNode != null)
14+
{
15+
foreach (var node in htmlNode)
16+
{
17+
callBack(node);
18+
}
19+
}
20+
}
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
namespace Contentstack.Utils.Interfaces
3+
{
4+
public interface IEmbeddedObject
5+
{
6+
string Uid
7+
{
8+
get;
9+
set;
10+
}
11+
}
12+
13+
public interface IEmbeddedContentTypeUid: IEmbeddedObject
14+
{
15+
string ContentTypeUid
16+
{
17+
get;
18+
set;
19+
}
20+
}
21+
22+
public interface IEmbeddedEntry: IEmbeddedObject
23+
{
24+
string Title
25+
{
26+
get;
27+
set;
28+
}
29+
}
30+
31+
public interface IEmbeddedAsset: IEmbeddedObject
32+
{
33+
string Title
34+
{
35+
get;
36+
set;
37+
}
38+
39+
string FileName
40+
{
41+
get;
42+
set;
43+
}
44+
45+
string Url
46+
{
47+
get;
48+
set;
49+
}
50+
}
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Contentstack.Utils.Interfaces
5+
{
6+
public interface IEntryEmbedable
7+
{
8+
Dictionary<string, List<IEmbeddedContentTypeUid>> embeddedEntries
9+
{
10+
get;
11+
set;
12+
}
13+
14+
Dictionary<string, List<IEmbeddedAsset>> embeddedAssets
15+
{
16+
get;
17+
set;
18+
}
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Contentstack.Utils.Models;
2+
using Contentstack.Utils.Interfaces;
3+
namespace Contentstack.Utils.Interfaces
4+
{
5+
public interface IRenderable
6+
{
7+
string RenderOption(IEmbeddedObject entry, Metadata metadata);
8+
}
9+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using Contentstack.Utils.Enums;
4+
using HtmlAgilityPack;
5+
6+
[assembly: InternalsVisibleTo("Contentstack.Utils.Tests")]
7+
namespace Contentstack.Utils.Models
8+
{
9+
public struct Metadata
10+
{
11+
/// <summary>
12+
/// This will specify the type of Embedded object
13+
/// </summary>
14+
public EmbedItemType ItemType;
15+
16+
/// <summary>
17+
/// This will specify the style type of Embedded object
18+
/// </summary>
19+
public StyleType StyleType;
20+
21+
/// <summary>
22+
/// Uid of Embedded object
23+
/// </summary>
24+
public string ItemUid;
25+
26+
/// <summary>
27+
/// Content type for the Embedded object
28+
/// </summary>
29+
public string ContentTypeUid;
30+
31+
/// <summary>
32+
/// Text wrapped in embed tag
33+
/// </summary>
34+
public string Text;
35+
36+
/// <summary>
37+
/// Attributes collection for embed tag
38+
/// </summary>
39+
public HtmlAttributeCollection attributes;
40+
41+
/// <summary>
42+
/// Html string of embed tag
43+
/// </summary>
44+
internal string OuterHTML;
45+
46+
public static implicit operator Metadata(HtmlNode node)
47+
{
48+
StyleType styleType;
49+
if (node.Attributes["sys-style-type"] == null || !(Enum.TryParse(node.Attributes["sys-style-type"].Value, true, out styleType)))
50+
{
51+
styleType = StyleType.Block;
52+
}
53+
54+
EmbedItemType embedItemType;
55+
if (node.Attributes["type"] == null || !(Enum.TryParse(node.Attributes["type"].Value, true, out embedItemType)))
56+
{
57+
embedItemType = EmbedItemType.Entry;
58+
}
59+
60+
return new Metadata() {
61+
Text = node.InnerText ?? "",
62+
OuterHTML = node.OuterHtml ?? "",
63+
StyleType = styleType,
64+
ItemType = embedItemType,
65+
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 : ""),
66+
ContentTypeUid = node.Attributes["data-sys-content-type-uid"] != null ? node.Attributes["data-sys-content-type-uid"].Value : "",
67+
attributes = node.Attributes
68+
};
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)