<commit_before>using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web.Hosting;
using System.Web.UI.WebControls;

using System.Linq;

using N2.Configuration;
using N2.Resources;
using N2.Web.Tokens;

namespace N2.Web.UI.WebControls
{
	/// <summary>
	/// A wrapper for the tinyMCE free text editor (http://tinymce.moxiecode.com).
	/// </summary>
	public class FreeTextArea : TextBox
	{
		static string configCssUrl;
		static string configScriptUrl;
		static NameValueCollection configSettings = new NameValueCollection();
		static bool configEnabled = true;
		static bool isInitalized = false;
		private Dictionary<string, string> customOverrides_ = new Dictionary<string, string>();
        private bool configTokensEnabled;

		public FreeTextArea()
		{
			TextMode = TextBoxMode.MultiLine;
			CssClass = "freeTextArea";
		}

		public virtual bool EnableFreeTextArea
		{
			get { return (bool)(ViewState["EnableFreeTextArea"] ?? configEnabled); }
			set { ViewState["EnableFreeTextArea"] = value; }
		}

		public virtual string DocumentBaseUrl
		{
			get { return (string)(ViewState["DocumentBaseUrl"]); }
			set { ViewState["DocumentBaseUrl"] = value; }
		}

		/// <summary> Custom TinyMCE config options </summary>  
		/// <remarks> Will override Web.config TinyMCE settings.
		///   See: http://tinymce.moxiecode.com/wiki.php/Configuration
		/// </remarks>
		public IDictionary<string, string> CustomOverrides { get { return customOverrides_; } }

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (!isInitalized)
			{
				isInitalized = true;
				var config = N2.Context.Current.Resolve<EditSection>();
				if (config != null)
				{
					configCssUrl = Url.ResolveTokens(config.TinyMCE.CssUrl);
					configScriptUrl = Url.ResolveTokens(config.TinyMCE.ScriptUrl);
					configEnabled = config.TinyMCE.Enabled;
                    configTokensEnabled = config.TinyMCE.EnableTokenDropdown;
					foreach (KeyValueConfigurationElement element in config.TinyMCE.Settings)
					{
						configSettings[element.Key] = element.Value;
					}
				}
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (EnableFreeTextArea)
			{
				Register.JQuery(Page);
				Register.TinyMCE(Page);
				Register.JavaScript(Page, configScriptUrl ?? Url.ResolveTokens("{ManagementUrl}/Resources/Js/FreeTextArea.js"));

				string freeTextAreaInitScript = string.Format("freeTextArea_init('{0}', {1});",
					Url.Parse(Page.Engine().ManagementPaths.EditTreeUrl),
					GetOverridesJson());
                Page.ClientScript.RegisterStartupScript(GetType(), "FreeTextArea_" + ClientID, freeTextAreaInitScript, true);
			}
		}

        private IEnumerable<TokenDefinition> GetTokens()
        {
            return Context.GetEngine().Resolve<TokenDefinitionFinder>().FindTokens();
        }

		protected virtual string GetOverridesJson()
		{
			IDictionary<string, string> overrides = new Dictionary<string, string>();
			overrides["elements"] = ClientID;
			overrides["content_css"] = configCssUrl ?? Url.ResolveTokens("{ManagementUrl}/Resources/Css/Editor.css");

            if (configTokensEnabled)
            {
                overrides["tokencomplete_enabled"] = "true";
                overrides["tokencomplete_settings"] = new { tokens = GetTokens() }.ToJson();
            }

			string language = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
			if (HostingEnvironment.VirtualPathProvider.FileExists(Url.ResolveTokens("{ManagementUrl}/Resources/tiny_mce/langs/" + language + ".js")))
				overrides["language"] = language;

			if (!string.IsNullOrEmpty(DocumentBaseUrl))
				overrides["document_base_url"] = Page.ResolveUrl(DocumentBaseUrl);

			foreach (string key in configSettings.AllKeys)
				overrides[key] = configSettings[key];

			foreach (var item in CustomOverrides)
				overrides[item.Key] = item.Value;

			return ToJsonString(overrides);
		}

		protected static string ToJsonString(IDictionary<string, string> collection)
		{
			var sb = new StringBuilder("{");

			foreach (string key in collection.Keys)
			{
				string value = collection[key];
				if (value == "true")
					value = "true";
				else if (value == "false")
					value = "false";
				else if (value == null)
					value = "null";
				else if (!value.StartsWith("[") && !value.StartsWith("{"))
					value = "'" + value + "'";
				sb.Append("'").Append(key).Append("': ").Append(value).Append(",");
			}
			if (sb.Length > 1)
				sb.Length--; // remove trailing comma
			return sb.Append("}").ToString();
		}
	}
}<commit_msg>Remember token complete setting second time<commit_after>using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web.Hosting;
using System.Web.UI.WebControls;

using System.Linq;

using N2.Configuration;
using N2.Resources;
using N2.Web.Tokens;

namespace N2.Web.UI.WebControls
{
	/// <summary>
	/// A wrapper for the tinyMCE free text editor (http://tinymce.moxiecode.com).
	/// </summary>
	public class FreeTextArea : TextBox
	{
		static string configCssUrl;
		static string configScriptUrl;
		static bool configEnabled = true;
		static bool isInitalized = false;
        static bool configTokensEnabled = true;
        static NameValueCollection configSettings = new NameValueCollection();
        private Dictionary<string, string> customOverrides_ = new Dictionary<string, string>();

		public FreeTextArea()
		{
			TextMode = TextBoxMode.MultiLine;
			CssClass = "freeTextArea";
		}

		public virtual bool EnableFreeTextArea
		{
			get { return (bool)(ViewState["EnableFreeTextArea"] ?? configEnabled); }
			set { ViewState["EnableFreeTextArea"] = value; }
		}

		public virtual string DocumentBaseUrl
		{
			get { return (string)(ViewState["DocumentBaseUrl"]); }
			set { ViewState["DocumentBaseUrl"] = value; }
		}

		/// <summary> Custom TinyMCE config options </summary>  
		/// <remarks> Will override Web.config TinyMCE settings.
		///   See: http://tinymce.moxiecode.com/wiki.php/Configuration
		/// </remarks>
		public IDictionary<string, string> CustomOverrides { get { return customOverrides_; } }

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (!isInitalized)
			{
				isInitalized = true;
				var config = N2.Context.Current.Resolve<EditSection>();
				if (config != null)
				{
					configCssUrl = Url.ResolveTokens(config.TinyMCE.CssUrl);
					configScriptUrl = Url.ResolveTokens(config.TinyMCE.ScriptUrl);
					configEnabled = config.TinyMCE.Enabled;
                    configTokensEnabled = config.TinyMCE.EnableTokenDropdown;
					foreach (KeyValueConfigurationElement element in config.TinyMCE.Settings)
					{
						configSettings[element.Key] = element.Value;
					}
				}
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			if (EnableFreeTextArea)
			{
				Register.JQuery(Page);
				Register.TinyMCE(Page);
				Register.JavaScript(Page, configScriptUrl ?? Url.ResolveTokens("{ManagementUrl}/Resources/Js/FreeTextArea.js"));

				string freeTextAreaInitScript = string.Format("freeTextArea_init('{0}', {1});",
					Url.Parse(Page.Engine().ManagementPaths.EditTreeUrl),
					GetOverridesJson());
                Page.ClientScript.RegisterStartupScript(GetType(), "FreeTextArea_" + ClientID, freeTextAreaInitScript, true);
			}
		}

        private IEnumerable<TokenDefinition> GetTokens()
        {
            return Context.GetEngine().Resolve<TokenDefinitionFinder>().FindTokens();
        }

		protected virtual string GetOverridesJson()
		{
			IDictionary<string, string> overrides = new Dictionary<string, string>();
			overrides["elements"] = ClientID;
			overrides["content_css"] = configCssUrl ?? Url.ResolveTokens("{ManagementUrl}/Resources/Css/Editor.css");

            if (configTokensEnabled)
            {
                overrides["tokencomplete_enabled"] = "true";
                overrides["tokencomplete_settings"] = new { tokens = GetTokens() }.ToJson();
            }

			string language = System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
			if (HostingEnvironment.VirtualPathProvider.FileExists(Url.ResolveTokens("{ManagementUrl}/Resources/tiny_mce/langs/" + language + ".js")))
				overrides["language"] = language;

			if (!string.IsNullOrEmpty(DocumentBaseUrl))
				overrides["document_base_url"] = Page.ResolveUrl(DocumentBaseUrl);

			foreach (string key in configSettings.AllKeys)
				overrides[key] = configSettings[key];

			foreach (var item in CustomOverrides)
				overrides[item.Key] = item.Value;

			return ToJsonString(overrides);
		}

		protected static string ToJsonString(IDictionary<string, string> collection)
		{
			var sb = new StringBuilder("{");

			foreach (string key in collection.Keys)
			{
				string value = collection[key];
				if (value == "true")
					value = "true";
				else if (value == "false")
					value = "false";
				else if (value == null)
					value = "null";
				else if (!value.StartsWith("[") && !value.StartsWith("{"))
					value = "'" + value + "'";
				sb.Append("'").Append(key).Append("': ").Append(value).Append(",");
			}
			if (sb.Length > 1)
				sb.Length--; // remove trailing comma
			return sb.Append("}").ToString();
		}
	}
}
{'fraction_non_alphanumeric': 0.12721485411140585, 'fraction_numerical': 0.0016976127320954908, 'mean_word_length': 3.631941031941032, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3102485', 'n_tokens_mistral': 3223, 'n_tokens_neox': 2841, 'n_words': 570}