예제 #1
0
        public static ParseResult ParseFrom(
            ParseInput input
            )
        {
            ArraySegment <string> lines  = input.Lines();
            ParseResult           result = new ParseResult();

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

            /*
             * Work out the list type from the first line
             * before we start to mangle the line contents
             */
            MarkdownElementType type;

            if (regexOrderedListLine.Match(input.FirstLine).Success)
            {
                type = MarkdownElementType.OrderedList;
            }
            else
            {
                type = MarkdownElementType.UnorderedList;
            }
            int endListSection = FindEndOfListSection(
                lines
                );
            // New array segment with only parsed content
            ArraySegment <string> listLines = new ArraySegment <string>(
                lines.Array,
                lines.Offset,
                endListSection
                );
            // Need to split into groups per list item
            int currentIndex = 0;
            // Hold the parsed list items as we go
            LinkedList <IHtmlable> listItems = new LinkedList <IHtmlable>();
            // Track whether list item contents should be in a paragraph
            bool whitespaceLineBefore = false;
            bool whitespaceLineAfter  = false;

            while (currentIndex < endListSection)
            {
                int endIndex = FindEndOfListItem(
                    listLines,
                    currentIndex
                    );

                /*
                 * There is a whitespace line between
                 * this list item and the following one
                 * and this list item isn't the final one
                 */
                whitespaceLineAfter = (
                    (endIndex < listLines.Count) &&
                    (
                        ContainsOnlyWhitespace(
                            listLines[endIndex - 1]
                            )
                    )
                    );
                // Create new parse input for this list item
                ParseInput listItemLines = new ParseInput(
                    input.Urls,
                    listLines.Array,
                    listLines.Offset + currentIndex,
                    endIndex - currentIndex
                    );
                RemoveListIndicators(
                    listItemLines
                    );
                ParseResult nextListItem = MarkdownListItem.ParseFrom(
                    listItemLines,
                    whitespaceLineBefore || whitespaceLineAfter
                    );
                foreach (
                    IHtmlable entry
                    in nextListItem.GetContent()
                    )
                {
                    listItems.AddLast(
                        entry
                        );
                }
                // Jump over lines just parsed
                currentIndex += (endIndex - currentIndex);
                // Whitespace after previous entry becomes before next entry
                whitespaceLineBefore = whitespaceLineAfter;

                /*
                 * If there's further whitespace after parsed section
                 * (for some reason) then jump over this too
                 */
                while (
                    (currentIndex < listLines.Count) &&
                    (
                        ContainsOnlyWhitespace(
                            lines[currentIndex]
                            )
                    )
                    )
                {
                    currentIndex++;
                }
            }
            result.Success = true;
            result.AddContent(
                new MarkdownList(
                    Utils.LinkedListToArray(
                        listItems
                        ),
                    type
                    )
                );
            return(result);
        }
예제 #2
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] != ' ')