public void ShouldNotParseIncorrectlyFormattedHorizontalRuleFail(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            Assert.AreEqual(
                targetHtml,
                html
                );
        }
        public void ShouldParseTwoSeparatedLinesAsTwoParagraphsSuccess()
        {
            string[] testData = new string[]
            {
                "test1",
                " ",
                "test2"
            };
            string         targetHtml = "<p>test1</p><p>test2</p>";
            MarkdownParser parser     = new MarkdownParser(
                testData
                );

            Assert.IsTrue(
                parser.Success
                );
            Assert.AreEqual(
                targetHtml,
                parser.ToHtml()
                );
        }
Beispiel #3
0
        public void ShouldParseTooManyLeadingHashesSingleLineHeadingSuccess(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            Assert.AreEqual(
                targetHtml,
                html
                );
        }
Beispiel #4
0
        public void ShouldParseCorrectlyFormattedWithSpaceSingleLineHeadingSuccess(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            Assert.AreEqual(
                targetHtml,
                html
                );
        }
Beispiel #5
0
        private static ParseResult ParseSingleLineHeading(
            ParseInput input
            )
        {
            ArraySegment <string> lines  = input.Lines();
            ParseResult           result = new ParseResult();
            // Calculate heading level, (maximum 6)
            int level = 0;

            while (
                (level < 6) &&
                (lines[0][level] == '#')
                )
            {
                level++;
            }
            Match  contentMatch = regexSingleLineHeading.Match(lines[0]);
            string content      = StripLeadingCharacter(
                StripTrailingCharacter(
                    contentMatch.Groups[1].Value,
                    '#'
                    ),
                ' '
                );

            lines[0]       = "";
            result.Success = true;
            result.AddContent(
                new MarkdownHeading(
                    level,
                    MarkdownParser.ParseInnerText(
                        new ParseInput(
                            input,
                            content
                            )
                        )
                    )
                );
            return(result);
        }
        public void ShouldNotParseIncorrectlyDelimitedEmphasisFail(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            // Check that the correct HTML is produced
            Assert.AreEqual(
                targetHtml,
                html
                );
        }
        public void ShouldParseCorrectlyEscapedEmphasisCharactersSuccess(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            // Check that the correct HTML is produced
            Assert.AreEqual(
                targetHtml,
                html
                );
        }
        public void ShouldParseCorrectlyFormattedUnderscoreStrongSuccess(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            // Check that the correct HTML is produced
            Assert.AreEqual(
                targetHtml,
                html
                );
        }
Beispiel #9
0
        public void ShouldParseIncorrectlyDelimitedStrikethroughAsParagraphSuccess(
            string markdown,
            string targetHtml
            )
        {
            MarkdownParser parser = new MarkdownParser(
                new string[] {
                markdown
            }
                );

            Assert.IsTrue(
                parser.Success
                );
            string html = parser.ToHtml();

            // Check that the correct HTML is produced
            Assert.AreEqual(
                targetHtml,
                html
                );
        }
        public static ParseResult ParseFrom(
            ParseInput input
            )
        {
            ArraySegment <string> lines  = input.Lines();
            ParseResult           result = new ParseResult();

            if (!CanParseFrom(input))
            {
                return(result);
            }
            int endQuoteSection = FindEndOfQuoteSection(
                lines
                );

            string[] truncatedLines = new string[endQuoteSection];
            // Remove quote arrows and spaces, if needed
            for (int i = 0; i < endQuoteSection; i++)
            {
                string truncated = lines[i];
                if (lines[i].StartsWith(">"))
                {
                    truncated = truncated.Substring(1);
                    int spaces = 0;
                    // Count spaces
                    while (
                        (spaces < truncated.Length) &&
                        (truncated[spaces] == ' ')
                        )
                    {
                        spaces++;
                    }
                    // If there are fewer than 5 spaces, remove all
                    if (spaces < 5)
                    {
                        truncatedLines[i] = truncated.Substring(spaces);
                    }
                    else
                    {
                        // More than five, just remove one space
                        truncatedLines[i] = truncated.Substring(1);
                    }
                }
                else
                {
                    truncatedLines[i] = lines[i];
                }
                // Remove original line
                lines[i] = "";
            }

            /*
             * The truncated lines should be parsed as any other line group
             * and wrapped in a blockquote element
             */
            MarkdownParser parser = new MarkdownParser(
                truncatedLines
                );

            MarkdownQuote quoteElement = new MarkdownQuote(
                parser.ContentAsArray()
                );

            result.Success = true;
            result.AddContent(
                quoteElement
                );
            return(result);
        }
Beispiel #11
0
        public static ParseResult ParseFrom(
            ParseInput input
            )
        {
            string line = input.FirstLine;

            ReferencedUrl[] urls   = input.Urls;
            ParseResult     result = new ParseResult();

            if (
                !CanParseFrom(input)
                )
            {
                return(result);
            }
            Match linkMatch;

            // Format: [text](url)
            linkMatch = regexLinkImmediate.Match(line);
            if (linkMatch.Success)
            {
                string text = linkMatch.Groups[1].Value;
                string url  = linkMatch.Groups[2].Value;
                result.Success = true;
                result.Line    = regexLinkImmediate.Replace(
                    line,
                    ""
                    );
                result.AddContent(
                    new MarkdownLink(
                        MarkdownParser.ParseInnerText(
                            new ParseInput(
                                input,
                                text
                                )
                            ),
                        url
                        )
                    );
            }
            // Format: [text][id]    [id]: url
            linkMatch = regexLinkReference.Match(line);
            if (linkMatch.Success)
            {
                string text      = linkMatch.Groups[1].Value;
                string reference = linkMatch.Groups[2].Value;
                foreach (ReferencedUrl url in urls)
                {
                    if (url.Reference == reference)
                    {
                        result.Success = true;
                        result.Line    = regexLinkReference.Replace(
                            line,
                            ""
                            );
                        result.AddContent(
                            new MarkdownLink(
                                MarkdownParser.ParseInnerText(
                                    new ParseInput(
                                        input,
                                        text
                                        )
                                    ),
                                url.Url
                                )
                            );
                    }
                }
            }
            // Format: [text]   [text]: url
            linkMatch = regexLinkSelfReference.Match(line);
            if (linkMatch.Success)
            {
                string text = linkMatch.Groups[1].Value;
                foreach (ReferencedUrl url in urls)
                {
                    if (url.Reference == text)
                    {
                        result.Success = true;
                        result.Line    = regexLinkSelfReference.Replace(
                            line,
                            ""
                            );
                        result.AddContent(
                            new MarkdownLink(
                                MarkdownParser.ParseInnerText(
                                    new ParseInput(
                                        input,
                                        text
                                        )
                                    ),
                                url.Url
                                )
                            );
                    }
                }
            }
            return(result);
        }
Beispiel #12
0
        // Parse a plain paragraph
        public static ParseResult ParseFrom(
            ParseInput input
            )
        {
            ArraySegment <string>  lines        = input.Lines();
            ParseResult            result       = new ParseResult();
            LinkedList <IHtmlable> innerContent = new LinkedList <IHtmlable>();
            // The paragraph doesn't get parsed past the first blank line
            int endIndex = 0;

            while (
                (endIndex < lines.Count) &&
                (
                    !ContainsOnlyWhitespace(
                        lines[endIndex]
                        )
                )
                )
            {
                endIndex++;
            }
            int i = 0;

            while (i < endIndex)
            {
                lines = input.Lines();
                if (MarkdownCodeBlock.CanParseFrom(input))
                {
                    ParseResult innerResult = MarkdownCodeBlock.ParseFrom(input);
                    foreach (IHtmlable entry in innerResult.GetContent())
                    {
                        innerContent.AddLast(entry);
                    }
                }
                else
                {
                    string line = lines[0];
                    if (endsWithAtLeastTwoSpaces(line))
                    {
                        string shortened = StripTrailingWhitespace(line);
                        foreach (
                            IHtmlable entry
                            in MarkdownParser.ParseInnerText(
                                new ParseInput(
                                    input,
                                    shortened
                                    )
                                )
                            )
                        {
                            innerContent.AddLast(entry);
                        }
                        innerContent.AddLast(
                            new MarkdownLinebreak()
                            );
                    }
                    else
                    {
                        foreach (
                            IHtmlable entry
                            in MarkdownParser.ParseInnerText(
                                new ParseInput(
                                    input,
                                    line
                                    )
                                )
                            )
                        {
                            innerContent.AddLast(entry);
                        }

                        /*
                         * If this is not the last line,
                         * it doesn't end in a manual linebreak
                         * and the user hasn't added a space themselves
                         * we need to add a space at the end
                         */
                        if (
                            (i < (endIndex - 1)) &&
                            (line.Length > 0) &&
                            (line[^ 1] != ' ')