UniversalMarkdown.Parse.Elements.TextRunInline.Parse C# (CSharp) Method

Parse() static private method

Parses unformatted text.
static private Parse ( string markdown, int start, int end ) : TextRunInline
markdown string The markdown text.
start int The location to start parsing.
end int The location to stop parsing.
return TextRunInline
        internal static TextRunInline Parse(string markdown, int start, int end)
        {
            // Handle escape sequences and entities.
            // Note: this code is designed to be as fast as possible in the case where there are no
            // escape sequences and no entities (expected to be the common case).
            StringBuilder result = null;
            int textPos = start;
            int searchPos = start;
            while (searchPos < end)
            {
                // Look for the next backslash.
                int sequenceStartIndex = markdown.IndexOfAny(new char[] { '\\', '&' }, searchPos, end - searchPos);
                if (sequenceStartIndex == -1)
                    break;
                searchPos = sequenceStartIndex + 1;

                char decodedChar;
                if (markdown[sequenceStartIndex] == '\\')
                {
                    // This is an escape sequence, with one more character expected.
                    if (sequenceStartIndex >= end - 1)
                        break;

                    // Check if the character after the backslash can be escaped.
                    decodedChar = markdown[sequenceStartIndex + 1];
                    if (Array.IndexOf(escapeCharacters, decodedChar) < 0)
                    {
                        // This character cannot be escaped.
                        continue;
                    }

                    // This here's an escape sequence!
                    if (result == null)
                        result = new StringBuilder(end - start);
                    result.Append(markdown.Substring(textPos, sequenceStartIndex - textPos));
                    result.Append(decodedChar);
                    searchPos = textPos = sequenceStartIndex + 2;
                }
                else if (markdown[sequenceStartIndex] == '&')
                {
                    // This is an entity e.g. "&nbsp;".

                    // Look for the semicolon.
                    int semicolonIndex = markdown.IndexOf(';', sequenceStartIndex + 1, end - (sequenceStartIndex + 1));
                    if (semicolonIndex == -1)   // Unterminated entity.
                        continue;

                    // Okay, we have an entity, but is it one we recognise?
                    string entityName = markdown.Substring(sequenceStartIndex + 1, semicolonIndex - (sequenceStartIndex + 1));
                    if (entities.ContainsKey(entityName) == false)  // Unrecognised entity.
                        continue;

                    // This here's an escape sequence!
                    if (result == null)
                        result = new StringBuilder(end - start);
                    result.Append(markdown.Substring(textPos, sequenceStartIndex - textPos));
                    result.Append((char)entities[entityName]);
                    searchPos = textPos = semicolonIndex + 1;
                }
            }

            if (result != null)
            {
                result.Append(markdown.Substring(textPos, end - textPos));
                return new TextRunInline { Text = result.ToString() };
            }
            return new TextRunInline { Text = markdown.Substring(start, end - start) };
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// This function can be called by any element parsing. Given a start and stopping point this will
        /// parse all found elements out of the range.
        /// </summary>
        /// <param name="markdown"></param>
        /// <param name="startingPos"></param>
        /// <param name="maxEndingPos"></param>
        protected void ParseInlineChildren(ref string markdown, int startingPos, int maxEndingPos)
        {
            int currentParsePosition = startingPos;


            while (currentParsePosition < maxEndingPos)
            {
                int nextElemntStart = 0;
                int nextElementEnd = 0;

                // Find the next element
                MarkdownInline element = Common.FindNextInlineElement(ref markdown, currentParsePosition, maxEndingPos, ref nextElemntStart, ref nextElementEnd);

                // If the element we found doesn't start at the position we are looking for there is text between the element and
                // the start. We need to wrap it into a Text Run
                if (nextElemntStart != currentParsePosition)
                {
                    TextRunInline textRun = new TextRunInline();
                    textRun.Parse(ref markdown, currentParsePosition, nextElemntStart);
                    Children.Add(textRun);
                }

                // Ask it to parse, it will return us the ending pos of itself.
                currentParsePosition = element.Parse(ref markdown, nextElemntStart, nextElementEnd);

                // Add it the the children
                Children.Add(element);
            }
        }