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

📄 formatter.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text.RegularExpressions;

namespace CommunityServer.Components {

	public class Formatter {

        /// <summary>
        /// Many WYSWIGY editors accidently convert local Urls to relative Urls. ConvertLocalUrls looks for these
        /// and attempts to convert them back.
        /// </summary>
        public static string ConvertLocalUrls(string body, string host)
        {
            return Regex.Replace(body,"<a href=\"/","<a href=\"" + host + "/" ,RegexOptions.IgnoreCase);
        }


        /// <summary>
        /// We do not allow any markup in comments. This method will transform links to full hrefs and 
        /// add the ref=nofollow attribute
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string SafeFeedback(string text)
        {
            if(Globals.IsNullorEmpty(text))
                return text;

            text = Globals.HtmlEncode(text);

            //Find any links
            string pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])";
            MatchCollection matchs;
                        
            matchs = Regex.Matches(text,pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match m in matchs) 
            {
                text = text.Replace(m.ToString(), "<a rel=\"nofollow\" target=\"_new\" href=\"" + m.ToString() + "\">" + m.ToString() + "</a>");
            }

            return text.Replace("\n", "<br>");			
        }

		#region FormatFontSize
		public static string FormatFontSize(double fontsize) {

			CSContext csContext = CSContext.Current;

			if (!csContext.User.IsAnonymous) {
				int size = csContext.User.Profile.FontSize;

				switch (size) {
					case -1:
						fontsize = fontsize - (fontsize * .02);
						break;

					case 1:
						fontsize = fontsize + (fontsize * .15);
						break;

					case 2:
						fontsize = fontsize + (fontsize * .30);
						break;

				}
			}

			return fontsize.ToString().Replace(",",".") + "em";
		}
		#endregion

		#region Format Date
		public static string FormatDate (DateTime date) {
			return FormatDate(date, false);
		}

		public static string FormatDate (DateTime date, bool showTime) {
		    CSContext csContext = CSContext.Current;
			string dateFormat = CSContext.Current.User.Profile.DateFormat;
            
			if (csContext.User.UserID > 0) {
				dateFormat = csContext.User.Profile.DateFormat;
				date = csContext.User.GetTimezone(date);
			}

			if (showTime)
				return date.ToString(dateFormat + " " + CSContext.Current.SiteSettings.TimeFormat);
			else
				return date.ToString(dateFormat);
		}
		#endregion

		#region Format Date Post
		// Used to return a date in Word format (Today, Yesterday, Last Week, etc)
		//
		public static string FormatDatePost (DateTime date) {
			// This doesn't have to be as complicated as the FormatLastPost.
			// TODO: Need to optimize FormatLastPost (the multiple calls to GetUser()).
			//
			string returnItem;
			string dateFormat;
			DateTime userLocalTime;
			User user;

			// Optimizing code to only grab GetUser() once, since it is a lot of overhead.
			//
			user = CSContext.Current.User;
			
			// Setting up the user's date profile
			//
			if (user.UserID > 0) {
				date = user.GetTimezone(date);
				dateFormat = user.Profile.DateFormat;
				userLocalTime = user.GetTimezone(DateTime.Now);
			} else {
				// date is already set
				dateFormat = CSContext.Current.SiteSettings.DateFormat;
				userLocalTime = DateTime.Now;
			}
			
			// little error checking
			//
			if (date < DateTime.Now.AddYears(-20) )
				return ResourceManager.GetString("NumberWhenZero");
			
			// make Today and Yesterday bold for now...
			//
			if ((date.DayOfYear == userLocalTime.DayOfYear) && (date.Year == userLocalTime.Year)) {

				returnItem = ResourceManager.GetString("TodayAt");
				returnItem += ((DateTime) date).ToString(CSContext.Current.SiteSettings.TimeFormat);

			} else if ((date.DayOfYear == (userLocalTime.DayOfYear - 1)) && (date.Year == userLocalTime.Year)) {

				returnItem = ResourceManager.GetString("YesterdayAt");
				returnItem += ((DateTime) date).ToString(CSContext.Current.SiteSettings.TimeFormat);

			} else {

				returnItem = date.ToString(dateFormat) + ", " + ((DateTime) date).ToString(CSContext.Current.SiteSettings.TimeFormat);

			}
			return returnItem;
		}
		#endregion

		#region Expand/Collapse Icon
		public static string ExplandCollapseIcon (Group group) {

			if (group.HideSections)
				return Globals.GetSkinPath() +"/images/expand-closed.gif";
            
			return Globals.GetSkinPath() +"/images/expand-open.gif";
            
		}
		#endregion



        #region Format number
        public static string FormatNumber (int number) {
            return FormatNumber (number, ResourceManager.GetString("NumberFormat"), ResourceManager.GetString("NumberWhenZero"));
        }

        public static string FormatNumber (int number, string whenZero) {
            return FormatNumber (number, ResourceManager.GetString("NumberFormat"), whenZero);
        }

        public static string FormatNumber (int number, string format, string whenZero) {

            if (number == 0)
                return whenZero;
            else
                return number.ToString(format);

        }
        #endregion

        #region String length formatter
        public static string CheckStringLength (string stringToCheck, int maxLength) {
            string checkedString = null;

            if (stringToCheck.Length <= maxLength)
                return stringToCheck;

            // If the string to check is longer than maxLength 
            // and has no whitespace we need to trim it down
            if ((stringToCheck.Length > maxLength) && (stringToCheck.IndexOf(" ") == -1)) {
                checkedString = stringToCheck.Substring(0, maxLength) + "...";
            } else if (stringToCheck.Length > 0) {
                string[] words;
                int expectedWhitespace = stringToCheck.Length / 8;

                // How much whitespace is there?
                words = stringToCheck.Split(' ');

                // If the number of wor
                //if (expectedWhitespace > words.Length)
                    checkedString = stringToCheck.Substring(0, maxLength) + "...";
                //else
                //    checkedString = stringToCheck;
            } else {
                checkedString = stringToCheck;
            }

            return checkedString;
        }
        #endregion

		#region Strip All Tags from a String
		/*
		 * Takes a string and strips all bbcode and html from the
		 * the string. Replacing any <br />s with linebreaks.  This
		 * method is meant to be used by ToolTips to present a
		 * a stripped-down version of the post.Body
		 *
		 */
		public static string StripAllTags ( string stringToStrip ) {
			// paring using RegEx
			//
			stringToStrip = Regex.Replace(stringToStrip, "</p(?:\\s*)>(?:\\s*)<p(?:\\s*)>", "\n\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
			stringToStrip = Regex.Replace(stringToStrip, "<br(?:\\s*)/>", "\n", RegexOptions.IgnoreCase | RegexOptions.Compiled);
			stringToStrip = Regex.Replace(stringToStrip, "\"", "''", RegexOptions.IgnoreCase | RegexOptions.Compiled);
			stringToStrip = Transforms.StripHtmlXmlTags( stringToStrip );

			return stringToStrip;
		}

        public static string RemoveHtml(string html)
        {
           return RemoveHtml(html,0);
        }

⌨️ 快捷键说明

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