/// <summary> /// Renders a text run element /// </summary> /// <param name="element"></param> /// <param name="currentInlines"></param> /// <param name="trimTextStart">If true this element should trin the start of the text and set to fales.</param> private void RenderItalicRun(ItalicTextElement element, InlineCollection currentInlines, ref bool trimTextStart) { // Create the text run Span italicSpan = new Span(); italicSpan.FontStyle = FontStyle.Italic; // Render the children into the italic inline. RenderInlineChildren(element, italicSpan.Inlines, ref trimTextStart); // Add it to the current inlines currentInlines.Add(italicSpan); }
/// <summary> /// Returns a list of trip chars for all of the inlines. These are used to detect the /// possible beginning of an inline. /// </summary> /// <returns></returns> public static List <InlineTripCharHelper> GetTripCharsList() { lock (s_tripCharList) { if (s_tripCharList.Count == 0) { s_tripCharList.Add(BoldTextElement.GetTripChars()); s_tripCharList.Add(ItalicTextElement.GetTripChars()); s_tripCharList.Add(MarkdownLinkInline.GetTripChars()); s_tripCharList.Add(RawHyperlinkInline.GetTripChars()); s_tripCharList.Add(RawSubredditInline.GetTripChars()); // Text run doesn't have one. } } return(s_tripCharList); }
/// <summary> /// Finds the next inline element by matching trip chars and verifying the match. /// </summary> /// <returns></returns> public static MarkdownInline FindNextInlineElement(ref string markdown, int startingPos, int endingPos, ref int nextElementStart, ref int nextElementEnd) { // Get the list of trip chars List <InlineTripCharHelper> tripChars = GetTripCharsList(); // Loop though all of the chars in this run and look for a trip char. for (int i = startingPos; i < endingPos; i++) { char currentChar = Char.ToLower(markdown[i]); // Try to match each trip char to the char foreach (InlineTripCharHelper currentTripChar in tripChars) { // Check if our current char matches the sufex char. if (currentChar == currentTripChar.FirstChar) { // We have a match! See if there is a suffix and if so if it matches. if (currentTripChar.FirstCharSuffix != null) { // We need to loop through the sufex and see if it matches the next n chars in the markdown. int suffexCharCounter = i + 1; bool suffexFound = true; foreach (char suffexChar in currentTripChar.FirstCharSuffix) { char test = Char.ToLower(markdown[suffexCharCounter]); if (suffexCharCounter >= endingPos || suffexChar != Char.ToLower(markdown[suffexCharCounter])) { suffexFound = false; break; } suffexCharCounter++; } // If the suffex didn't match this isn't a possibility. if (!suffexFound) { continue; } } // If we are here we have a possible match. Call into the inline class to verify. // Note! The order of bold and italic here is important because they both start with * // otherwise italic will consume bold's opening tag. switch (currentTripChar.Type) { case MarkdownInlineType.Bold: if (BoldTextElement.VerifyMatch(ref markdown, i, endingPos, ref nextElementStart, ref nextElementEnd)) { return(new BoldTextElement()); } break; case MarkdownInlineType.Italic: if (ItalicTextElement.VerifyMatch(ref markdown, i, endingPos, ref nextElementStart, ref nextElementEnd)) { return(new ItalicTextElement()); } break; case MarkdownInlineType.MarkdownLink: if (MarkdownLinkInline.VerifyMatch(ref markdown, i, endingPos, ref nextElementStart, ref nextElementEnd)) { return(new MarkdownLinkInline()); } break; case MarkdownInlineType.RawHyperlink: if (RawHyperlinkInline.VerifyMatch(ref markdown, i, endingPos, ref nextElementStart, ref nextElementEnd)) { return(new RawHyperlinkInline()); } break; case MarkdownInlineType.RawSubreddit: if (RawSubredditInline.VerifyMatch(ref markdown, i, endingPos, ref nextElementStart, ref nextElementEnd)) { return(new RawSubredditInline()); } break; } } } } // If we didn't find any elements we have a normal text block. // Let is consume the entire range. nextElementStart = startingPos; nextElementEnd = endingPos; return(new TextRunInline()); }