Example #1
0
        public static ParseResult ParseFrom(
            ParseInput input
            )
        {
            string      line   = input.FirstLine;
            ParseResult result = new ParseResult();

            if (!CanParseFrom(input))
            {
                // Fail immediately if we cannot parse this text as strikethrough
                result.Line = line;
                return(result);
            }
            int j = 2;

            // Find closing tildes
            while (
                (j < line.Length) &&
                !(
                    (line.Substring(j - 1, 2) == "~~") &&
                    (line[j - 2] != '\\')
                    )
                )
            {
                j++;
            }
            if (j >= line.Length)
            {
                // Fail if we cannot find the closing squiggles
                result.Line = line;
                return(result);
            }
            // Parse everything inside the stars
            MarkdownStrikethrough element = new MarkdownStrikethrough(
                MarkdownParser.ParseInnerText(
                    new ParseInput(
                        input,
                        line.Substring(2, j - 3)
                        )
                    )
                );

            result.AddContent(element);
            result.Line    = line.Substring(j + 1);
            result.Success = true;
            return(result);
        }
        // Given a single line of text, parse this, including special (emph, etc...) sections
        public static IHtmlable[] ParseInnerText(
            ParseInput input
            )
        {
            // Store parsed content as we go
            LinkedList <IHtmlable> content = new LinkedList <IHtmlable>();

            // Until the whole string has been consumed
            while (input.FirstLine.Length > 0)
            {
                ParseResult result;
                if (MarkdownStrong.CanParseFrom(input))
                {
                    result = MarkdownStrong.ParseFrom(input);
                }
                else if (MarkdownStrikethrough.CanParseFrom(input))
                {
                    result = MarkdownStrikethrough.ParseFrom(input);
                }
                else if (MarkdownEmphasis.CanParseFrom(input))
                {
                    result = MarkdownEmphasis.ParseFrom(input);
                }
                else if (MarkdownCodeInline.CanParseFrom(input))
                {
                    result = MarkdownCodeInline.ParseFrom(input);
                }
                else if (MarkdownLink.CanParseFrom(input))
                {
                    result = MarkdownLink.ParseFrom(input);
                }
                else if (MarkdownImage.CanParseFrom(input))
                {
                    result = MarkdownImage.ParseFrom(input);
                }
                else
                {
                    result = MarkdownText.ParseFrom(
                        input,
                        false
                        );
                }

                /*
                 * If no parsing method suceeded
                 * for once character to be parsed as text
                 */
                if (!result.Success)
                {
                    result = MarkdownText.ParseFrom(
                        input,
                        true
                        );
                }
                // Extract parsed content
                foreach (IHtmlable entry in result.GetContent())
                {
                    content.AddLast(entry);
                }
                // Update text to be parsed
                input.FirstLine = result.Line;
            }
            IHtmlable[] contentArray = new IHtmlable[content.Count];
            content.CopyTo(
                contentArray,
                0
                );
            return(contentArray);
        }