forked from DovetailSoftware/dovetail-bootstrap
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added start of parser to be used by Htmlization of history items
- Loading branch information
Showing
7 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Linq; | ||
using Dovetail.SDK.Bootstrap.History; | ||
using NUnit.Framework; | ||
using Sprache; | ||
|
||
namespace Dovetail.SDK.Bootstrap.Tests | ||
{ | ||
[TestFixture] | ||
public class history_parser_document | ||
{ | ||
[Test] | ||
public void finds_all_items() | ||
{ | ||
const string input = "item1\r\nitem2\nitem3"; | ||
|
||
var document = HistoryParser.Document.Parse(input); | ||
|
||
var items = document.Items.ToArray(); | ||
items[0].ToString().ShouldEqual("item1"); | ||
items[1].ToString().ShouldEqual("item2"); | ||
items[2].ToString().ShouldEqual("item3"); | ||
} | ||
|
||
[Test] | ||
public void finds_email_headers_mixed_with_items() | ||
{ | ||
const string input = "item1\r\nto: kmiller@dovetailsoftware.com\nitem3"; | ||
|
||
var document = HistoryParser.Document.Parse(input); | ||
|
||
var items = document.Items.ToArray(); | ||
items[0].ToString().ShouldEqual("item1"); | ||
|
||
var emailHeaderItem = ((EmailHeader) items[1]).Headers.First(); | ||
emailHeaderItem.ShouldEqual("to"); | ||
emailHeaderItem.Text.ShouldEqual("kmiller@dovetailsoftware.com"); | ||
|
||
items[2].ToString().ShouldEqual("item3"); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class history_parser_items | ||
{ | ||
[Test] | ||
public void detect_item() | ||
{ | ||
const string input = "line1\r\nline2"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
item.ToString().ShouldEqual("line1"); | ||
} | ||
|
||
[Test] | ||
public void item_white_space_is_removed() | ||
{ | ||
const string input = " line1\r\n line2"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
item.ToString().ShouldEqual("line1"); | ||
} | ||
|
||
[Test] | ||
public void email_header_items_are_detected() | ||
{ | ||
const string input = "from: yadda\nto:email@example.com\nsubject:math"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
var emailHeader = (item as EmailHeader); | ||
emailHeader.ShouldNotBeNull(); | ||
emailHeader.Headers.Count().ShouldEqual(3); | ||
} | ||
|
||
[Test] | ||
public void email_header_properties_are_populated() | ||
{ | ||
const string input = "from: yadda\r\nan item"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
var emailHeaderItem = ((EmailHeader)item).Headers.First(); | ||
emailHeaderItem.Title.ShouldEqual("from"); | ||
emailHeaderItem.Text.ShouldEqual("yadda"); | ||
} | ||
|
||
[Test] | ||
public void email_header_titles_ignores_case() | ||
{ | ||
const string input = "FROM: other content\r\nan item"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
var emailHeaderItem = ((EmailHeader)item).Headers.First(); | ||
emailHeaderItem.Title.ShouldEqual("FROM"); | ||
emailHeaderItem.Text.ShouldEqual("other content"); | ||
} | ||
|
||
[Test] | ||
public void item_similar_to_an_email_header_is_still_just_an_item() | ||
{ | ||
const string input = "notaheader: yadda\r\nan item"; | ||
|
||
var item = HistoryParser.Item.Parse(input); | ||
|
||
(item as EmailHeader).ShouldBeNull(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Sprache; | ||
|
||
namespace Dovetail.SDK.Bootstrap.History | ||
{ | ||
public class Document | ||
{ | ||
public IEnumerable<Item> Items { get; set; } | ||
} | ||
|
||
public class Item | ||
{ | ||
} | ||
|
||
public class Content : Item | ||
{ | ||
public string Text { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return Text; | ||
} | ||
} | ||
|
||
public class EmailHeader : Item | ||
{ | ||
public IEnumerable<EmailHeaderItem> Headers { get; set; } | ||
} | ||
|
||
public class EmailHeaderItem | ||
{ | ||
public string Title { get; set; } | ||
public string Text { get; set; } | ||
} | ||
|
||
public class HistoryParser | ||
{ | ||
public static readonly string[] EmailHeaderFields = new[] {"from", "to", "Send to", "cc", "subject"}; | ||
|
||
public static readonly Parser<char> UntilEndOfLine = Parse.CharExcept(c => c == '\r' || c == '\n', "start of line ending"); | ||
|
||
public static readonly Parser<EmailHeaderItem> EmailHeaderItem = | ||
from title in Parse.Letter.Many().Text().Token() | ||
where EmailHeaderFields.Any(h => h.Equals(title, StringComparison.InvariantCultureIgnoreCase)) | ||
from _1 in Parse.Char(':') | ||
from text in UntilEndOfLine.Many().Token().Text() | ||
from rest in Parse.WhiteSpace.Many() | ||
select new EmailHeaderItem {Title = title, Text = text}; | ||
|
||
public static readonly Parser<EmailHeader> EmailHeader = | ||
from items in EmailHeaderItem.Many() | ||
select new EmailHeader {Headers = items}; | ||
|
||
public static readonly Parser<Content> Content = | ||
from text in UntilEndOfLine.Many().Token().Text() | ||
from rest in Parse.WhiteSpace.Many() | ||
select new Content {Text = text}; | ||
|
||
public static readonly Parser<Item> Item = | ||
EmailHeader.Select(n => (Item) n) | ||
.Or(Content); | ||
|
||
public static readonly Parser<Document> Document = | ||
from leading in Parse.WhiteSpace.Many() | ||
from items in Item.Many().End() | ||
select new Document {Items = items}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters