/// <summary> /// Attempts to parse an inline code span. /// </summary> /// <param name="markdown"> The markdown text. </param> /// <param name="start"> The location to start parsing. </param> /// <param name="maxEnd"> The location to stop parsing. </param> /// <returns> A parsed inline code span, or <c>null</c> if this is not an inline code span. </returns> internal static Common.InlineParseResult Parse(string markdown, int start, int maxEnd) { // Check the first char. if (start == maxEnd || markdown[start] != '`') { return(null); } // There is an alternate syntax that starts and ends with two backticks. // e.g. ``sdf`sdf`` would be "sdf`sdf". int innerStart = start + 1; int innerEnd, end; if (innerStart < maxEnd && markdown[innerStart] == '`') { // Alternate double back-tick syntax. innerStart++; // Find the end of the span. innerEnd = Common.IndexOf(markdown, "``", innerStart, maxEnd); if (innerEnd == -1) { return(null); } end = innerEnd + 2; } else { // Standard single backtick syntax. // Find the end of the span. innerEnd = Common.IndexOf(markdown, '`', innerStart, maxEnd); if (innerEnd == -1) { return(null); } end = innerEnd + 1; } // The span must contain at least one character. if (innerStart == innerEnd) { return(null); } // We found something! var result = new CodeInline(); result.Text = markdown.Substring(innerStart, innerEnd - innerStart).Trim(' ', '\t', '\r', '\n'); return(new Common.InlineParseResult(result, start, end)); }
/// <summary> /// Attempts to parse an inline code span. /// </summary> /// <param name="markdown"> The markdown text. </param> /// <param name="start"> The location to start parsing. </param> /// <param name="maxEnd"> The location to stop parsing. </param> /// <returns> A parsed inline code span, or <c>null</c> if this is not an inline code span. </returns> internal static Common.InlineParseResult Parse(string markdown, int start, int maxEnd) { // Check the first char. if (start == maxEnd || markdown[start] != '`') return null; // There is an alternate syntax that starts and ends with two backticks. // e.g. ``sdf`sdf`` would be "sdf`sdf". int innerStart = start + 1; int innerEnd, end; if (innerStart < maxEnd && markdown[innerStart] == '`') { // Alternate double back-tick syntax. innerStart++; // Find the end of the span. innerEnd = Common.IndexOf(markdown, "``", innerStart, maxEnd); if (innerEnd == -1) return null; end = innerEnd + 2; } else { // Standard single backtick syntax. // Find the end of the span. innerEnd = Common.IndexOf(markdown, '`', innerStart, maxEnd); if (innerEnd == -1) return null; end = innerEnd + 1; } // The span must contain at least one character. if (innerStart == innerEnd) return null; // We found something! var result = new CodeInline(); result.Text = markdown.Substring(innerStart, innerEnd - innerStart).Trim(' ', '\t', '\r', '\n'); return new Common.InlineParseResult(result, start, end); }
/// <summary> /// Renders a code element /// </summary> /// <param name="inlineCollection"> The list to add to. </param> /// <param name="element"> The parsed inline element to render. </param> /// <param name="parent"> The container element. </param> /// <param name="context"> Persistent state. </param> private void RenderCodeRun(InlineCollection inlineCollection, CodeInline element, TextElement parent, RenderContext context) { var run = new Run(); run.FontFamily = CodeFontFamily ?? FontFamily; run.Text = CollapseWhitespace(context, element.Text); // Add it to the current inlines inlineCollection.Add(run); }