After reading the blog post: “Highlighting Keywords Found In Search Results”. I realize that I should apply this utility for my existing code2code.info blog, you also do it for you by following my below instruction:
Prerequisite:
- BlogEngine (BE) source code version 2.5
- Visual Studio 2010
Note:Because I’ve been using BlogEngine v2.5 with .net 4.0 so I have to use Visual Studio 2010 to customize my existing blog. But you also can do it with using Visual Studio 2008 if you are using older version of BE
Step 1: Open Util.cs in BlogEngine.Core and paste the code below (end of line to easy to tracking or maintain)
///<summary>
/// Wraps matched strings in HTML span elements styled with a background-color
///</summary>
///<param name="text"></param>
///<param name="keywords">Comma-separated list of strings to be highlighted</param>
///<param name="cssClass">The Css color to apply</param>
///<param name="fullMatch">false for returning all matches, true for whole word matches only</param>
///<returns>string</returns>
public static string HighlightKeyWords(this string text, string keywords, string cssClass, bool fullMatch)
{
if (text == String.Empty || keywords == String.Empty || cssClass == String.Empty)
return text;
var words = keywords.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (!fullMatch)
return words.Select(word => word.Trim()).Aggregate(text,
(current, pattern) =>
Regex.Replace(current,
pattern,
string.Format("<span style=\"background-color:{0}\">{1}</span>",
cssClass,
'$0'),
RegexOptions.IgnoreCase));
return words.Select(word => '\\b' + word.Trim() + '\\b')
.Aggregate(text, (current, pattern) =>
Regex.Replace(current,
pattern,
string.Format("<span style=\"background-color:{0}\">{1}</span>",
cssClass,
'$0'),
RegexOptions.IgnoreCase));
}
Press F5 to Build BlogEngine.Core project.
Step 2: Open search.aspx and add the code like below:
<asp:repeater runat='server' id="rep">
<ItemTemplate>
<div class="searchresult">
<a href='<%# Eval('RelativeLink') %>"><%# Eval('Title').ToString().HighlightKeyWords(Request.QueryString['q'], 'yellow', true)%></a>
<span class="text"><%# GetContent((string)Eval('Description'), (string)Eval('Content')).HighlightKeyWords(Request.QueryString['q'], 'yellow', true)%></span>
<span class='type' runat='server' id='type' />
<span class="url"><%# ShortenUrl((String)Eval('RelativeLink')) %></span>
</div>
</ItemTemplate>
</asp:repeater>
Note: The 2nd parameter is style sheet class name, you have to define it by your refer color.
Step 3: Testing and deployment.
I have apply the highlight for title and content if it matches. Then you will see the result output like screenshot below:

Happy BE-ing...










