public static ParseResult ParseFrom( ParseInput input ) { string line = input.FirstLine; ParseResult result = new ParseResult(); if (!CanParseFrom(input)) { // Return a failed result if cannot parse from this line result.Line = line; return(result); } // Otherwise parse and return result Match contentMatch = regexParseable.Match(line); string content; if (contentMatch.Groups[1].Value.Length != 0) { content = contentMatch.Groups[1].Value; } else { content = contentMatch.Groups[2].Value; } // Parse everything inside the stars MarkdownEmphasis element = new MarkdownEmphasis( MarkdownParser.ParseInnerText( new ParseInput( input, content ) ) ); result.AddContent(element); result.Line = line.Substring( content.Length + 2 ); 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); }