示例#1
0
        public ActionResult New(string message = null)
        {
            ViewData["UserState"] = AppUserState;

            var snippet = new CodeSnippet();

            snippet.Author = this.AppUserState.Name;

            string codeUrl = Request.QueryString["url"];

            if (!string.IsNullOrEmpty(codeUrl))
            {
                HttpClient client = new HttpClient();
                client.Timeout = 4000;
                snippet.Code   = client.DownloadString(codeUrl);

                snippet.Title = Path.GetFileName(codeUrl);
                string extension = Path.GetExtension(codeUrl);

                snippet.Language = CodeFormatFactory.GetStringLanguageFromExtension(extension);
                Response.Write(snippet.Language);
            }

            if (!string.IsNullOrEmpty(message))
            {
                this.ErrorDisplay.ShowMessage(message);
            }

            return(this.View("New", snippet));
        }
示例#2
0
        /// <summary>
        /// Returns formatted code for the specified language.
        ///
        /// see CSharpFormat for supported types
        /// </summary>
        /// <param name="code"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public string GetFormattedCode(string code, string language, bool showLineNumbers = false, bool allowWordWrap = false)
        {
            SourceFormat formatter = CodeFormatFactory.Create(language);

            formatter.AllowWordWrapping = allowWordWrap;

            if (showLineNumbers)
            {
                formatter.LineNumbers = true;
            }

            return(formatter.FormatCode(code));
        }