Beispiel #1
0
        /// <summary/>
        public HtmlFormat()
        {
            const string regJavaScript     = @"(?<=&lt;script(?:\s.*?)?&gt;).+?(?=&lt;/script&gt;)";
            const string regComment        = @"&lt;!--.*?--&gt;";
            const string regAspTag         = @"&lt;%@.*?%&gt;|&lt;%|%&gt;";
            const string regAspCode        = @"(?<=&lt;%).*?(?=%&gt;)";
            const string regTagDelimiter   = @"(?:&lt;/?!?\??(?!%)|(?<!%)/?&gt;)+";
            const string regTagName        = @"(?<=&lt;/?!?\??(?!%))[\w\.:-]+(?=.*&gt;)";
            const string regAttributes     = @"(?<=&lt;(?!%)/?!?\??[\w:-]+).*?(?=(?<!%)/?&gt;)";
            const string regEntity         = @"&amp;\w+;";
            const string regAttributeMatch = @"(=?"".*?""|=?'.*?')|([\w:-]+)";

            //the regex object will handle all the replacements in one pass
            var regAll = "(" + regJavaScript + ")|(" + regComment + ")|("
                         + regAspTag + ")|(" + regAspCode + ")|("
                         + regTagDelimiter + ")|(" + regTagName + ")|("
                         + regAttributes + ")|(" + regEntity + ")";

            CodeRegex   = new Regex(regAll, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            attribRegex = new Regex(regAttributeMatch, RegexOptions.Singleline);

            csf = new CSharpFormat();
            jsf = new JavaScriptFormat();
        }
        /// <summary>
        /// Returns the formatted text.
        /// </summary>
        /// <param name="options">Whatever options were set in the regex groups.</param>
        /// <param name="text">Send the e.body so it can get formatted.</param>
        /// <returns>The formatted string of the match.</returns>
        private static string Highlight(HighlightOptions options, string text)
        {
            switch (options.Language)
            {
            case "c#":
                var csf = new CSharpFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                return(WebUtility.HtmlDecode(csf.FormatCode(text)));

            case "vb":
                var vbf = new VisualBasicFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                return(vbf.FormatCode(text));

            case "js":
                var jsf = new JavaScriptFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                return(WebUtility.HtmlDecode(jsf.FormatCode(text)));

            case "html":
                var htmlf = new HtmlFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                text = StripHtml(text).Trim();
                var code = htmlf.FormatCode(WebUtility.HtmlDecode(text)).Trim();
                return(code.Replace("\r\n", "<br />").Replace("\n", "<br />"));

            case "xml":
                var xmlf = new HtmlFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                text = text.Replace("<br />", "\r\n");
                text = StripHtml(text).Trim();
                var xml = xmlf.FormatCode(WebUtility.HtmlDecode(text)).Trim();
                return(xml.Replace("\r\n", "<br />").Replace("\n", "<br />"));

            case "tsql":
                var tsqlf = new TsqlFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                return(WebUtility.HtmlDecode(tsqlf.FormatCode(text)));

            case "msh":
                var mshf = new MshFormat
                {
                    LineNumbers = options.DisplayLineNumbers,
                    Alternate   = options.AlternateLineNumbers
                };
                return(WebUtility.HtmlDecode(mshf.FormatCode(text)));
            }

            return(string.Empty);
        }