Example #1
0
        private string CreateHtmlInnerRecursively(IEnumerable <Snippet> snippets, IDictionary <string, NumberingSchema> numbering)
        {
            var flatContent          = new StringBuilder();
            Action <Snippet> convert = null;

            convert = e =>
            {
                var builder = e.GetType().GetCustomAttributes(typeof(SnippetElementAttribute), true).OfType <SnippetElementAttribute>().Single();
                if (builder.CreateResource)
                {
                    string itemHref = String.Empty;
                    // BaseType because it's a proxy element; if database context changes behavior, change this
                    switch (e.GetType().BaseType.Name)
                    {
                    case "ImageSnippet":
                        // pull resource from BLOB storage
                        if (e.Content != null)
                        {
                            itemHref = ((ImageSnippet)e).ItemHref;
                            var properties = System.Web.Helpers.Json.Decode <ImageProperties>(((ImageSnippet)e).Properties);
                            var image      = ImageUtil.ApplyImageProperties(e.Content, properties);
                            if (image != null)
                            {
                                using (var ms = new MemoryStream())
                                {
                                    image.Save(ms, ImageFormat.Png);
                                    byte[] bytes = ms.ToArray();
                                    ((ImageSnippet)e).ItemHref = CreateImage(this, new CreateImageArguments {
                                        Content = bytes, FileName = itemHref
                                    });
                                }
                            }
                            else
                            {
                                // create a placeholder image
                                var fs = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/bullet.png"));
                                ((ImageSnippet)e).ItemHref = CreateImage(this, new CreateImageArguments {
                                    Content = fs, FileName = itemHref
                                });
                            }
                        }
                        break;
                    }
                    flatContent.Append(builder.BuildHtml(e, numbering));
                }
                else
                {
                    flatContent.Append(builder.BuildHtml(e, numbering));
                    // if resource do not add again
                    if (e.HasChildren())
                    {
                        e.Children.OfType <Snippet>().ToList().ForEach(c => convert(c));
                    }
                }
            };
            snippets.ToList().ForEach(c => convert(c));
            return(flatContent.ToString());
        }