⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transforms.cs

📁 解压即可使用
💻 CS
📖 第 1 页 / 共 3 页
字号:
				RegexOptions options	= RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline ;
                string forumHomePath = Globals.GetSiteUrls().Emoticon;

				foreach ( Smiley smiley in emoticonTxTable ) {
					smileyPattern	= string.Format(@"\[{0}\]", Regex.Escape(smiley.SmileyCode) );
					replacePattern	= string.Format(imgFormat, forumHomePath, smiley.SmileyUrl, smiley.SmileyText + " [[SmileyCode]]");

					formattedPost = Regex.Replace( formattedPost, smileyPattern, replacePattern, options );
			
					if ( smiley.IsSafeWithoutBrackets() )
					{
						formattedPost = Regex.Replace( formattedPost, Regex.Escape(smiley.SmileyCode), 
								replacePattern, 
								options ); 
					}

					// If the smiley code contains a < or >, check to see if it was HtmlEncoded in the body
					if(smiley.SmileyCode.IndexOfAny( new char[] { '<', '>', '&' }) != -1)
					{
						try
						{
							smileyPattern	= string.Format(@"\[{0}\]", Regex.Escape(HttpContext.Current.Server.HtmlEncode(smiley.SmileyCode)) );
							formattedPost = Regex.Replace( formattedPost, smileyPattern, replacePattern, options );

							if(smiley.IsSafeWithoutBrackets())
							{
								formattedPost = Regex.Replace( formattedPost, Regex.Escape(HttpContext.Current.Server.HtmlEncode(smiley.SmileyCode)), 
									replacePattern, 
									options ); 
							}
						}
						catch(NullReferenceException) { }	// If we are outside of a web-context, referencing HttpContext.Current could throw a null ref.
					}

					// Finally, look for [SmileyCode] to place the smiley's code back in there (mostly for in the alt tags)
					// If the smiley was actually there, it could end up getting mistaken for another smiley... ie, [:)]
					// would get an alt tag of "Smile [:)]", it'll then search for the non-bracket version, and see :) and treat it as another instance
					formattedPost = Regex.Replace( formattedPost, @"\[SmileyCode\]", smiley.SmileyCode, options );

				}

				return formattedPost;
            } catch( Exception e ) {
                CSException ex = new CSException( CSExceptionType.UnknownError, "Could not transform smilies in the post.", e );
				ex.Log();

				return formattedPost;
            }
        }

        // *********************************************************************
        //  PerformUserTransforms
        //
        /// <summary>
        /// Performs the user defined transforms
        /// </summary>
        /// 
        // ********************************************************************/
        static string PerformUserTransforms(string stringToTransform, ArrayList userDefinedTransforms) {
            int iLoop = 0;			

            while (iLoop < userDefinedTransforms.Count) {		
		        
                // Special work for anchors
                stringToTransform = Regex.Replace(stringToTransform, userDefinedTransforms[iLoop].ToString(), userDefinedTransforms[iLoop+1].ToString(), RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);

                iLoop += 2;
            }

            return stringToTransform;
        }


        // *********************************************************************
        //  LoadTransformFile
        //
        /// <summary>
        /// Returns an array list containing transforms that the user defined in a transform file.
        /// </summary>
        /// 
        // ********************************************************************/
        private static ArrayList LoadTransformFile (string filename) {
            string cacheKey = "transformTable-" + filename;
            ArrayList tranforms;
            string filenameOfTransformFile;

            // read the transformation hashtable from the cache
            //
            tranforms = CSCache.Get(cacheKey) as ArrayList;

            if (tranforms == null) {

                tranforms = new ArrayList();

                // Grab the transform file
                //
                filenameOfTransformFile = CSContext.Current.Context.Request.MapPath("~/" + filename);

                if (filenameOfTransformFile.Length > 0) {

                    StreamReader sr = File.OpenText( filenameOfTransformFile );

                    // Read through each set of lines in the text file
                    //
                    string line = sr.ReadLine(); 
                    string replaceLine = "";

                    while (line != null) {

                        line = Regex.Escape(line);
                        replaceLine = sr.ReadLine();

                        // make sure replaceLine != null
                        //
                        if (replaceLine == null) 
                            break;
					
                        line = line.Replace("<CONTENTS>", "((.|\n)*?)");
                        line = line.Replace("<WORDBOUNDARY>", "\\b");
                        line = line.Replace("<", "&lt;");
                        line = line.Replace(">", "&gt;");
                        line = line.Replace("\"", "&quot;");

                        replaceLine = replaceLine.Replace("<CONTENTS>", "$1");					
					
                        tranforms.Add(line);
                        tranforms.Add(replaceLine);

                        line = sr.ReadLine();

                    }

                    // close the streamreader
                    //
                    sr.Close();		

                    // slap the ArrayList into the cache and set its dependency to the transform file.
                    //
                    CSCache.Insert(cacheKey, tranforms, new CacheDependency(filenameOfTransformFile));
                }
            }
  
            return tranforms;
        }
		#endregion

		#region FormatEditNotes()
        private static string FormatEditNotes2(string stringToTransform) {

            Match match;
            StringBuilder editTable = null;

            match = Regex.Match(stringToTransform, ".Edit by=&quot;(?<Editor>(.|\\n)*?)&quot;.(?<Notes>(.|\\n)*?)./Edit.", RegexOptions.IgnoreCase | RegexOptions.Compiled);

            if (match.Captures.Count > 0) {

                editTable = new StringBuilder();

                editTable.Append( "<table>" );
                editTable.Append( "<tr>" );
                editTable.Append( "<td>" );
                editTable.Append( match.Groups["Editor"].ToString() );
                editTable.Append( "</td>" );
                editTable.Append( "</tr>" );
                editTable.Append( "<tr>" );
                editTable.Append( "<td>" );
                editTable.Append( match.Groups["Notes"].ToString() );
                editTable.Append( "</td>" );
                editTable.Append( "</tr>" );
                editTable.Append( "</table>" );
            }
            if (editTable != null)
                return stringToTransform.Replace(match.ToString(), editTable.ToString());
            else
                return stringToTransform;
        }
		#endregion

		#region SourceCodeMarkup
        private static string SourceCodeMarkup(string stringToTransform) {
			StringBuilder formattedSource = new StringBuilder();
            string[] table = new string[3];

            table[0] = "<table border=\"0\" cellspacing=\"0\" width=\"100%\">";
            table[1] = "<tr><td width=\"15\"></td><td bgcolor=\"lightgrey\" width=\"15\"></td><td bgcolor=\"lightgrey\"><br>";
            table[2] = "<br>&nbsp;</td></tr></table>";
            MatchCollection matchs;

            // Look for code
            //
            matchs = Regex.Matches(stringToTransform, "\\[code language=\"(?<lang>(.|\\n)*?)\"\\](?<code>(.|\\n)*?)\\[/code\\]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);

            foreach (Match match in matchs) {

                // Remove HTML formatting code
                //
                string codeToFormat = match.Groups["code"].ToString().Replace("</P>\r\n", "<br>").Replace("<P>","");

                // Get the formatted source code
                //
                formattedSource.Append(table[0]);
                formattedSource.Append(table[1]);
                formattedSource.Append(FormatSource(codeToFormat, GetLanguageType(match.Groups["lang"].ToString())).Replace(Globals.HtmlNewLine, ""));
                formattedSource.Append(table[2]);

                // Update the main string
                //
                stringToTransform = stringToTransform.Replace(match.ToString(), formattedSource.ToString());
				formattedSource.Remove( 0, formattedSource.Length );
            }

            return stringToTransform;
        }
		#endregion

		#region PerformSpecializedTransforms
        private static string PerformSpecializedTransforms(string stringToTransform) {
            StringBuilder stringToReturn = new StringBuilder();
//            MatchCollection matchs;

            stringToTransform = SourceCodeMarkup(stringToTransform);

            return stringToTransform;

			// TDD 3/16/2004
			// commenting out the rest of this code since it's unreachable. If not needed after release then we can remove it.
//            // First we need to crack the string into segments to be transformed
//            // there is only 1 special marker we care about: [code language="xxx"][/code]
//            matchs = Regex.Matches(stringToTransform, "\\[code language=&quot;(?<lang>(.|\\n)*?)&quot;\\](?<code>(.|\\n)*?)\\[/code\\]", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Compiled);
//
//            // Get the full string that we intend to return
//            //
//            stringToReturn.Append(stringToTransform);
//
//            foreach (Match match in matchs) {
//                char[] charCode = new char[] {'\r', '\n'};
//                string sourceCodeToMarkup;
//                string markedUpSourceCode;
//
//                // Code/Markup content
//                //
//                sourceCodeToMarkup = match.Groups["code"].ToString().TrimStart(charCode).TrimEnd(charCode);
//                //				markedUpSourceCode = FormatSource(sourceCodeToMarkup, ));
//
//                // Replace marked up code
//                //
//                string openTable = "<table width=\"100%\"><tr><td width=\"15\"></td><td bgcolor=\"lightgrey\">";
//                string closeTable = "</td></tr></table>";
//                stringToReturn = stringToReturn.Replace(sourceCodeToMarkup, openTable + markedUpSourceCode + closeTable);
//
//                // Remove the code tags
//                //
//                string openCodeTag = "[code language=&quot;" + match.Groups["lang"] + "&quot;]";
//                string closeCodeTag = "[/code]";
//
//                stringToReturn = stringToReturn.Replace(openCodeTag, ""); 
//                stringToReturn = stringToReturn.Replace(closeCodeTag, ""); 
//
//            }
//
//            return stringToReturn.ToString();
//
        }
		#endregion

		#region Private vars, enums, and helper functions
        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 = 2;
        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();

            textBuffer.Write("<font face=\"Lucida Console\" size=\"" + _fontsize + "\">");

            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("</font>");

            return textBuffer.ToString();
        }

        static string FixCSLine(string source) {

            if (source == null)
                return null;

⌨️ 快捷键说明

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