Last active
August 29, 2015 14:06
-
-
Save kgiszewski/de30fa5f70514c2bffe7 to your computer and use it in GitHub Desktop.
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
public static IHtmlString TransformGlossaryTerms(this string input, ControllerContext context) | |
{ | |
if (String.IsNullOrWhiteSpace(input)) | |
{ | |
return new HtmlString(input); | |
} | |
var glossaryTerms = new List<string>(); | |
foreach (var term in NodeHelper.Instance.Glossary.Descendants("GlossaryTerm")) | |
{ | |
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 = NodeHelper.Instance.Glossary | |
.Descendants() | |
.Where(x => x.DocumentTypeAlias == "GlossaryTerm") | |
.FirstOrDefault(x => x.Name.ToLower() == term.ToLower()); | |
if (glossaryContent != null) | |
{ | |
var glossaryTerm = new GlossaryTerm() | |
{ | |
PopoverText = glossaryContent.GetPropertyValue<string>("text"), | |
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(); | |
} | |
} | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace foo.Umbraco.Extensions.Models | |
{ | |
public class GlossaryTerm | |
{ | |
public string Text { get; set; } | |
public string PopoverText { get; set; } | |
} | |
} | |
@inherits Umbraco.Web.Mvc.UmbracoViewPage<GlossaryTerm> | |
<a class="js-tooltip" href="#" data-toggle="tooltip" title="" data-original-title="@Model.PopoverText">@Model.Text</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment