예제 #1
0
        /// <summary>
        /// Takes the input collection and parses it into an HTML string. First row is considered to be the
        /// header of the table.
        /// </summary>
        /// <param name="tableData"></param>
        /// <returns></returns>
        public string ToTableHtml(ObservableCollection <ObservableCollection <CellContent> > tableData = null)
        {
            if (tableData == null)
            {
                tableData = TableData;
            }

            if (tableData == null || tableData.Count < 1)
            {
                return(string.Empty);
            }

            for (int i = tableData.Count - 1; i > -1; i--)
            {
                if (tableData[i] == null || tableData[i].Count == 0)
                {
                    tableData.Remove(tableData[i]);
                }
            }

            var columnInfo = GetColumnInfo(tableData);

            StringBuilder sb = new StringBuilder();

            sb.Clear();

            sb.AppendLine("\n<table>");
            sb.AppendLine("<thead>");
            sb.AppendLine("\t<tr>");

            for (int i = 0; i < columnInfo.Count; i++)
            {
                var colInfo = columnInfo[i];
                sb.AppendLine($"\t\t<th>{HtmlUtils.HtmlEncode(colInfo.Title.Trim())}</th>");
            }

            sb.AppendLine("\t</tr>");
            sb.AppendLine("</thead>");

            sb.AppendLine("<tbody>");
            foreach (var row in tableData.Skip(1))
            {
                sb.AppendLine("\t<tr>");
                for (int i = 0; i < row.Count; i++)
                {
                    var col = row[i];
                    col.Text = col.Text.Replace("\n", "<br>").Replace("\r", "");
                    sb.AppendLine($"\t\t<td>{HtmlUtils.HtmlEncode(col.Text.Trim())}</td>");
                }

                sb.AppendLine("\t</tr>");
            }

            sb.AppendLine("</tbody>");
            sb.AppendLine("</table>\n");

            return(sb.ToString());
        }
예제 #2
0
        /// <summary>
        /// Returns an author either as a name
        /// </summary>
        /// <returns></returns>
        public string GetAuthorLink()
        {
            if (UserId == string.Empty)
            {
                return(HtmlUtils.HtmlEncode(Author));
            }

            return(HtmlUtils.Href(HtmlUtils.HtmlEncode(Author), WebUtils.ResolveUrl("~/list/user/") + UserId));
        }
예제 #3
0
 private string Encode(object obj)
 {
     if (obj != null)
     {
         return(_HtmlTableSetting.IsHtmlEncodeMode ? HtmlUtils.HtmlEncode(obj.ToString()) : obj.ToString());
     }
     else
     {
         return("");
     }
 }
예제 #4
0
        static void AssertHtmlEncode(string text, string expected)
        {
            string encoded, decoded;

            encoded = HtmlUtils.HtmlEncode(text);
            Assert.AreEqual(expected, encoded, "HtmlEncode(string)");

            encoded = HtmlUtils.HtmlEncode(text, 0, text.Length);
            Assert.AreEqual(expected, encoded, "HtmlEncode(string,int,int)");

            encoded = HtmlUtils.HtmlEncode(text.ToCharArray(), 0, text.Length);
            Assert.AreEqual(expected, encoded, "HtmlEncode(char[],int,int)");

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlEncode(writer, text);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlEncode(TextWriter,string)");
            }

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlEncode(writer, text, 0, text.Length);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlEncode(TextWriter,string,int,int)");
            }

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlEncode(writer, text.ToCharArray(), 0, text.Length);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlEncode(TextWriter,char[],int,int)");
            }

            decoded = HtmlUtils.HtmlDecode(encoded);
            Assert.AreEqual(text, decoded, "HtmlDecode(string)");

            decoded = HtmlUtils.HtmlDecode(encoded, 0, encoded.Length);
            Assert.AreEqual(text, decoded, "HtmlDecode(string,int,int)");

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlDecode(writer, encoded);
                decoded = writer.ToString();
                Assert.AreEqual(text, decoded, "HtmlDecode(TextWriter,string)");
            }

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlDecode(writer, encoded, 0, encoded.Length);
                decoded = writer.ToString();
                Assert.AreEqual(text, decoded, "HtmlDecode(TextWriter,string,int,int)");
            }
        }
예제 #5
0
        public string RenderTemplate(string templateText, object model, out string error)
        {
            error = null;

            string result = RazorHost.RenderTemplate(templateText, model);

            if (result == null)
            {
                result =
                    "<h3>Template Rendering Error</h3>\r\n<hr/>\r\n" +
                    "<pre>" + HtmlUtils.HtmlEncode(RazorHost.ErrorMessage) + "</pre>";
            }

            return(result);
        }
예제 #6
0
        public static JsonMessage HtmlEncode(string text)
        {
            JsonMessage jsonMessage = new JsonMessage();

            if (string.IsNullOrEmpty(text))
            {
                jsonMessage.Flag    = false;
                jsonMessage.Message = "编码内容不能为空";
                return(jsonMessage);
            }
            try {
                jsonMessage.Flag = true;
                jsonMessage.Data = HtmlUtils.HtmlEncode(text);
            }
            catch (Exception ex) {
                jsonMessage.Flag    = false;
                jsonMessage.Message = ex.Message;
            }
            return(jsonMessage);
        }
        /// <summary>
        /// Parses a single HttpRequestData object to HTML
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public string ToHtml(bool asDocument = true)
        {
            //return TemplateRenderer.RenderTemplate("Request.cshtml", this);

            HttpRequestData req = this;

            StringBuilder sb   = new StringBuilder();
            string        html = "";

            if (!string.IsNullOrEmpty(req.ErrorMessage))
            {
                sb.AppendLine("<div class='error-display'>");
                sb.AppendLine(@"<div class='error-header'>Error Message</div>");
                sb.AppendLine(@"<div>" + req.ErrorMessage + "</div>");
                sb.AppendLine("</div>");
            }

            html = @"
<h3>Request Headers</h3>
<pre>
<b><span style='color: darkred;'>{0}</span> <a href='{1}'>{1}</a></b> HTTP/1.1
";
            sb.AppendFormat(html, req.HttpVerb, req.Url);

            if (!string.IsNullOrEmpty(req.ContentType))
            {
                sb.AppendLine("Content-type: " + req.ContentType);
            }

            if (req.Headers != null)
            {
                foreach (var header in req.Headers)
                {
                    sb.AppendLine(header.Name + ": " + header.Value);
                }
            }

            if (!string.IsNullOrEmpty(req.RequestContent))
            {
                sb.AppendLine();
                sb.AppendLine(HtmlUtils.HtmlEncode(req.RequestContent.Trim()));
            }

            sb.AppendLine("</pre>");

            if (req.TimeTakenMs > 0)
            {
                sb.AppendFormat("<div class='timetaken'>{0}ms</div>", req.TimeTakenMs.ToString("n0"));
            }

            if (!string.IsNullOrEmpty(req.StatusCode))
            {
                html = @"<h3>Http Response</h3>
<pre>";

                sb.AppendLine(html);


                string cssClass = req.StatusCode.CompareTo("399") > 0 ? "error-response" : "success-response";


                if (!string.IsNullOrEmpty(req.StatusCode))
                {
                    sb.AppendFormat("<div class='{0}'>HTTP/1.1 {1} {2}</div>", cssClass, req.StatusCode, req.StatusDescription);
                }

                sb.AppendLine(req.ResponseHeaders);

                if (req.ResponseContent != null)
                {
                    sb.Append(HtmlUtils.HtmlEncode(req.ResponseContent.Trim()));
                }
            }

            if (!asDocument)
            {
                return(sb.ToString());
            }

            html = @"<!DOCTYPE HTML>
<html>
<head>
    <link href='css/WebSurge.css' type='text/css' rel='stylesheet' />
</head>
<body>
";
            return(html + sb + "</body>\r\n</html>");
        }
예제 #8
0
        public void TestArgumentExceptions()
        {
            var          writer = new StringWriter();
            const string text   = "text";

            // HtmlAttributeEncode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, null));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode((string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text, 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text, 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode((char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, (string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text.ToCharArray(), 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, (char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), 0, text.Length, 'x'));

            // HtmlEncode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode((string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode((char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text.ToCharArray(), 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, (string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text.ToCharArray(), 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, (char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text.ToCharArray(), 0, text.Length + 1));

            // HtmlDecode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(writer, null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(writer, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(writer, text, 0, text.Length + 1));
        }
예제 #9
0
        /// <summary>
        /// Returns and rss or atom feed
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        protected internal ActionResult GetFeed(object instance)
        {
            try
            {
                string title  = "CodePaste.NET";
                string action = this.RouteData.Values["listAction"] as string;
                if (string.IsNullOrEmpty(action))
                {
                    action = this.RouteData.Values["Action"] as string ?? string.Empty;
                }
                action = action.ToLower();

                if (action == "recent")
                {
                    title = "CodePaste.NET Recent Snippets";
                }
                else if (action == "mysnippets")
                {
                    title = "CodePaste.NET - My Snippets";
                }

                SyndicationFeed feed = new SyndicationFeed(title, "Paste and Link .NET Code", new Uri(Request.Url.AbsoluteUri));

                feed.BaseUri         = new Uri("http://codepaste.net/recent");
                feed.LastUpdatedTime = DateTime.Now;

                List <SyndicationItem> feedItems = new List <SyndicationItem>();
                foreach (CodeSnippetListItem item in (IEnumerable)instance)
                {
                    // remove lower ascii characters (< 32 exclude 9,10,13)
                    string code = Regex.Replace(item.Code, @"[\u0000-\u0008,\u000B,\u000C,\u000E-\u001F]", "");

                    SyndicationItem rssItem = new SyndicationItem()
                    {
                        Id      = item.Id,
                        Title   = SyndicationContent.CreateHtmlContent(item.Title),
                        Content = SyndicationContent.CreateHtmlContent(
                            //"<link href=\"" + WebUtils.ResolveServerUrl("~/css/csharp.css") + "\" rel=\"stylesheet\" type=\"text/css\">\r\n <style type='text/css'>.kwrd { color: blue; font-weight: bold }</style>\r\n" +
                            "<pre>\r\n" +
                            HtmlUtils.HtmlEncode(code) +
                            //snippet.GetFormattedCode(item.Code, item.Language, item.ShowLineNumbers) +
                            "</pre>"),
                        PublishDate = item.Entered,
                    };

                    if (!string.IsNullOrEmpty(item.Author))
                    {
                        rssItem.Authors.Add(new SyndicationPerson("", item.Author, null));
                    }

                    rssItem.Links.Add(new SyndicationLink(new Uri(WebUtils.GetFullApplicationPath() + "/" + item.Id),
                                                          "alternate", item.Title, "text/html", 1000));

                    feedItems.Add(rssItem);
                }

                feed.Items = feedItems;

                MemoryStream ms       = new MemoryStream();
                var          settings = new XmlWriterSettings()
                {
                    CheckCharacters = false
                };
                XmlWriter writer = XmlWriter.Create(ms, settings);
                if (this.Format == "rss")
                {
                    Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
                    rssFormatter.WriteTo(writer);
                }
                else
                {
                    Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(feed);
                    atomFormatter.WriteTo(writer);
                }
                writer.Flush();

                ms.Position = 0;

                return(this.Content(Encoding.UTF8.GetString(ms.ToArray()), "application/xml"));
            }
            catch (Exception ex)
            {
                Response.Write(HtmlUtils.DisplayMemo("Error: " + ex.GetBaseException().Message + "\r\n" + ex.StackTrace));
                Response.End();
                return(null);
            }

            return(null);
        }
        /// <summary>
        /// Renders an Html table for Class members showing
        /// </summary>
        public RawString ClassMemberTableHtml(
            string tableAttributes  = null,
            string memberLabel      = "Member",
            string descriptionLabel = "Description"
            )
        {
            var childTopics = Topic.Topics.Where(t => GenericUtils.Inlist(t.DisplayType, "classproperty", "classmethod",
                                                                          "classevent", "classfield", "classconstructor"));


            StringBuilder sb = new StringBuilder();

            sb.Append($@"
<table class='detailtable' {tableAttributes}>
<tr><th colspan='2'>{memberLabel}</th><th>{descriptionLabel}</th></tr>
            ");

            bool alternate = false;

            foreach (var childTopic in childTopics)
            {
                sb.AppendLine("<tr" + (alternate ? " class='alternaterow'>" : ">"));

                string icon = childTopic.DisplayType;
                if (childTopic.ClassInfo.Scope == "protected")
                {
                    icon += "protected";
                }

                sb.AppendLine($"\t<td class='col-icon'><img src='icons/{icon}.png' />");
                if (childTopic.ClassInfo.Static)
                {
                    sb.Append("<img src='bmp/static.gif'/>");
                }
                sb.AppendLine("\t</td>");

                string link = childTopic.GetTopicLink(HtmlUtils.HtmlEncode(childTopic.Title));
                sb.AppendLine($"\t<td>{link}</td>");
                sb.AppendLine($"\t<td class='col-detail'>{HtmlUtils.HtmlAbstract(childTopic.Body, 200)}");

                if (childTopic.DisplayType == "classmethod")
                {
                    sb.AppendLine($"\t\t<div><small><b>{childTopic.ClassInfo.Syntax}</b></small></div>");

                    // check for overloads
                    var overloads =
                        childTopic.Topics
                        .Where(t => t.ClassInfo.MemberName == childTopic.ClassInfo.MemberName &&
                               t.Id != childTopic.Id);
                    foreach (var overload in overloads)
                    {
                        sb.AppendLine($"\t\t<div class='syntaxoverloads'>{overload.ClassInfo.Syntax}</div>");
                    }
                }
                sb.AppendLine("\t</td>");
                sb.AppendLine("</tr>");

                alternate = !alternate;
            }
            sb.AppendLine("</table>");


            return(new RawString(sb.ToString()));
        }
        /// <summary>
        /// Display a list of child topics
        /// </summary>
        public RawString ChildTopicsList(string topicTypesList)
        {
            StringBuilder sb = new StringBuilder();

            var             topicTypes = topicTypesList.Split();
            List <DocTopic> childTopics;

            if (topicTypes.Length > 0)
            {
                childTopics = Topic.Topics.Where(t => GenericUtils.Inlist <string>(t.DisplayType, topicTypes)).ToList();
            }
            else
            {
                childTopics = Topic.Topics.ToList();
            }

            if (childTopics.Count < 1)
            {
                return(new RawString());
            }

            sb.AppendLine("<ul style='list-style-type:none'>");

            foreach (var childTopic in childTopics)
            {
                sb.AppendLine($@"<li><img src='icons/{childTopic.DisplayType}.png' /> {HtmlUtils.HtmlEncode(childTopic.Title)}</li>");
            }
            sb.AppendLine("</ul>");

            return(new RawString(sb.ToString()));
        }