private static ParseResult ParseDoubleLineHeading( ParseInput input ) { ArraySegment <string> lines = input.Lines(); ParseResult result = new ParseResult(); int level; if (lines[1].StartsWith("=")) { level = 1; } else { level = 2; } MarkdownHeading element = new MarkdownHeading( level, MarkdownParser.ParseInnerText( new ParseInput( input, lines[0] ) ) ); lines[0] = ""; lines[1] = ""; result.Success = true; result.AddContent( element ); return(result); }
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); }
// 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); }
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); }
// Shared code for parsing strong sections private static ParseResult ParseStrongSection( ParseInput input, string delimiter ) { string line = input.FirstLine; ParseResult result = new ParseResult(); int j = 2; // Find closing two characters while ( (j < line.Length) && !( (line.Substring(j - 1, 2) == delimiter) && (line[j - 2] != '\\') ) ) { j++; } if (j >= line.Length) { // Fail if closing characters cannot be found result.Line = line; return(result); } // Parse everything inside the strong section delimiters MarkdownStrong element = new MarkdownStrong( MarkdownParser.ParseInnerText( new ParseInput( input, line.Substring(2, j - 3) ) ) ); result.AddContent(element); result.Line = line.Substring(j + 1); result.Success = true; // Return the line string minus the content we parsed return(result); }
public static ParseResult ParseFrom( ParseInput lines, bool innerParagraph ) { ParseResult result = new ParseResult(); ParseResult innerResult; IHtmlable returnedElement; // If the list items content contains another list if (MarkdownList.CanParseFrom(lines)) { innerResult = MarkdownList.ParseFrom(lines); returnedElement = new MarkdownListItem( innerResult.GetContent() ); } else { // Otherwise, if the item content should go in a paragraph if (innerParagraph) { innerResult = MarkdownParagraph.ParseFrom(lines); returnedElement = new MarkdownListItem( innerResult.GetContent() ); } else { // line item content should not go in a paragraph returnedElement = new MarkdownListItem( MarkdownParser.ParseInnerText(lines) ); } } result.Success = true; result.AddContent( returnedElement ); return(result); }
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 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); }
// 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] != ' ')