transforms.cs

来自「微软的.NET论坛的源代码(COOL!!!)」· CS 代码 · 共 377 行 · 第 1/2 页

CS
377
字号

                // Literal content
                literalContent = match.Groups["content"].ToString();
                literalContent = literalContent.Replace("\n", "\n" + Globals.HtmlNewLine + "\n");
                stringToReturn.Append(literalContent);

                // Code/Markup content
                codeContent = match.Groups["code"].ToString(); //stringToTransform.Substring(match.Index, match.Length);
                stringToReturn.Append( FormatSource(codeContent, GetLanguageType(match.Groups["lang"].ToString())));

                // Reset the currentIndexPosition pointer
                currentIndexPosition = (match.Index + match.Length) + 1;
            }

            // Return the content
            if (stringToReturn.Length == 0) {
                return stringToTransform.Replace("\n", "\n" + Globals.HtmlNewLine + "\n");
            } else {

                // Pick up any other characters in the string
                if (currentIndexPosition < stringToTransform.Length) {
    
                    string remainderOfMessage = stringToTransform.Substring(currentIndexPosition);
                    remainderOfMessage = remainderOfMessage.Replace("\n", "\n" + Globals.HtmlNewLine + "\n");

                    stringToReturn.Append(remainderOfMessage);
                }

                return stringToReturn.ToString();
            }

        }

        static Language GetLanguageType(string language) {
            
            switch (language.ToUpper()) {
                case "VB" :
                    return Language.VB;
                case "JS" :
                    return Language.JScript;
                case "JScript" :
                    return Language.JScript;
                default :
                    return Language.CS;
            }
        }

        public enum Language {
            CS,
            VB,
            JScript
        }

        const int _fontsize = 3;
        const string TAG_FNTRED  = "<font color= \"red\">";
        const string TAG_FNTBLUE = "<font color= \"blue\">" ;
        const string TAG_FNTGRN  = "<font color= \"green\">" ;
        const string TAG_FNTMRN  = "<font color=\"maroon\">" ;
        const string TAG_EFONT   = "</font>" ;


        public static string FormatSource(string htmlEncodedSource, Language language) {

            StringWriter textBuffer = new StringWriter();

            if ( _fontsize > 5 ) {
                textBuffer.Write("<font size=\"" + _fontsize + "\"><b>\r\n");
            } else {
                textBuffer.Write("<font size=\"" + _fontsize + "\">\r\n");
            }

            textBuffer.Write("<pre>");
            if(language == Language.CS) {
                textBuffer.Write(FixCSLine(htmlEncodedSource)) ;
            } else if(language == Language.JScript) {
                textBuffer.Write(FixJSLine(htmlEncodedSource)) ;
            } else if(language == Language.VB) {
                textBuffer.Write(FixVBLine(htmlEncodedSource)) ;
            }
            textBuffer.Write("</pre>");

            if ( _fontsize > 5 ) {
                textBuffer.Write("</b></font>");
            } else {
                textBuffer.Write("</font>");
            }

            return textBuffer.ToString();
        }

        static string FixCSLine(string source) {

            if (source == null)
                return null;

            source = Regex.Replace(source, "(?i)(\\t)", "    ");

            String[] keywords = new String[] {
                                                 "private", "protected", "public", "namespace", "class", "break",
                                                 "for", "if", "else", "while", "switch", "case", "using",
                                                 "return", "null", "void", "int", "bool", "string", "float",
                                                 "this", "new", "true", "false", "const", "static", "base",
                                                 "foreach", "in", "try", "catch", "finally", "get", "set", "char", "default"};

            String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")";

            source = Regex.Replace(source, "\\b" + CombinedKeywords + "\\b(?<!//.*)", TAG_FNTBLUE + "${keyword}" + TAG_EFONT);
            source = Regex.Replace(source, "(?<comment>//.*$)", TAG_FNTGRN + "${comment}" + TAG_EFONT);

            return source;
        }

        static string FixJSLine(string source) {
            if (source == null)
                return null;

            source = Regex.Replace(source, "(?i)(\\t)", "    ");

            String[] keywords = new String[] {
                                                 "private", "protected", "public", "namespace", "class", "var",
                                                 "for", "if", "else", "while", "switch", "case", "using", "get",
                                                 "return", "null", "void", "int", "string", "float", "this", "set",
                                                 "new", "true", "false", "const", "static", "package", "function",
                                                 "internal", "extends", "super", "import", "default", "break", "try",
                                                 "catch", "finally" };

            String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")";

            source = Regex.Replace(source, "\\b" + CombinedKeywords + "\\b(?<!//.*)", TAG_FNTBLUE + "${keyword}" + TAG_EFONT);
            source = Regex.Replace(source, "(?<comment>//.*$)", TAG_FNTGRN + "${comment}" + TAG_EFONT);

            return source;
        }

        static string FixVBLine(string source) {
            if (source == null)
                return null;

            source = Regex.Replace(source, "(?i)(\\t)", "    ");

            String[] keywords = new String[] {
                                                 "Private", "Protected", "Public", "End Namespace", "Namespace",
                                                 "End Class", "Exit", "Class", "Goto", "Try", "Catch", "End Try",
                                                 "For", "End If", "If", "Else", "ElseIf", "Next", "While", "And",
                                                 "Do", "Loop", "Dim", "As", "End Select", "Select", "Case", "Or",
                                                 "Imports", "Then", "Integer", "Long", "String", "Overloads", "True",
                                                 "Overrides", "End Property", "End Sub", "End Function", "Sub", "Me",
                                                 "Function", "End Get", "End Set", "Get", "Friend", "Inherits",
                                                 "Implements","Return", "Not", "New", "Shared", "Nothing", "Finally",
                                                 "False", "Me", "My", "MyBase" };


            String CombinedKeywords = "(?<keyword>" + String.Join("|", keywords) + ")";

            source = Regex.Replace(source, "(?i)\\b" + CombinedKeywords + "\\b(?<!'.*)", TAG_FNTBLUE + "${keyword}" + TAG_EFONT);
            source = Regex.Replace(source, "(?<comment>'(?![^']*&quot;).*$)", TAG_FNTGRN + "${comment}" + TAG_EFONT);

            return source;
        }
    }
}

/*


            // replace all carraige returns with <br> tags
            strBody = strBody.Replace("\n", "\n" + Globals.HtmlNewLine + "\n");

            // Ensure we have safe anchors
            Match m = Regex.Match(strBody, "href=\"((.|\\n)*?)\"");
            foreach(Capture capture in m.Captures) {

                if ( (!capture.ToString().StartsWith("href=\"http://")) )
                  strBody = strBody.Replace(capture.ToString(), "");

//      TODO          if ( (!capture.ToString().StartsWith("href=\"http://")) && (!capture.ToString().StartsWith("href=\"/")) )
//                    strBody = strBody.Replace(capture.ToString(), "");
            }

            // Create mailto links with any words containing @
//            strBody = Regex.Replace(strBody, "\\b((?<!<a\\s+href\\s*=\\s*\\S+)\\w+@([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?)", "$1", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            // replace all whitespace with &nbsp;
            //strBody = strBody.Replace(" ", "&nbsp;");

            return strBody;
            */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?