public static string ToFormatted(this string source, TextFormattingOptions options)
 {
     return FormattingUtilities.FormatText(source, options);
 }
        public static string FormatText(string text, TextFormattingOptions options)
        {
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            string formatted = text;

            if (options.UrlEncode)
            {
                formatted = Encoder.UrlEncode(formatted);
            }
            else if (options.HtmlEncode)
            {
                formatted = Encoder.HtmlEncode(formatted);
            }
            else if (options.HtmlAttributeEncode)
            {
                //formatted = AntiXss.HtmlAttributeEncode(formatted);
                formatted = HttpUtility.HtmlAttributeEncode(formatted);
            }
            else if (options.JavascriptEncode)
            {
                formatted = HttpUtility.JavaScriptStringEncode(formatted);
            }
            else
            {
                formatted = RemoveStyleAndScriptDefinitions(text);

                if (options.RemoveAllTags)
                    formatted = RemoveAllTags(formatted);

                else if (options.BlackListedTags != null)
                    formatted = RemoveTags(formatted, options.BlackListedTags);

                else if (options.WhiteListedTags != null)
                    formatted = RemoveAllTagsExceptWhiteListed(formatted, options.WhiteListedTags);

                //formatted = AntiXss.GetSafeHtmlFragment(formatted);
            }

            if (options.MaxWordLength > 0)
                formatted = SplitLongWordsInText(formatted, options.MaxWordLength);

            if (options.MaxLength > 0 && formatted.Length > options.MaxLength)
                formatted = CropText(formatted, options.MaxLength);

            if (options.NewLinesToBreakTags)
            {
                formatted = formatted.Replace("\n", "<br />\n");
                formatted = formatted.Replace("&#13;", "<br />\n"); //we also replace the html-encoded newline chars.
            }

            if (options.LowerCaseFirstLetter)
                formatted = MakeFirstLetterToLowerCase(formatted);

            else if (options.UpperCaseFirstLetter)
                formatted = MakeFirstLetterUpperCase(formatted);

            return formatted;
        }