-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4647aea
commit 97cc3b8
Showing
7 changed files
with
276 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |