#5407: Layout elements now evaluate tokens.

This commit is contained in:
Sipke Schoorstra
2015-06-29 14:01:58 +03:00
parent e10ba8126a
commit 5b40567b6d
73 changed files with 406 additions and 217 deletions

View File

@@ -1,16 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class HeadingElementDriver : ElementDriver<Heading> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
public HeadingElementDriver(IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
private readonly IElementFilterProcessor _processor;
public HeadingElementDriver(IElementFilterProcessor processor) {
_processor = processor;
}
protected override EditorResult OnBuildEditor(Heading element, ElementEditorContext context) {
@@ -30,16 +30,8 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Heading element, ElementDisplayContext context) {
var text = element.Content;
var flavor = "html";
var processedText = ApplyHtmlFilters(text, flavor);
context.ElementShape.ProcessedText = processedText;
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
context.ElementShape.Level = element.Level;
}
private string ApplyHtmlFilters(string content, string flavor) {
return _htmlFilters.Aggregate(content, (t, filter) => filter.ProcessContent(t, flavor));
}
}
}

View File

@@ -1,16 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class HtmlElementDriver : ElementDriver<Html> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
public HtmlElementDriver(IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
private readonly IElementFilterProcessor _processor;
public HtmlElementDriver(IElementFilterProcessor processor) {
_processor = processor;
}
protected override EditorResult OnBuildEditor(Html element, ElementEditorContext context) {
@@ -28,15 +28,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Html element, ElementDisplayContext context) {
var text = element.Content;
var flavor = "html";
var processedText = ApplyHtmlFilters(text, flavor);
context.ElementShape.ProcessedText = processedText;
}
private string ApplyHtmlFilters(string content, string flavor) {
return _htmlFilters.Aggregate(content, (t, filter) => filter.ProcessContent(t, flavor));
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
}
}
}

View File

@@ -1,12 +1,18 @@
using Orchard.Environment.Extensions;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using MarkdownElement = Orchard.Layouts.Elements.Markdown;
namespace Orchard.Layouts.Drivers {
[OrchardFeature("Orchard.Layouts.Markdown")]
public class MarkdownElementDriver : ElementDriver<MarkdownElement> {
private readonly IElementFilterProcessor _processor;
public MarkdownElementDriver(IElementFilterProcessor processor) {
_processor = processor;
}
protected override EditorResult OnBuildEditor(MarkdownElement element, ElementEditorContext context) {
var viewModel = new MarkdownEditorViewModel {
@@ -23,11 +29,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(MarkdownElement element, ElementDisplayContext context) {
context.ElementShape.ProcessedContent = ToHtml(element.Content);
}
private string ToHtml(string markdown) {
return new MarkdownSharp.Markdown().Transform(markdown);
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "markdown", context.GetTokenData());
}
}
}

View File

@@ -3,11 +3,19 @@ using System.Linq;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class ParagraphElementDriver : ElementDriver<Paragraph> {
private readonly IElementFilterProcessor _processor;
public ParagraphElementDriver(IElementFilterProcessor processor) {
_processor = processor;
}
protected override EditorResult OnBuildEditor(Paragraph element, ElementEditorContext context) {
var viewModel = new ParagraphEditorViewModel {
Text = element.Content
@@ -18,8 +26,12 @@ namespace Orchard.Layouts.Drivers {
context.Updater.TryUpdateModel(viewModel, context.Prefix, null, null);
element.Content = viewModel.Text;
}
return Editor(context, editor);
}
protected override void OnDisplaying(Paragraph element, ElementDisplayContext context) {
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "html", context.GetTokenData());
}
}
}

View File

@@ -233,27 +233,27 @@ namespace Orchard.Layouts.Drivers {
Title: T("Items to display"),
Value: "0",
Description: T("The number of items to display. Enter 0 for no limit. When using pagination, this is the number of items per page."),
Classes: new[] { "text", "medium", "tokenized" }),
Classes: new[] { "text", "medium" }),
_Skip: shape.Textbox(
Id: "Skip",
Name: "Skip",
Title: T("Offset"),
Value: "0",
Description: T("The number of items to skip (e.g., if 2 is entered, the first 2 items won't be diplayed)."),
Classes: new[] { "text", "medium", "tokenized" }),
Classes: new[] { "text", "medium" }),
_MaxItems: shape.Textbox(
Id: "MaxItems",
Name: "MaxItems",
Title: T("MaxItems items"),
Value: "20",
Description: T("Maximum number of items which can be queried at once. Use 0 for unlimited. This is only used as a failsafe when the number of items comes from a user-provided source such as the query string."),
Classes: new[] { "text", "medium", "tokenized" }),
Classes: new[] { "text", "medium" }),
_PagerSuffix: shape.Textbox(
Id: "PagerSuffix",
Name: "PagerSuffix",
Title: T("Suffix"),
Description: T("Optional. Provide a suffix to use when multiple pagers are displayed on the same page, e.g., when using multiple Projection Widgets, or to define alternates."),
Classes: new[] { "text", "medium", "tokenized" }),
Classes: new[] { "text", "medium" }),
_DisplayPager: shape.Checkbox(
Id: "DisplayPager",
Name: "DisplayPager",

View File

@@ -1,16 +1,16 @@
using System.Collections.Generic;
using System.Linq;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Elements;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Services;
using Orchard.Layouts.ViewModels;
using Orchard.Services;
namespace Orchard.Layouts.Drivers {
public class TextElementDriver : ElementDriver<Text> {
private readonly IEnumerable<IHtmlFilter> _htmlFilters;
public TextElementDriver(IEnumerable<IHtmlFilter> htmlFilters) {
_htmlFilters = htmlFilters;
private readonly IElementFilterProcessor _processor;
public TextElementDriver(IElementFilterProcessor processor) {
_processor = processor;
}
protected override EditorResult OnBuildEditor(Text element, ElementEditorContext context) {
@@ -28,15 +28,7 @@ namespace Orchard.Layouts.Drivers {
}
protected override void OnDisplaying(Text element, ElementDisplayContext context) {
var text = element.Content;
var flavor = "textarea";
var processedText = ApplyHtmlFilters(text, flavor);
context.ElementShape.ProcessedText = processedText;
}
private string ApplyHtmlFilters(string content, string flavor) {
return _htmlFilters.Aggregate(content, (t, filter) => filter.ProcessContent(t, flavor));
context.ElementShape.ProcessedContent = _processor.ProcessContent(element.Content, "textarea", context.GetTokenData());
}
}
}

View File

@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using Orchard.Environment.Extensions;
using Orchard.Services;
using Orchard.Layouts.Services;
using Orchard.Tokens;
namespace Orchard.Layouts.Filters {
// TODO: Fix the version that lives in Orchard.Tokens.Filters.
[OrchardFeature("Orchard.Layouts.Tokens")]
public class TokensFilter : IHtmlFilter {
public class TokensFilter : IElementFilter {
private readonly ITokenizer _tokenizer;
@@ -16,20 +15,18 @@ namespace Orchard.Layouts.Filters {
}
public string ProcessContent(string text, string flavor) {
return TokensReplace(text);
return ProcessContent(text, flavor, new Dictionary<string, object>());
}
private string TokensReplace(string text) {
public string ProcessContent(string text, string flavor, IDictionary<string, object> context) {
if (String.IsNullOrEmpty(text))
return "";
if (!text.Contains("#{")) {
return text;
}
var data = new Dictionary<string, object>();
text = _tokenizer.Replace(text, data, new ReplaceOptions {Encoding = ReplaceOptions.NoEncode});
text = _tokenizer.Replace(text, context, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
return text;
}

View File

@@ -0,0 +1,16 @@
using System.Collections.Generic;
using Orchard.Layouts.Framework.Display;
namespace Orchard.Layouts.Helpers {
public static class ElementDisplayContextHelper {
public static IDictionary<string, object> GetTokenData(this ElementDisplayContext context) {
var data = new Dictionary<string, object>();
if (context.Content != null)
data["Content"] = context.Content.ContentItem;
return data;
}
}
}

View File

@@ -341,6 +341,7 @@
<Compile Include="Handlers\ElementDriversCoordinator.cs" />
<Compile Include="Helpers\DictionaryExtensions.cs" />
<Compile Include="Helpers\EditorResultExtensions.cs" />
<Compile Include="Helpers\ElementDisplayContextHelper.cs" />
<Compile Include="Helpers\ObjectStoreHelper.cs" />
<Compile Include="Helpers\PrefixHelper.cs" />
<Compile Include="Helpers\JsonHelper.cs" />
@@ -354,6 +355,9 @@
<Compile Include="Services\ElementEventContext.cs" />
<Compile Include="Services\ElementEventHandlerBase.cs" />
<Compile Include="Models\ElementSessionState.cs" />
<Compile Include="Services\ElementFilterProcessor.cs" />
<Compile Include="Services\IElementFilterProcessor.cs" />
<Compile Include="Services\IElementFilter.cs" />
<Compile Include="Services\ICurrentControllerAccessor.cs" />
<Compile Include="Services\IElementEventHandler.cs" />
<Compile Include="Services\ILayoutEditorFactory.cs" />
@@ -548,6 +552,9 @@
<ItemGroup>
<Content Include="Views\EditorTemplates\Elements.Heading.cshtml" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Elements\Heading.Design.cshtml" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public class ElementFilterProcessor : IElementFilterProcessor {
private readonly IEnumerable<IHtmlFilter> _filters;
public ElementFilterProcessor(IEnumerable<IHtmlFilter> filters) {
_filters = filters;
}
public string ProcessContent(string text, string flavor, IDictionary<string, object> context) {
foreach (var htmlFilter in _filters) {
var elementFilter = htmlFilter as IElementFilter;
text = elementFilter != null ? elementFilter.ProcessContent(text, flavor, context) : htmlFilter.ProcessContent(text, flavor);
}
return text;
}
}
}

View File

@@ -0,0 +1,8 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public interface IElementFilter : IHtmlFilter {
string ProcessContent(string text, string flavor, IDictionary<string, object> context);
}
}

View File

@@ -0,0 +1,8 @@
using System.Collections.Generic;
using Orchard.Services;
namespace Orchard.Layouts.Services {
public interface IElementFilterProcessor : IDependency {
string ProcessContent(string text, string flavor, IDictionary<string, object> context);
}
}

View File

@@ -0,0 +1,6 @@
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model, "h" + (Model.Level >= 1 && Model.Level <= 6 ? Model.Level : 1));
tagBuilder.InnerHtml = Model.Element.Content;
}
@tagBuilder.ToHtmlString()

View File

@@ -1,6 +1,6 @@
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model, "h" + (Model.Level >= 1 && Model.Level <= 6 ? Model.Level : 1));
tagBuilder.InnerHtml = Model.ProcessedText;
tagBuilder.InnerHtml = Model.ProcessedContent;
}
@tagBuilder.ToHtmlString()

View File

@@ -1,6 +1,6 @@
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model);
tagBuilder.InnerHtml = Model.ProcessedText;
tagBuilder.InnerHtml = Model.ProcessedContent;
}
@tagBuilder.ToHtmlString()

View File

@@ -1 +1,6 @@
<p>@Html.Raw((string)Model.Element.Content)</p>
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model, "p");
tagBuilder.InnerHtml = Model.ProcessedContent;
}
@tagBuilder.ToHtmlString()

View File

@@ -1,6 +1,6 @@
@using Orchard.Layouts.Helpers
@{
var tagBuilder = TagBuilderExtensions.CreateElementTagBuilder(Model);
tagBuilder.InnerHtml = Model.ProcessedText;
tagBuilder.InnerHtml = Model.ProcessedContent;
}
@tagBuilder.ToHtmlString()