コード例 #1
0
        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());
        }
コード例 #2
0
 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();
 }
コード例 #3
0
            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);
            }
コード例 #4
0
            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);
            }
コード例 #5
0
 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('`');
     }
 }
コード例 #6
0
        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);
        }
コード例 #7
0
            public void ClosingTagAfterDigit_IsNotATag()
            {
                var text     = @"abc__5";
                var expected = new TextTree();

                expected.AddChild("abc__5");
                CompareMarkDownTextAndTree(expected, text);
            }
コード例 #8
0
            public void SimpleTextWithEscapeSymbols_ParseEscapeSymbolCorrectly()
            {
                var text     = @"a\\b\c";
                var expected = new TextTree();

                expected.AddChild(@"a\b\c");
                CompareMarkDownTextAndTree(expected, text);
            }
コード例 #9
0
            public void SimpleText_GetSingleTextNode()
            {
                var text     = @"abc";
                var expected = new TextTree();

                expected.AddChild("abc");
                CompareMarkDownTextAndTree(expected, text);
            }
コード例 #10
0
            public void OpeningTagAfterDigit_IsNotATag()
            {
                var text     = @"4_abc";
                var expected = new TextTree();

                expected.AddChild("4_abc");
                CompareMarkDownTextAndTree(expected, text);
            }
コード例 #11
0
 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);
 }
コード例 #12
0
            public void TextWithAngleBrackets_ParsedCorrectly()
            {
                var text     = @"\<code\>";
                var expected = new TextTree();

                expected.AddChild("&lt;code&gt;");
                CompareMarkDownTextAndTree(expected, text);
            }
コード例 #13
0
 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);
 }
コード例 #14
0
            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);
            }
コード例 #15
0
            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);
            }
コード例 #16
0
            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);
            }
コード例 #17
0
            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);
            }
コード例 #18
0
            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);
            }
コード例 #19
0
            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);
            }
コード例 #20
0
 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);
 }
コード例 #21
0
            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);
            }
コード例 #22
0
 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);
 }
コード例 #23
0
 public void OpeningTagAfterDigit_IsNotATag()
 {
     var text = @"4_abc";
     var expected = new TextTree();
     expected.AddChild("4_abc");
     CompareMarkDownTextAndTree(expected, text);
 }
コード例 #24
0
 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);
 }
コード例 #25
0
            void CompareMarkDownTextAndTree(TextTree expected, string text)
            {
                var actual = new TreeBuilder().GetTree(text);

                CollectionAssert.AreEqual(expected.Children, actual.Children);
            }
コード例 #26
0
 public bool Equals(TextTree anotherTextTree)
 {
     return(Children.SequenceEqual(anotherTextTree.Children) && Tag == anotherTextTree.Tag &&
            IsComplete == anotherTextTree.IsComplete);
 }
コード例 #27
0
 public void SimpleTextWithEscapeSymbols_ParseEscapeSymbolCorrectly()
 {
     var text = @"a\\b\c";
     var expected = new TextTree();
     expected.AddChild(@"a\b\c");
     CompareMarkDownTextAndTree(expected, text);
 }
コード例 #28
0
 public void SimpleText_GetSingleTextNode()
 {
     var text = @"abc";
     var expected = new TextTree();
     expected.AddChild("abc");
     CompareMarkDownTextAndTree(expected, text);
 }
コード例 #29
0
 public bool Equals(TextTree anotherTextTree)
 {
     return Children.SequenceEqual(anotherTextTree.Children) && Tag == anotherTextTree.Tag
         && IsComplete == anotherTextTree.IsComplete;
 }
コード例 #30
0
 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);
 }
コード例 #31
0
 public void TextWithAngleBrackets_ParsedCorrectly()
 {
     var text = @"\<code\>";
     var expected = new TextTree();
     expected.AddChild("&lt;code&gt;");
     CompareMarkDownTextAndTree(expected, text);
 }
コード例 #32
0
 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);
 }
コード例 #33
0
 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);
 }
コード例 #34
0
 void CompareMarkDownTextAndTree(TextTree expected, string text)
 {
     var actual = new TreeBuilder().GetTree(text);
     CollectionAssert.AreEqual(expected.Children, actual.Children);
 }
コード例 #35
0
 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);
 }
コード例 #36
0
 public void ClosingTagAfterDigit_IsNotATag()
 {
     var text = @"abc__5";
     var expected = new TextTree();
     expected.AddChild("abc__5");
     CompareMarkDownTextAndTree(expected, text);
 }