Adding Common.Body.Summary template that displays a generated body excerpt

- Added body summary template
- Added an Ellipsize(int length) string extension
- Added an Excerpt(string markup, int length) HtmlHelper extension
- Added a LongestMatch method to ContentPartTemplateResult (pretty much a copy/paste from item) to get the fallback on filename functionality
- Changed the BodyDriver to try to display SummaryAdmin or Summary template variations on Body if the displayType fits

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-03-03 03:32:01 -08:00
parent 98b483f827
commit 1f417fbe23
7 changed files with 64 additions and 2 deletions

View File

@@ -20,11 +20,12 @@ namespace Orchard.Core.Common.Controllers {
// \/\/ Haackalicious on many accounts - don't copy what has been done here for the wrapper \/\/
protected override DriverResult Display(BodyAspect part, string displayType) {
var model = new BodyDisplayViewModel { BodyAspect = part, Text = BbcodeReplace(part.Text)};
var model = new BodyDisplayViewModel { BodyAspect = part, Text = BbcodeReplace(part.Text) };
return Combined(
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/ManageWrapperPre").Location("primary", "5") : null,
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/Manage").Location("primary", "5") : null,
ContentPartTemplate(model, TemplateName, Prefix).Location("primary", "5"),
ContentPartTemplate(model, TemplateName, Prefix).LongestMatch(displayType, "Summary", "SummaryAdmin").Location("primary", "5"),
Services.Authorizer.Authorize(Permissions.ChangeOwner) ? ContentPartTemplate(model, "Parts/ManageWrapperPost").Location("primary", "5") : null);
}

View File

@@ -0,0 +1,9 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<BodyDisplayViewModel>" %>
<%@ Import Namespace="Orchard.Core.Common.ViewModels"%>
<%--//doing excerpt generation on the way out for now so we don't stick ourselves with needing to regen excerpts for existing data
//also, doing this here, inline, until we have a pluggable processing model (both in and out)
//also, ...this is ugly--%>
<%=string.Format(
"<p>{0} {1}</p>",
Html.Excerpt(Model.Text, 200).ToString().Replace("\r\n", "</p>\r\n<p>"),
Html.ItemDisplayLink(T("[more]").ToString(), Model.BodyAspect.ContentItem)) %>

View File

@@ -225,6 +225,7 @@
<Content Include="Themes\Views\DisplayTemplates\Items\ContentItem.ascx" />
</ItemGroup>
<ItemGroup>
<Content Include="Common\Views\DisplayTemplates\Parts\Common.Body.Summary.ascx" />
<Content Include="Common\Views\DisplayTemplates\Parts\ManageWrapperPost.ascx" />
<Content Include="Common\Views\DisplayTemplates\Parts\ManageWrapperPre.ascx" />
<Content Include="Common\Views\DisplayTemplates\Parts\Manage.ascx" />

View File

@@ -1,3 +1,4 @@
using System.Linq;
using Orchard.ContentManagement.Handlers;
namespace Orchard.ContentManagement.Drivers {
@@ -34,5 +35,22 @@ namespace Orchard.ContentManagement.Drivers {
Position = position;
return this;
}
public ContentPartTemplateResult LongestMatch(string displayType, params string[] knownDisplayTypes) {
if (string.IsNullOrEmpty(displayType))
return this;
var longest = knownDisplayTypes.Aggregate("", (best, x) => {
if (displayType.StartsWith(x) && x.Length > best.Length) return x;
return best;
});
if (string.IsNullOrEmpty(longest))
return this;
TemplateName += "." + longest;
return this;
}
}
}

View File

@@ -0,0 +1,19 @@
using System.Linq;
using System.Text.RegularExpressions;
namespace Orchard.Extensions {
public static class StringExtensions {
public static string Ellipsize(this string text, int characterCount) {
return text.Ellipsize(characterCount, "&#160;&#8230;");
}
public static string Ellipsize(this string text, int characterCount, string ellipsis) {
var cleanTailRegex = new Regex(@"\s+\S*$");
if (string.IsNullOrEmpty(text) || characterCount < 0 || text.Length <= characterCount)
return text;
return cleanTailRegex.Replace(text.Substring(0, characterCount + 1), "") + ellipsis;
}
}
}

View File

@@ -3,9 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Orchard.Extensions;
using Orchard.Services;
using Orchard.Settings;
using Orchard.Utility;
@@ -88,6 +90,17 @@ namespace Orchard.Mvc.Html {
#endregion
#region Excerpt
public static MvcHtmlString Excerpt(this HtmlHelper html, string markup, int length) {
var tagRegex = new Regex("<[^<>]*>", RegexOptions.Singleline);
var text = html.Encode(tagRegex.Replace(markup, ""));
return MvcHtmlString.Create(text.Ellipsize(length));
}
#endregion
#region Format Date/Time
public static string DateTimeRelative(this HtmlHelper htmlHelper, DateTime? value, string defaultIfNull) {

View File

@@ -151,6 +151,7 @@
<Compile Include="Extensions\AreaFolders.cs" />
<Compile Include="Extensions\ExtensionFolders.cs" />
<Compile Include="Extensions\Loaders\AreaExtensionLoader.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="Extensions\UriExtensions.cs" />
<Compile Include="Mvc\AntiForgery\ValidateAntiForgeryTokenOrchardAttribute.cs" />
<Compile Include="Mvc\Extensions\UrlHelperExtensions.cs" />