public GhostExportDocument(GhostExportDocumentData data, GhostExportDocumentMetadata metaData) { this.Data = data; this.Metadata = metaData; }
private static void ProcessFile(string path, string outputPath) { // guard the path, that it exists if (!System.IO.File.Exists(path)) { WriteOutput($"File {path} does not exist, or you do not have permissions to write to.", LoggingLevels.Error); } Console.WriteLine($"Processing file on {path}"); // load the document XDocument doc; try { doc = XDocument.Load(path); } catch (Exception e) { WriteOutput(e.Message, LoggingLevels.Error); return; } var channelNode = doc.Descendants().SingleOrDefault(x => x.Name.LocalName == "channel"); if (channelNode == null) { WriteOutput("The file is missing a channel node.", LoggingLevels.Error); } // build categories and tags tree // and this needs to be special, as some tags are self-referencing... of course var elements = channelNode.Elements() .Where(x => x.Name.LocalName == "category").ToArray(); var categories = new Dictionary <string, GhostTag>(); foreach (var element in elements) { var tag = Process <GhostTag>(element, (node, item) => GhostTagVisitor.Visit(node, item, categories)); categories.Add(tag.Slug, tag); } elements = channelNode.Elements() .Where(x => x.Name.LocalName == "tag").ToArray(); var tags = new Dictionary <string, GhostTag>(); foreach (var element in elements) { var tag = Process <GhostTag>(element, (node, item) => GhostTagVisitor.Visit(node, item, tags)); tags.Add(tag.Slug, tag); } var attachments = channelNode.Elements() .Where(x => x.Name.LocalName == "item" && x.Elements().Single(p => p.Name.LocalName == "post_type").Value == "attachment") .Select(p => Process <WordpressAttachment>(p, (node, item) => WordpressAttachmentVisitor.Visit(node, item))) .ToDictionary(x => x.Id, x => x); // TODO: this is horrible, but it'll have to do for now FileContext.Current.Attachments = attachments; var posts = channelNode.Elements() .Where(x => x.Name.LocalName == "item" && x.Descendants().Single(p => p.Name.LocalName == "post_type").Value == "post") .Select(p => Process <GhostPost>(p, (node, item) => GhostPostVisitor.Process(node, item))) .ToDictionary(x => x.Id, x => x); // build the final post foreach (var post in posts.Values) { post.BuildLinks(attachments, categories, tags); // transform the image url post.Image = TransformUrl(post.Image); } // build the document var data = new GhostExportDocumentData { Posts = posts.Values.ToList(), Categories = categories.Values.Where(x => x.PostsUsingTag.Count > 0).ToList() }; var document = new GhostExportDocument(data, new GhostExportDocumentMetadata()); var jsonOutput = JsonConvert.SerializeObject(document); // write it as a simple UTF8 file, without BOM File.WriteAllText(outputPath, jsonOutput, new UTF8Encoding(false)); //Console.WriteLine(JsonConvert.SerializeObject(document)); }