// Shared code for parsing emphasis sections public static ParseResult ParseFrom( ParseInput input ) { string line = input.FirstLine; ParseResult result = new ParseResult(); if (!CanParseFrom(input)) { // Fail immediately if this string cannot be parsed result.Line = line; return(result); } int j = 1; // Find closing ` while ( (j < line.Length) && !( (line[j] == '`') && (line[j - 1] != '\\') ) ) { j++; } if (j >= line.Length) { // If we cannot parse, then return line as is result.Line = line; return(result); } // Parse everything inside the backticks MarkdownCodeInline element = new MarkdownCodeInline( MarkdownParser.ParseInnerText( new ParseInput( input, line.Substring(1, j - 1) ) ) ); 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); }