Allow Text token chaining when possible.

Correct some errors on token descriptions.
This commit is contained in:
Thierry Fleury
2015-07-24 17:42:07 +02:00
parent 25c34d8bca
commit 67e7d1074e
2 changed files with 21 additions and 17 deletions

View File

@@ -36,7 +36,7 @@ namespace Orchard.Tokens.Providers {
.Token("DisplayUrl", T("Display Url"), T("Url to display the content."), "Url")
.Token("EditUrl", T("Edit Url"), T("Url to edit the content."), "Url")
.Token("Container", T("Container"), T("The container Content Item."), "Content")
.Token("Body", T("Body"), T("The body text of the content item."), "Content")
.Token("Body", T("Body"), T("The body text of the content item."), "Text")
;
// Token descriptors for fields

View File

@@ -13,38 +13,42 @@ namespace Orchard.Tokens.Providers {
public void Describe(DescribeContext context) {
context.For("Text", T("Text"), T("Tokens for text strings"))
.Token("Limit:*", T("Limit:<text length>[,<ellipsis>]"), T("Limit text to specified length and append an optional ellipsis text."))
.Token("Format:*", T("Format:<text format>"), T("Optional format specifier (e.g. foo{0}bar). See format strings at <a target=\"_blank\" href=\"http://msdn.microsoft.com/en-us/library/az4se3k1.aspx\">Standard Formats</a> and <a target=\"_blank\" href=\"http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx\">Custom Formats</a>"), "DateTime")
.Token("TrimEnd:*", T("TrimEnd:<chars|number>"), T("Trims the specified characters or number of them from the end of the string."), "Text")
.Token("Format:*", T("Format:<text format>"), T("Optional format specifier (e.g. foo{0}bar)."))
.Token("TrimEnd:*", T("TrimEnd:<chars|number>"), T("Trims the specified characters or number of them from the end of the string."))
.Token("UrlEncode", T("Url Encode"), T("Encodes a URL string."), "Text")
.Token("HtmlEncode", T("Html Encode"), T("Encodes an HTML string."), "Text")
.Token("LineEncode", T("Line Encode"), T("Replaces new lines with <br /> tags."))
.Token("LineEncode", T("Line Encode"), T("Replaces new lines with <br /> tags."), "Text")
;
}
public void Evaluate(EvaluateContext context) {
context.For<String>("Text", () => "")
.Token( // {Text}
// {Text}
.Token(
token => token == String.Empty ? String.Empty : null,
(token, d) => d.ToString())
.Token( // {Text.Limit:<length>[,<ellipsis>]}
token => {
if (token.StartsWith("Limit:", StringComparison.OrdinalIgnoreCase)) {
var param = token.Substring("Limit:".Length);
return param;
}
return null;
},
// {Text.Limit:<length>[,<ellipsis>]}
.Token(
token => FilterTokenParam("Limit:", token),
(token, t) => Limit(t, token))
// {Text.Format:<formatstring>}
.Token(
token => token.StartsWith("Format:", StringComparison.OrdinalIgnoreCase) ? token.Substring("Format:".Length) : null,
(token, d) => String.Format(d,token))
.Token(token => token.StartsWith("TrimEnd:", StringComparison.OrdinalIgnoreCase) ? token.Substring("TrimEnd:".Length) : null, TrimEnd)
token => FilterTokenParam("Format:", token),
(token, d) => String.Format(d, token))
// {Text.TrimEnd:<chars|number>}
.Token(token => FilterTokenParam("TrimEnd:", token), TrimEnd)
.Token("UrlEncode", HttpUtility.UrlEncode)
.Chain("UrlEncode", "Text", HttpUtility.UrlEncode)
.Token("HtmlEncode", HttpUtility.HtmlEncode)
.Chain("HtmlEncode", "Text", HttpUtility.HtmlEncode)
.Token("LineEncode", text => text.Replace(System.Environment.NewLine, "<br />"))
.Chain("LineEncode", "Text", text => text.Replace(System.Environment.NewLine, "<br />"))
;
}
private static string FilterTokenParam(string tokenName, string token) {
return token.StartsWith(tokenName, StringComparison.OrdinalIgnoreCase) ? token.Substring(tokenName.Length) : null;
}
private static string TrimEnd(string token, string param) {