public Element CreateListingSnippet(Element after) { int newOrderNr = 1; var ns = new ListingSnippet { Name = "Listing", Content = "public class Code {\n//Code goes here...\n}\n".GetBytes() }; ns.Parent = GetSection(after); if (after is TextSnippet || after is ListingSnippet) { newOrderNr = after.OrderNr + 1; // inserting always after so new element gets the position of current element } // else: keep 1 as default, as if not invoked from another textsnippet assume we're inserting after section this.ReorderLeafElementsAfterElement(ns, newOrderNr); return(ns); }
private static void RestoreOpusFromFile(Opus opus, XDocument xDoc, string userName) { var saveElements = new List <Element>(); Func <IEnumerable <XElement>, Element, List <Element> > helper = null; var currentChapter = opus.Name; var chapterOrder = 1; helper = (nodes, parent) => { var ret = new List <Element>(); var orderNr = 1; foreach (var elm in nodes) { Element newElm = null; # region Detect Element Type switch (elm.Attribute("Type").Value.ToLower()) { case "opus": # region OPUS // do nothing as this import runs on opus level already, simply assign current as start opus.Name = elm.Attribute("Name").NullSafeString(); ((Opus)opus).Version = ((Opus)opus).Version + 1; break; # endregion case "section": # region SECTION if (elm.FirstNode != null && elm.FirstNode.NodeType == System.Xml.XmlNodeType.Text) { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes(((XText)elm.FirstNode).Value.Trim()) }; } else { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes("Empty Section") }; } if (elm.Attribute("Name") == null || String.IsNullOrEmpty(elm.Attribute("Name").Value)) { newElm.Name = System.Text.Encoding.UTF8.GetString(newElm.Content); } else { newElm.Name = elm.Attribute("Name").Value; if (elm.FirstNode == null) { newElm.Content = System.Text.Encoding.UTF8.GetBytes(elm.Attribute("Name").Value); } } // only if import has an opus/parent part if (elm.Parent != null && elm.Parent.Name == "Content") { newElm.Parent = opus; currentChapter = elm.Attribute("Name").NullSafeString(); } currentChapter = currentChapter ?? "Import Files"; break; # endregion case "text": # region TEXT newElm = new TextSnippet { Content = Encoding.UTF8.GetBytes(elm.GetInnerXml()), Name = elm.Value.CleanUpString(15) }; break; # endregion case "image": # region IMAGE var imgType = "png"; var error = false; byte[] content = null; switch (elm.Attribute("Method").NullSafeString()) { case "Base64": // assume the image is stored internally as Base64 try { content = Convert.FromBase64String(elm.Value.Trim()); } catch (Exception ex) { error = true; } break; default: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error in Image: {0}", elm.ToString()); Console.ForegroundColor = ConsoleColor.Gray; break; } if (error) { break; } // for this test console we don't save the images, add debug info here if needed break; # endregion case "listing": # region LISTING newElm = new ListingSnippet { Content = Encoding.UTF8.GetBytes(elm.Value.Trim()), //.Replace("\n", " "); - Causes problems with Listing widget. All data is displayed in one line Name = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Title = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Language = elm.Attribute("Language") == null ? "" : elm.Attribute("Language").Value, SyntaxHighlight = elm.Attribute("Highlight") == null || Boolean.Parse(elm.Attribute("Highlight").Value), LineNumbers = elm.Attribute("LineNumbers") == null || Boolean.Parse(elm.Attribute("LineNumbers").Value) }; # endregion break;
protected override void Seed(EditorContext context) { base.Seed(context); #region Author Portal Console.WriteLine("DemoContent"); #region create a sample Func <IEnumerable <XElement>, List <Element> > helper = null; var currentChapter = String.Empty; var chapterOrder = 1; Document opus = null; helper = nodes => { var ret = new List <Element>(); int orderNr = 1; foreach (var elm in nodes) { Element newElm = null; # region Detect Element Type switch (elm.Attribute("Type").Value.ToLower()) { case "opus": # region OPUS // create opus newElm = new Document { Name = elm.Attribute("Name").Value }; opus = (Document)newElm; // this is an easy way to implement a workflow break; # endregion case "section": # region SECTION if (elm.FirstNode != null && elm.FirstNode.NodeType == System.Xml.XmlNodeType.Text) { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes(((XText)elm.FirstNode).Value.Trim()) }; } else { newElm = new Section { Content = System.Text.Encoding.UTF8.GetBytes("Empty Section") }; } if (elm.Attribute("Name") == null || String.IsNullOrEmpty(elm.Attribute("Name").Value)) { newElm.Name = System.Text.Encoding.UTF8.GetString(newElm.Content); } else { newElm.Name = elm.Attribute("Name").Value; if (elm.FirstNode == null) { newElm.Content = System.Text.Encoding.UTF8.GetBytes(elm.Attribute("Name").Value); } } // Detect Chapter Elements to store resources in subfolders if (elm.Parent.Attribute("Type").Value == "Opus") { currentChapter = elm.Attribute("Name").Value; } break; # endregion case "text": # region TEXT newElm = new TextSnippet { Content = System.Text.Encoding.UTF8.GetBytes(elm.GetInnerXml()), Name = elm.Value.CleanUpString(15) }; break; # endregion case "image": # region IMAGE var imgpath = elm.Value.Trim(); Debug.Assert(Path.GetExtension(imgpath).Length > 0, "no extension " + imgpath); // get and optionally create folder after chapter // var res = new Resource { Name = elm.Attribute("Name").Value, OwnerDocument = opus, TypesOfResource = TypeOfResource.Content, MimeType = "image/" + Path.GetExtension(imgpath).Substring(1) // kick the leading "." }; System.Drawing.Image img = null; var localPath = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location), "DemoContent", imgpath); if (File.Exists(localPath)) { var bytes = File.ReadAllBytes(localPath); res.Content = bytes; } context.Resources.Add(res); newElm = new ImageSnippet { Content = res.Content, Name = res.Name, Title = res.Name, MimeType = res.MimeType }; var imgprops = new ImageProperties(); if (elm.Attribute("Width") == null || elm.Attribute("Height") == null) { if (img != null) { imgprops.ImageWidth = imgprops.OriginalWidth = img.Width; imgprops.ImageHeight = imgprops.OriginalHeight = img.Height; } else { imgprops.ImageWidth = imgprops.OriginalWidth = 100; imgprops.ImageHeight = imgprops.OriginalHeight = 100; } } else { imgprops.ImageWidth = imgprops.OriginalWidth = Convert.ToInt32(elm.Attribute("Width").GetNullSafeValue()); imgprops.ImageHeight = imgprops.OriginalHeight = Convert.ToInt32(elm.Attribute("Height").GetNullSafeValue()); } ((ImageSnippet)newElm).Properties = new JavaScriptSerializer().Serialize(imgprops); break; # endregion case "listing": # region LISTING newElm = new ListingSnippet { Content = System.Text.UTF8Encoding.UTF8.GetBytes(elm.Value.Trim()), //.Replace("\n", " "); - Causes problems with Listing widget. All data is displayed in one line Name = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Title = elm.Attribute("Name") == null ? "Listing" : elm.Attribute("Name").Value, Language = elm.Attribute("Language") == null ? "" : elm.Attribute("Language").Value, SyntaxHighlight = elm.Attribute("Highlight") == null ? true : Boolean.Parse(elm.Attribute("Highlight").Value), LineNumbers = elm.Attribute("LineNumbers") == null ? true : Boolean.Parse(elm.Attribute("LineNumbers").Value) }; # endregion break;