Skip to content

Commit

Permalink
Add HtmlHelperExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
sitefinitysteve committed Nov 4, 2016
1 parent 4647aea commit 97cc3b8
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
A random bunch of MVC Feather controls and extensions to improve your sitefinity experience
</summary>
<projectUrl>https://github.com/sitefinitysteve/RandomSiteControlsMVC</projectUrl>
<dependencies>
<dependency id="HtmlAgilityPack"/>
</dependencies>
</metadata>
</package>
129 changes: 129 additions & 0 deletions Extensions/MVCHtmlExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity;
using Telerik.Sitefinity.GenericContent.Model;
using HtmlAgilityPack;
using RandomSiteControlsMVC.Helpers;
using Telerik.Sitefinity.Web.Utilities;
using Telerik.Sitefinity.Modules.GenericContent;
using System.IO;
using System.Web.Routing;
using Telerik.Sitefinity.Frontend.Media.Mvc.Controllers;
using Telerik.Sitefinity.Frontend.Mvc.Infrastructure.Controllers;
using Telerik.Sitefinity.Frontend.Media.Mvc.Models.Image;
using Telerik.Sitefinity.Mvc;
using System.Diagnostics;
using RandomSiteControlsMVC.MVC.Models.InlineMarkupHelpers;

namespace Telerik.Sitefinity
{
public static class SFSHtml
{
private const string _mediaItemPrefix = "sfvrsn";

public static IHtmlString EnhanceRaw(string html, string imageViewPath = "/Views/Image/Image.Inline.cshtml", string documentViewPath = "/Views/Document/DocumentLink.Inline.cshtml")
{
var result = String.Empty;

if (html.Contains(_mediaItemPrefix))
{
try
{
var doc = new HtmlDocument();
doc.LoadHtml(html);

//Parse images
var images = doc.DocumentNode.SelectNodes("//img[contains(@src, '" + _mediaItemPrefix + "')]");
if (images != null)
{
foreach (var node in images)
{
//Find the id
var imageRef = new SfImageLink(node.GetAttributeValue("src", "#"));

if (imageRef.FoundDataItem())
{
if (HttpContext.Current != null)
{
var markup = SFSHtml.GetRazorViewAsString(new ImageController(), imageRef.DataItem, imageViewPath);
var newNode = HtmlNode.CreateNode(markup);
node.ParentNode.ReplaceChild(newNode, node);
}

}
}
}

//Parse documents
var documents = doc.DocumentNode.SelectNodes("//a[contains(@href, '" + _mediaItemPrefix + "')]");
if (documents != null)
{
foreach (var node in documents)
{
var src = node.GetAttributeValue("href", "#");

if (src.Contains("?{0}".Arrange(_mediaItemPrefix)) && src != "#")
{
//Find the id
var docRef = new SfDocLink(src);

if (docRef.FoundDataItem())
{
var markup = SFSHtml.GetRazorViewAsString(new DocumentController(), docRef.DataItem, documentViewPath);
var newNode = HtmlNode.CreateNode(markup);
node.ParentNode.ReplaceChild(newNode, node);
}
}
}
}

//Parse pages
/*
var pages = doc.DocumentNode.SelectNodes("//a[starts-with(@href, '/')]);
if (pages != null)
{
foreach (var node in pages)
{
//TODO
}
}
*/

var fixedHtml = doc.DocumentNode.OuterHtml;
result = LinkParser.ResolveLinks(fixedHtml, DynamicLinksParser.GetContentUrl, null, false);

return MvcHtmlString.Create(result);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
//Problem, just show something
return MvcHtmlString.Create(html);
}
}
else
{
//No version
return MvcHtmlString.Create(html);
}
}

public static string GetRazorViewAsString(Controller controller, object model, string viewPath)
{
var st = new StringWriter();
var context = new HttpContextWrapper(HttpContext.Current);
var routeData = new RouteData();
var controllerContext = new ControllerContext(new RequestContext(context, routeData), new ImageController());

var razor = new RazorView(controllerContext, "~/Frontend-Assembly/Telerik.Sitefinity.Frontend/Mvc{0}".Arrange(viewPath), null, false, null);
razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), st), st);

return st.ToString();
}
}
}
64 changes: 64 additions & 0 deletions MVC/Models/InlineMarkupHelpers/SfDocLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Frontend.Media.Mvc.Models.Document;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Modules.Libraries.Documents;

namespace RandomSiteControlsMVC.MVC.Models.InlineMarkupHelpers
{

public class SfDocLink
{
public SfDocLink(string url)
{
url = url.Split('?').GetValue(0).ToString();
var parts = url.TrimStart('/').Split('/');

int extensionPosition = url.LastIndexOf(".");
if (extensionPosition >= 0)
url = url.Substring(0, extensionPosition);

this.Type = parts.GetValue(0).ToString();
this.Source = parts.GetValue(1).ToString();
this.Library = parts.GetValue(2).ToString();
this.UrlName = parts.GetValue(parts.Length - 1).ToString();

this.ItemUrl = url;

this.ResolveMediaItem();
}

#region Methods
private void ResolveMediaItem()
{
var librariesManager = LibrariesManager.GetManager();

var document = librariesManager.GetDocuments().FirstOrDefault(x => x.ItemDefaultUrl == this.ItemUrl);

if (document != null)
{
this.DataItem = document;
}
}

public bool FoundDataItem()
{
return DataItem == null ? false : true;
}
#endregion

#region Properties

public string Type { get; set; }
public string Library { get; set; }
public string Source { get; set; }
public string UrlName { get; set; }
public string ItemUrl { get; set; }
public Document DataItem { get; set; }
#endregion
}
}
63 changes: 63 additions & 0 deletions MVC/Models/InlineMarkupHelpers/SfImageLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Frontend.Media.Mvc.Models.Image;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;

namespace RandomSiteControlsMVC.MVC.Models.InlineMarkupHelpers
{

public class SfImageLink
{
public SfImageLink(string url)
{
url = url.Split('?').GetValue(0).ToString();
var parts = url.TrimStart('/').Split('/');

int extensionPosition = url.LastIndexOf(".");
if (extensionPosition >= 0)
url = url.Substring(0, extensionPosition);

this.Type = parts.GetValue(0).ToString();
this.Source = parts.GetValue(1).ToString();
this.Library = parts.GetValue(2).ToString();
this.UrlName = parts.GetValue(parts.Length - 1).ToString();

this.ItemUrl = url;

this.ResolveMediaItem();
}

#region Methods
private void ResolveMediaItem()
{
var librariesManager = LibrariesManager.GetManager();

var image = librariesManager.GetImages().FirstOrDefault(x => x.ItemDefaultUrl == this.ItemUrl);

if (image != null)
{
this.DataItem = image;
}
}

public bool FoundDataItem()
{
return DataItem == null ? false : true;
}
#endregion

#region Properties

public string Type { get; set; }
public string Library { get; set; }
public string Source { get; set; }
public string UrlName { get; set; }
public string ItemUrl { get; set; }
public Image DataItem { get; set; }
#endregion
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("9.1.6110.1")]
[assembly: AssemblyFileVersion("9.1.6110.1")]
[assembly: AssemblyVersion("9.2.6202.0")]
[assembly: AssemblyFileVersion("9.2.6202.0")]

//INSTALLER
[assembly: PreApplicationStartMethod(typeof(Installer), "PreApplicationStart")]
Expand Down
12 changes: 11 additions & 1 deletion RandomSiteControlsMVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack, Version=1.4.9.5, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9.5\lib\Net45\HtmlAgilityPack.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Web\bin\Microsoft.IdentityModel.dll</HintPath>
Expand Down Expand Up @@ -88,6 +92,9 @@
<HintPath>..\Web\bin\Telerik.Sitefinity.Frontend.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Telerik.Sitefinity.Frontend.Media">
<HintPath>..\Web\bin\Telerik.Sitefinity.Frontend.Media.dll</HintPath>
</Reference>
<Reference Include="Telerik.Sitefinity.Model">
<HintPath>..\Web\bin\Telerik.Sitefinity.Model.dll</HintPath>
<Private>False</Private>
Expand All @@ -112,6 +119,7 @@
<Compile Include="Extensions\GuidExtensions.cs" />
<Compile Include="Extensions\HierarchicalTaxonExtensions.cs" />
<Compile Include="Extensions\MarkupExtensions.cs" />
<Compile Include="Extensions\MVCHtmlExtensions.cs" />
<Compile Include="Extensions\PublishingExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\TaxonExtensions.cs" />
Expand All @@ -128,6 +136,8 @@
<Compile Include="MVC\Models\ContentLiteral\ContentLiteralModel.cs" />
<Compile Include="MVC\Models\DocumentTree\DocumentTreeNode.cs" />
<Compile Include="MVC\Models\DocumentTree\StyleEnum.cs" />
<Compile Include="MVC\Models\InlineMarkupHelpers\SfDocLink.cs" />
<Compile Include="MVC\Models\InlineMarkupHelpers\SfImageLink.cs" />
<Compile Include="MVC\Models\Markdown\MarkdownModel.cs" />
<Compile Include="MVC\Models\TabStrip\Tab.cs" />
<Compile Include="MVC\Models\TabStrip\TabStripModel.cs" />
Expand Down Expand Up @@ -194,8 +204,8 @@
<EmbeddedResource Include="MVC\Views\DocumentTree\Resources\documenttree.less" />
<EmbeddedResource Include="MVC\Views\DocumentTree\DesignerView.Simple.json" />
<EmbeddedResource Include="MVC\Views\DocumentTree\DesignerView.Simple.cshtml" />
<None Include="packages.config" />
<None Include="README.md" />
<None Include="Settings.aiis" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="MVC\Scripts\ContentLiteral\designerview-simple.js" />
Expand Down
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.9.5" targetFramework="net45" />
</packages>

0 comments on commit 97cc3b8

Please sign in to comment.