Text Parsing - My Parser Skipping commands

Posted by The.Anti.9 on Stack Overflow See other posts from Stack Overflow or by The.Anti.9
Published on 2010-05-25T19:06:23Z Indexed on 2010/05/25 19:11 UTC
Read the original article Hit count: 374

Filed under:
|

I'm trying to parse text-formatting. I want to mark inline code, much like SO does, with backticks (`). The rule is supposed to be that if you want to use a backtick inside of an inline code element, You should use double backticks around the inline code.

like this:

`` mark inline code with backticks ( ` ) ``

My parser seems to skip over the double backticks completely for some reason. Heres the code for the function that does the inline code parsing:

    private string ParseInlineCode(string input)
    {
        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == '`' && input[i - 1] != '\\')
            {
                if (input[i + 1] == '`')
                {
                    string str = ReadToCharacter('`', i + 2, input);
                    while (input[i + str.Length + 2] != '`')
                    {
                        str += ReadToCharacter('`', i + str.Length + 3, input);
                    }
                    string tbr = "``" + str + "``";
                    str = str.Replace("&", "&amp;");
                    str = str.Replace("<", "&lt;");
                    str = str.Replace(">", "&gt;");
                    input = input.Replace(tbr, "<code>" + str + "</code>");
                    i += str.Length + 13;
                }
                else
                {
                    string str = ReadToCharacter('`', i + 1, input);
                    input = input.Replace("`" + str + "`", "<code>" + str + "</code>");
                    i += str.Length + 13;
                }
            }
        }
        return input;
    }

If I use single backticks around something, it wraps it in the <code> tags correctly.

© Stack Overflow or respective owner

Related posts about c#

Related posts about text-parsing