예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlFormat"/> class.
        /// The html format.
        /// </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
            const string RegAll = "(" + RegJavaScript + ")|(" + RegComment + ")|(" + RegAspTag + ")|(" + RegAspCode + ")|(" +
                                  RegTagDelimiter + ")|(" + RegTagName + ")|(" + RegAttributes + ")|(" + RegEntity + ")";

            this.CodeRegex   = new Regex(RegAll, RegexOptions.IgnoreCase | RegexOptions.Singleline);
            this.attribRegex = new Regex(RegAttributeMatch, RegexOptions.Singleline);

            this.csf = new CSharpFormat();
            this.jsf = new JavaScriptFormat();
        }
예제 #2
0
        private JavaScriptFormat jsf; //to format client-side JavaScript code

        #endregion Fields

        #region Constructors

        /// <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
            string 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();
        }
예제 #3
0
    /// <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>
    public string Highlight(HighlightOptions options, string text)
    {
        switch (options.Language)
        {
            case "c#":
                CodeFormatter.CSharpFormat csf = new CodeFormatter.CSharpFormat();
                csf.LineNumbers = options.DisplayLineNumbers;
                csf.Alternate = options.AlternateLineNumbers;
                return csf.FormatCode(text);

            case "vb":
                CodeFormatter.VisualBasicFormat vbf = new CodeFormatter.VisualBasicFormat();
                vbf.LineNumbers = options.DisplayLineNumbers;
                vbf.Alternate = options.AlternateLineNumbers;
                //e.Body = codeRegex.Replace(text, new MatchEvaluator(CodeEvaluator));
                //e.Body = codeBeginTagRegex.Replace(codeRegex.Replace(text, new MatchEvaluator(CodeEvaluator)), @"<p>");
                //e.Body = codeEndTagRegex.Replace(codeBeginTagRegex.Replace(codeRegex.Replace(text, new MatchEvaluator(CodeEvaluator)), @"<p>"), @"</p>");
                return vbf.FormatCode(text);

            case "js":
                CodeFormatter.JavaScriptFormat jsf = new CodeFormatter.JavaScriptFormat();
                jsf.LineNumbers = options.DisplayLineNumbers;
                jsf.Alternate = options.AlternateLineNumbers;
                return jsf.FormatCode(text);

            case "html":
                CodeFormatter.HtmlFormat htmlf = new CodeFormatter.HtmlFormat();
                htmlf.LineNumbers = options.DisplayLineNumbers;
                htmlf.Alternate = options.AlternateLineNumbers;
                text = StripHtml(text).Trim();
                string code = htmlf.FormatCode(text).Trim();
                return code.Replace(Environment.NewLine, "<br />");

            case "xml":
                CodeFormatter.HtmlFormat xmlf = new CodeFormatter.HtmlFormat();
                xmlf.LineNumbers = options.DisplayLineNumbers;
                xmlf.Alternate = options.AlternateLineNumbers;
                text = StripHtml(text).Trim();
                string xml = xmlf.FormatCode(text).Trim();
                return xml.Replace(Environment.NewLine, "<br />");

            case "tsql":
                CodeFormatter.TsqlFormat tsqlf = new CodeFormatter.TsqlFormat();
                tsqlf.LineNumbers = options.DisplayLineNumbers;
                tsqlf.Alternate = options.AlternateLineNumbers;
                return tsqlf.FormatCode(text);

            case "msh":
                CodeFormatter.MshFormat mshf = new CodeFormatter.MshFormat();
                mshf.LineNumbers = options.DisplayLineNumbers;
                mshf.Alternate = options.AlternateLineNumbers;
                return mshf.FormatCode(text);
        }

        return string.Empty;
    }
예제 #4
0
    /// <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 HttpContext.Current.Server.HtmlDecode(csf.FormatCode(text));

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

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

            case "html":
                var htmlf = new HtmlFormat
                    {
                        LineNumbers = options.DisplayLineNumbers,
                        Alternate = options.AlternateLineNumbers
                    };
                text = Utils.StripHtml(text);
                var code = htmlf.FormatCode(HttpContext.Current.Server.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 = Utils.StripHtml(text);
                var xml = xmlf.FormatCode(HttpContext.Current.Server.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 HttpContext.Current.Server.HtmlDecode(tsqlf.FormatCode(text));

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

        return string.Empty;
    }
예제 #5
0
    /// <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 string Highlight(HighlightOptions options, string text)
    {
        switch (options.Language)
        {
            case "c#":
                CSharpFormat csf = new CSharpFormat();
                csf.LineNumbers = options.DisplayLineNumbers;
                csf.Alternate = options.AlternateLineNumbers;
                return HttpContext.Current.Server.HtmlDecode(csf.FormatCode(text));

            case "vb":
                VisualBasicFormat vbf = new VisualBasicFormat();
                vbf.LineNumbers = options.DisplayLineNumbers;
                vbf.Alternate = options.AlternateLineNumbers;
                return HttpContext.Current.Server.HtmlDecode(vbf.FormatCode(text));

            case "js":
                JavaScriptFormat jsf = new JavaScriptFormat();
                jsf.LineNumbers = options.DisplayLineNumbers;
                jsf.Alternate = options.AlternateLineNumbers;
                return HttpContext.Current.Server.HtmlDecode(jsf.FormatCode(text));

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

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

            case "tsql":
                TsqlFormat tsqlf = new TsqlFormat();
                tsqlf.LineNumbers = options.DisplayLineNumbers;
                tsqlf.Alternate = options.AlternateLineNumbers;
                return HttpContext.Current.Server.HtmlDecode(tsqlf.FormatCode(text));

            case "msh":
                MshFormat mshf = new MshFormat();
                mshf.LineNumbers = options.DisplayLineNumbers;
                mshf.Alternate = options.AlternateLineNumbers;
                return HttpContext.Current.Server.HtmlDecode(mshf.FormatCode(text));

        }

        return string.Empty;
    }