Пример #1
0
        private static string ParseCode(string text)
        {
            StringBuilder result = new StringBuilder();
            Regex codeStartRegex = new Regex(string.Format(@"<{0}>", CODE_ELEMENT), RegexOptions.Multiline);
            Regex codeEndRegex = new Regex(string.Format(@"</{0}>", CODE_ELEMENT), RegexOptions.Multiline);
            string[] textParts = null;
            bool isInCodeElement = false;

            CSharpSyntaxHighlighter codeHighlighter = new CSharpSyntaxHighlighter();

            while (text.Length > 0)
            {
                if (!isInCodeElement)
                {
                    textParts = codeStartRegex.Split(text, 2);
                    if (textParts.Length == 2)
                    {
                        result.Append(textParts[0]);
                        text = textParts[1];
                        isInCodeElement = true;
                    }
                    else
                    {
                        result.Append(textParts[0]);
                        break;
                    }
                }
                else
                {
                    textParts = codeEndRegex.Split(text, 2);
                    if (textParts.Length == 2)
                    {
                        result.Append(codeHighlighter.Process(textParts[0]));
                        text = textParts[1];
                        isInCodeElement = false;
                    }
                    else
                    {
                        result.Append(textParts[0]);
                        break;
                    }
                }
            }
            return result.ToString();
        }
Пример #2
0
        private static string ReinsertElementsWithNoFormatting(string text, Dictionary<string, WikiElement> elementSubstitutions)
        {
            CSharpSyntaxHighlighter cSharpHighlighter = new CSharpSyntaxHighlighter();
            SqlSyntaxHighlighter sqlHighlighter = new SqlSyntaxHighlighter();

            foreach (KeyValuePair<string, WikiElement> element in elementSubstitutions)
            {
                string noFormattingElementTag = element.Value.Tag.ToLower();
                string contentToReinsert = element.Value.Content;

                if (noFormattingElementTag == "!--")
                    text = text.Replace(element.Key, contentToReinsert);
                else if (noFormattingElementTag == "nowiki")
                    text = text.Replace(element.Key, Tools.TextToXml(contentToReinsert));   // Escapes the HTML.
                else if (noFormattingElementTag == "pre")
                {
                    //string attributes = element.Value.Attributes.Count > 0 ? element.Value.Attributes.ToString() : string.Empty;
                    //text = text.Replace(element.Key, string.Format("{0}{1}", attributes, Tools.TextToXml(contentToReinsert)));
                    text = text.Replace(element.Key, Tools.TextToXml(contentToReinsert));
                }
                else if (noFormattingElementTag == "code" || noFormattingElementTag == "sql")
                {
                    // Modify the text contained within &lt;code&gt; tags with markup for colourization.
                    string colourizedCode = noFormattingElementTag == "code" ? cSharpHighlighter.Process(contentToReinsert) : sqlHighlighter.Process(contentToReinsert);
                    foreach(KeyValuePair<string, string> codeKeyWord in DataModule.CodeKeywordsIndex)
                        colourizedCode = colourizedCode.Replace(codeKeyWord.Key, Tools.GetLinkToRead(codeKeyWord.Value, codeKeyWord.Key));
                    text = text.Replace(element.Key, colourizedCode);
                }
            }
            return text;
        }