mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-22 21:02:08 +08:00
Removing old cmspages
- Create new branch dev for iteration activity - CmsPages was a leftover from the previous iteration where we rewrote it as the Pages package. --HG-- branch : dev
This commit is contained in:
@@ -1,62 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using NUnit.Framework;
|
|
||||||
using Orchard.CmsPages.Services.Templates;
|
|
||||||
|
|
||||||
namespace Orchard.CmsPages.Tests.Services.Templates {
|
|
||||||
[TestFixture]
|
|
||||||
public class MetadataParserTests {
|
|
||||||
[Test]
|
|
||||||
public void ParserShouldReturnEmptyListForEmptyMetadata() {
|
|
||||||
var reader = new StringReader(" \r\n ");
|
|
||||||
var parser = new MetadataParser();
|
|
||||||
IList<MetadataEntry> result = parser.Parse(reader);
|
|
||||||
|
|
||||||
Assert.That(result.Count, Is.EqualTo(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ParserShouldIgnoreEmptyTags() {
|
|
||||||
var reader = new StringReader(" : test value \r\n ");
|
|
||||||
var parser = new MetadataParser();
|
|
||||||
IList<MetadataEntry> result = parser.Parse(reader);
|
|
||||||
|
|
||||||
Assert.That(result.Count, Is.EqualTo(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ParserShouldReturnMetadata() {
|
|
||||||
var reader = new StringReader("Description: test");
|
|
||||||
var parser = new MetadataParser();
|
|
||||||
IList<MetadataEntry> result = parser.Parse(reader);
|
|
||||||
|
|
||||||
Assert.That(result.Count, Is.EqualTo(1));
|
|
||||||
Assert.That(result[0].Tag, Is.EqualTo("Description"));
|
|
||||||
Assert.That(result[0].Value, Is.EqualTo("test"));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ParserShouldReturnMultiMetadata() {
|
|
||||||
var reader = new StringReader("Description: test\r\n Tag2 : this is my test");
|
|
||||||
var parser = new MetadataParser();
|
|
||||||
IList<MetadataEntry> result = parser.Parse(reader);
|
|
||||||
|
|
||||||
Assert.That(result.Count, Is.EqualTo(2));
|
|
||||||
Assert.That(result[0].Tag, Is.EqualTo("Description"));
|
|
||||||
Assert.That(result[0].Value, Is.EqualTo("test"));
|
|
||||||
Assert.That(result[1].Tag, Is.EqualTo("Tag2"));
|
|
||||||
Assert.That(result[1].Value, Is.EqualTo("this is my test"));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void ParserShouldIgnoreTagsNotPreceededByNewLine() {
|
|
||||||
var reader = new StringReader("Description: test Tag2 : this is \r\n my test \r\n ");
|
|
||||||
var parser = new MetadataParser();
|
|
||||||
IList<MetadataEntry> result = parser.Parse(reader);
|
|
||||||
|
|
||||||
Assert.That(result.Count, Is.EqualTo(1));
|
|
||||||
Assert.That(result[0].Tag, Is.EqualTo("Description"));
|
|
||||||
Assert.That(result[0].Value, Is.EqualTo("test Tag2 : this is \r\n my test"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace Orchard.CmsPages.Services.Templates {
|
|
||||||
/// <summary>
|
|
||||||
/// Parse the content of a text reader into a list of metadata entries.
|
|
||||||
/// </summary>
|
|
||||||
public class MetadataParser {
|
|
||||||
private readonly Regex _tagRegex;
|
|
||||||
|
|
||||||
public MetadataParser() {
|
|
||||||
_tagRegex = new Regex(@"^\s*(?<tag>\w+)\s*:\s*(?<value>)", RegexOptions.Multiline);
|
|
||||||
}
|
|
||||||
|
|
||||||
public IList<MetadataEntry> Parse(TextReader reader) {
|
|
||||||
string content = reader.ReadToEnd();
|
|
||||||
var result = new List<MetadataEntry>();
|
|
||||||
|
|
||||||
// Find matches
|
|
||||||
MatchCollection matches = _tagRegex.Matches(content);
|
|
||||||
|
|
||||||
// Process each match
|
|
||||||
for (int i = 0; i < matches.Count; i++) {
|
|
||||||
Match currentMatch = matches[i];
|
|
||||||
Match nextMatch = (i < matches.Count - 1 ? matches[i + 1] : null);
|
|
||||||
|
|
||||||
//int tagIndex = currentMatch.Groups["tag"].Index;
|
|
||||||
string tag = currentMatch.Groups["tag"].Value;
|
|
||||||
|
|
||||||
int valueIndex = currentMatch.Groups["value"].Index;
|
|
||||||
string value =
|
|
||||||
nextMatch == null ?
|
|
||||||
content.Substring(valueIndex) :
|
|
||||||
content.Substring(valueIndex, nextMatch.Groups["tag"].Index - valueIndex);
|
|
||||||
|
|
||||||
// Remove optional trailing space and line separators at end of value
|
|
||||||
int count = 0;
|
|
||||||
foreach (char ch in value.Reverse()) {
|
|
||||||
if (char.IsSeparator(ch) || char.IsWhiteSpace(ch) || ch == '\r' || ch == '\n') {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
value = value.Substring(0, value.Length - count);
|
|
||||||
|
|
||||||
// Add result entry
|
|
||||||
result.Add(new MetadataEntry { Tag = tag, Value = value });
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user