mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding #{} tokens support
--HG-- branch : 1.x
This commit is contained in:
@@ -41,21 +41,38 @@ namespace Orchard.Tokens.Implementation {
|
||||
}
|
||||
|
||||
public string Replace(string text, IDictionary<string, object> data, ReplaceOptions options) {
|
||||
var tokenset = Parse(text);
|
||||
|
||||
// do we have to replace tokens with hashes ?
|
||||
bool hashMode = text.Contains("#{");
|
||||
|
||||
var tokenset = Parse(text, hashMode);
|
||||
var tokens = tokenset.Item2;
|
||||
var replacements = Evaluate(options.Predicate == null ? tokens : tokens.Where(options.Predicate), data);
|
||||
|
||||
return replacements.Aggregate(tokenset.Item1,
|
||||
(current, replacement) => current.Replace("{" + replacement.Key + "}", (options.Encoding ?? ReplaceOptions.NoEncode)(replacement.Key, replacement.Value)));
|
||||
(current, replacement) => current.Replace((hashMode ? "#{" : "{") + replacement.Key + "}", (options.Encoding ?? ReplaceOptions.NoEncode)(replacement.Key, replacement.Value)));
|
||||
}
|
||||
|
||||
private static Tuple<string, IEnumerable<string>> Parse(string text) {
|
||||
private static Tuple<string, IEnumerable<string>> Parse(string text, bool hashMode) {
|
||||
var tokens = new List<string>();
|
||||
if (!string.IsNullOrEmpty(text)) {
|
||||
var inToken = false;
|
||||
var tokenStart = 0;
|
||||
for (var i = 0; i < text.Length; i++) {
|
||||
var c = text[i];
|
||||
|
||||
if (!inToken && hashMode) {
|
||||
if (c == '#' && i + 1 < text.Length) {
|
||||
c = text[i + 1];
|
||||
if (c == '{') {
|
||||
i++;
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (c == '{') {
|
||||
if (i + 1 < text.Length && text[i + 1] == '{') {
|
||||
text = text.Substring(0, i) + text.Substring(i + 1);
|
||||
|
@@ -76,5 +76,29 @@ namespace Orchard.Tokens.Tests {
|
||||
public void TestPredicate() {
|
||||
Assert.That(_tokenizer.Replace("{Site.Global1}{Site.Global2}", null, new ReplaceOptions { Predicate = token => token == "Site.Global2" }), Is.EqualTo("{Site.Global1}[global2]"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HashTokenShouldBeReplaced() {
|
||||
Assert.That(_tokenizer.Replace("foo #{Site.Global1}", null), Is.EqualTo("foo [global1]"));
|
||||
Assert.That(_tokenizer.Replace("#{Site.Global1}#{Site.Global2}", null), Is.EqualTo("[global1][global2]"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HashInsideTokenShouldBeIgnored() {
|
||||
Assert.That(_tokenizer.Replace("{Site.Global1.#}", null), Is.EqualTo(""));
|
||||
Assert.That(_tokenizer.Replace("#{Site.Global1.#}", null), Is.EqualTo(""));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SimplePatterShouldBeIgnoredWhenHashIsPresent() {
|
||||
Assert.That(_tokenizer.Replace("#{Site.Global1}", null), Is.EqualTo("[global1]"));
|
||||
Assert.That(_tokenizer.Replace("{Site.Global1}#{Site.Global1}", null), Is.EqualTo("{Site.Global1}[global1]"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HashPatternCanBeEscaped() {
|
||||
Assert.That(_tokenizer.Replace("##{Site.Global1}", null), Is.EqualTo("#[global1]"));
|
||||
Assert.That(_tokenizer.Replace("#{{Site.Global1}}", null), Is.EqualTo("#{Site.Global1}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user