public string ConvertTreeToHtml(TextTree textTree) { var text = new StringBuilder(); if (textTree.IsComplete) { text.Append("<"); text.Append(DecodeTag[textTree.Tag]); text.Append(">"); } foreach (var child in textTree.Children) { if (child is string) { text.Append(child); } else { var taggedNode = child as TextTree; // ReSharper disable once PossibleNullReferenceException if (!taggedNode.IsComplete) { text.Append(taggedNode.Tag == Tag.Em ? "_" : "__"); } text.Append(ConvertTreeToHtml(taggedNode)); } } if (textTree.IsComplete) { text.Append("</"); text.Append(DecodeTag[textTree.Tag]); text.Append(">"); } return(text.ToString()); }
public string ConvertTreeToHtml(TextTree textTree) { var text = new StringBuilder(); if (textTree.IsComplete) { text.Append("<"); text.Append(DecodeTag[textTree.Tag]); text.Append(">"); } foreach (var child in textTree.Children) { if (child is string) text.Append(child); else { var taggedNode = child as TextTree; // ReSharper disable once PossibleNullReferenceException if (!taggedNode.IsComplete) { text.Append(taggedNode.Tag == Tag.Em ? "_" : "__"); } text.Append(ConvertTreeToHtml(taggedNode)); } } if (textTree.IsComplete) { text.Append("</"); text.Append(DecodeTag[textTree.Tag]); text.Append(">"); } return text.ToString(); }
public void TreeWithTagInTag_ParseCorrectly() { var text = @"__b _`a`_ c__"; var expected = new TextTree(); var codeTag = new TextTree() { IsComplete = true, Tag = Tag.Code }; var emTag = new TextTree() { IsComplete = true, Tag = Tag.Em }; var strongTag = new TextTree() { IsComplete = true, Tag = Tag.Strong }; codeTag.AddChild("a"); emTag.AddChild(""); emTag.AddChild(codeTag); emTag.AddChild(""); strongTag.AddChild("b "); strongTag.AddChild(emTag); strongTag.AddChild(" c"); expected.AddChild(""); expected.AddChild(strongTag); expected.AddChild(""); CompareMarkDownTextAndTree(expected, text); }
public void DeepTree_ParseCorrectly() { var tree = new TextTree(); var codeTag = new TextTree() { IsComplete = true, Tag = Tag.Code }; var emTag = new TextTree() { IsComplete = true, Tag = Tag.Em }; var strongTag = new TextTree() { IsComplete = true, Tag = Tag.Strong }; codeTag.AddChild("a"); emTag.AddChild(""); emTag.AddChild(codeTag); emTag.AddChild(""); strongTag.AddChild("b "); strongTag.AddChild(emTag); strongTag.AddChild(" c"); tree.AddChild(""); tree.AddChild(strongTag); tree.AddChild(""); var expected = "<strong>b <em><code>a</code></em> c</strong>"; var actual = MarkDownProcessor.ConvertTreeToHtml(tree); CollectionAssert.AreEqual(expected, actual); }
private void ParseBackQuote() { if (codeIndices.Contains(index)) { var node = new TextTree() { Tag = Tag.Code, IsComplete = true }; parsingStack.Push(textStorage.ToString()); textStorage.Clear(); index++; while (text[index] != '`') { textStorage.Append(text[index]); index++; } node.AddChild(textStorage.ToString()); textStorage.Clear(); parsingStack.Push(node); } else { textStorage.Append('`'); } }
private void ConstructNodeAndPushBackToStack(Tag tag) { var node = new TextTree { Tag = tag, IsComplete = true }; while (parsingStack.Count > 0) { object curNode = parsingStack.Pop(); if (curNode is string) { node.AddChild(curNode); } else { var castedNode = curNode as TextTree; // ReSharper disable once PossibleNullReferenceException if (castedNode.Tag == tag && !castedNode.IsComplete) { break; } if (castedNode.Tag == Tag.Em && !castedNode.IsComplete) { lowLineIndex = -1; } if (castedNode.Tag == Tag.Strong && !castedNode.IsComplete) { doubleLowLineIndex = -1; } node.AddChild(castedNode); } } node.ReverseChildren(); parsingStack.Push(node); }
public void ClosingTagAfterDigit_IsNotATag() { var text = @"abc__5"; var expected = new TextTree(); expected.AddChild("abc__5"); CompareMarkDownTextAndTree(expected, text); }
public void SimpleTextWithEscapeSymbols_ParseEscapeSymbolCorrectly() { var text = @"a\\b\c"; var expected = new TextTree(); expected.AddChild(@"a\b\c"); CompareMarkDownTextAndTree(expected, text); }
public void SimpleText_GetSingleTextNode() { var text = @"abc"; var expected = new TextTree(); expected.AddChild("abc"); CompareMarkDownTextAndTree(expected, text); }
public void OpeningTagAfterDigit_IsNotATag() { var text = @"4_abc"; var expected = new TextTree(); expected.AddChild("4_abc"); CompareMarkDownTextAndTree(expected, text); }
public void SimpleTextTree_GetSimpleText() { var tree = new TextTree() { IsComplete = true, Tag = Tag.Paragraph }; tree.AddChild("abc"); var expected = "<p>abc</p>"; var actual = MarkDownProcessor.ConvertTreeToHtml(tree); CollectionAssert.AreEqual(expected, actual); }
public void TextWithAngleBrackets_ParsedCorrectly() { var text = @"\<code\>"; var expected = new TextTree(); expected.AddChild("<code>"); CompareMarkDownTextAndTree(expected, text); }
public void TagSymbolWithoutPair_StayAsText() { var tree = new TextTree() { IsComplete = true, Tag = Tag.Paragraph }; tree.AddChild("abc"); tree.AddChild(new TextTree() { Tag = Tag.Em }); var expected = "<p>abc_</p>"; var actual = MarkDownProcessor.ConvertTreeToHtml(tree); CollectionAssert.AreEqual(expected, actual); }
public void OpeningTagAfterNotDigitAndNumber_IsATag() { var text = @"$_abc"; var expected = new TextTree(); expected.AddChild("$"); var emNode = new TextTree() { Tag = Tag.Em }; expected.AddChild(emNode); expected.AddChild("abc"); CompareMarkDownTextAndTree(expected, text); }
public void EscapeSymbolsBeforeTags_EscapeSymbolsEscapeTags() { var text = @"\_a b_ _\_c__ d"; var expected = new TextTree(); expected.AddChild(@"_a b_ "); var emNode = new TextTree() { Tag = Tag.Em }; expected.AddChild(emNode); expected.AddChild(@"_c__ d"); CompareMarkDownTextAndTree(expected, text); }
public void TextWithCodeTag_ParseTagCode() { var text = @"a`b`"; var expected = new TextTree(); expected.AddChild(@"a"); var codeTag = new TextTree() { IsComplete = true, Tag = Tag.Code }; codeTag.AddChild(@"b"); expected.AddChild(codeTag); expected.AddChild(""); CompareMarkDownTextAndTree(expected, text); }
public void TextWithEscapeInsideCodeTag_DontParseEscapeSymbol() { var text = @"a`b\`"; var expected = new TextTree(); expected.AddChild(@"a"); var codeTag = new TextTree() { IsComplete = true, Tag = Tag.Code }; codeTag.AddChild(@"b\"); expected.AddChild(codeTag); expected.AddChild(""); CompareMarkDownTextAndTree(expected, text); }
public void DifferentTagsIntersect_TakeFirstTag() { var text = @"a _a __b_ c__"; var expected = new TextTree(); expected.AddChild(@"a "); var emTag = new TextTree() { IsComplete = true, Tag = Tag.Em }; var incompleteStrongTag = new TextTree() { Tag = Tag.Strong }; emTag.AddChild(@"a "); emTag.AddChild(incompleteStrongTag); emTag.AddChild("b"); expected.AddChild(emTag); expected.AddChild(" c__"); CompareMarkDownTextAndTree(expected, text); }
void CompareMarkDownTextAndTree(TextTree expected, string text) { var actual = new TreeBuilder().GetTree(text); CollectionAssert.AreEqual(expected.Children, actual.Children); }
public bool Equals(TextTree anotherTextTree) { return(Children.SequenceEqual(anotherTextTree.Children) && Tag == anotherTextTree.Tag && IsComplete == anotherTextTree.IsComplete); }
public bool Equals(TextTree anotherTextTree) { return Children.SequenceEqual(anotherTextTree.Children) && Tag == anotherTextTree.Tag && IsComplete == anotherTextTree.IsComplete; }