Last active
August 29, 2015 14:06
-
-
Save rbaty-barr/a79c8f36790fb8fe8b4e to your computer and use it in GitHub Desktop.
SegalHelpers
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
using Umbraco.Core.Models; | |
using Umbraco.Core; | |
using Umbraco.Web.Models; | |
using Umbraco.Web; | |
using Umbraco.Core.Logging; | |
using HtmlAgilityPack; | |
using System.Text.RegularExpressions; | |
using System.IO; | |
namespace trainingNetWWW.App_Code | |
{ | |
public class GlossaryTerm | |
{ | |
public string Text { get; set; } | |
public string PopoverText { get; set; } | |
} | |
public static class segalHelpers | |
{ | |
public static IHtmlString TransformGlossaryTerms(this string input, ControllerContext context) | |
{ | |
if (String.IsNullOrWhiteSpace(input)) | |
{ | |
return new HtmlString(input); | |
} | |
var glossaryTerms = new List<string>(); | |
//var umbHelper = new UmbracoHelper(UmbracoContext.Current); | |
var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current); | |
var TermNode = umbracoHelper.TypedContent(1160); | |
foreach (var term in TermNode.Descendants("HoverTerm")) | |
{ | |
glossaryTerms.Add(term.Name.ToLower()); | |
} | |
var document = new HtmlDocument(); | |
document.LoadHtml(input); | |
var pTags = document.DocumentNode.SelectNodes("//p"); | |
if (pTags == null) | |
return new HtmlString(input); | |
//find all the unique uses of glossary terms | |
var regex = new Regex(@"\b(" + string.Join("|", glossaryTerms.Select(Regex.Escape).ToArray()) + @"\b)", RegexOptions.IgnoreCase); | |
var termsUsed = new List<string>(); | |
foreach (Match m in regex.Matches(input)) | |
{ | |
if (!termsUsed.Contains(m.Value)) | |
{ | |
//LogHelper.Info<GlossaryTerm>("Using: " + m.Value); | |
termsUsed.Add(m.Value); | |
} | |
} | |
//build a dictionary | |
var dictionary = new Dictionary<string, string>(); | |
foreach (var term in termsUsed) | |
{ | |
var umbHelper = new UmbracoHelper(UmbracoContext.Current); | |
var glossaryContent = TermNode | |
.Descendants() | |
.Where(x => x.DocumentTypeAlias == "HoverTerm") | |
.FirstOrDefault(x => x.Name.ToLower() == term.ToLower()); | |
if (glossaryContent != null) | |
{ | |
var glossaryTerm = new GlossaryTerm() | |
{ | |
PopoverText = glossaryContent.GetPropertyValue<string>("bodyText"), | |
Text = term | |
}; | |
dictionary[term] = RenderRazorViewToString(context, "~/Views/Partials/Popovers/Popover.cshtml", glossaryTerm).Trim(); | |
} | |
else | |
{ | |
LogHelper.Info<GlossaryTerm>("Couldn't find a term in the content: " + term.ToLower()); | |
} | |
} | |
foreach (var pTag in pTags) | |
{ | |
var pTagInner = pTag.InnerHtml; | |
foreach (var term in termsUsed) | |
{ | |
var pattern = string.Format(@"\b{0}(?=(?:[^""]*""[^""]*"")*[^""]*\Z)\b", Regex.Escape(term)); | |
//LogHelper.Info<GlossaryTerm>("About to regex: " + term); | |
if (dictionary.ContainsKey(term)) | |
{ | |
pTagInner = Regex.Replace(pTagInner, pattern, dictionary[term]); | |
} | |
else | |
{ | |
LogHelper.Info<GlossaryTerm>(term + " not in dictionary"); | |
} | |
} | |
pTag.ParentNode.ReplaceChild(HtmlTextNode.CreateNode("<p>" + pTagInner + "</p>"), pTag); | |
} | |
return new HtmlString(document.DocumentNode.OuterHtml); | |
} | |
public static string RenderRazorViewToString(ControllerContext context, string viewName, object model) | |
{ | |
using (var sw = new StringWriter()) | |
{ | |
var viewResult = ViewEngines.Engines.FindPartialView(context, viewName); | |
var viewContext = new ViewContext(context, viewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), sw); | |
viewResult.View.Render(viewContext, sw); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment