Example #1
0
        private static void ProcessTags(GhostFormat data, int postId, post post)
        {
            if (data.data.tags is null)
            {
                data.data.tags = new List <Tag>();
            }

            if (data.data.posts_tags is null)
            {
                data.data.posts_tags = new List <Posts_Tags>();
            }

            // Extract the categories
            foreach (var category in post.categories)
            {
                // find category in ghost data
                var tagId = GetOrCreateTag(data, category);

                // Create tag/post mapping

                if (data.data.posts_tags == null)
                {
                    data.data.posts_tags = new List <Posts_Tags>();
                }

                data.data.posts_tags.Add(new Posts_Tags()
                {
                    post_id = postId,
                    tag_id  = tagId
                });
            }
        }
Example #2
0
        public static GhostFormat BuildGhostObject(List<post> postList, Options options)
        {
            var data = new GhostFormat();

            data.meta = BuildMeta();
            data.data = new Data();

            ParsePosts(data, postList, options);

            return data;
        }
Example #3
0
        public static GhostFormat BuildGhostObject(List <post> postList, Options options)
        {
            var data = new GhostFormat
            {
                meta = BuildMeta(),
                data = new Data()
            };

            ParsePosts(data, postList, options);

            return(data);
        }
Example #4
0
        private static void ParsePosts(GhostFormat data, List <post> postList, Options options)
        {
            var id = 1;

            foreach (var post in postList.OrderBy(p => p.pubDate))
            {
                if (data.data.posts is null)
                {
                    data.data.posts = new List <Post>();
                }

                post.content = SanitiseImagesAndLinks(post.content, options);

                var ghostPost = new Post
                {
                    id               = id,
                    title            = post.title,
                    slug             = post.slug,
                    html             = post.content,
                    markdown         = ConvertHtmlToMarkdown(post.content),
                    featured         = 0,
                    page             = 0,
                    status           = post.ispublished ? "published" : "draft",
                    language         = "en_GB",
                    meta_title       = null,
                    meta_description = null,
                    author_id        = 1, // Default to 1
                    created_at       = GetUnixTime(post.lastModified),
                    created_by       = 1, // Default to 1
                    updated_at       = GetUnixTime(post.lastModified),
                    updated_by       = 1, // Default to 1
                    published_at     = GetUnixTime(post.pubDate),
                    published_by     = 1  // Default to 1
                };

                data.data.posts.Add(ghostPost);

                // Look at tags
                ProcessTags(data, id, post);

                id++;
            }
        }
Example #5
0
        private static int GetOrCreateTag(GhostFormat data, string category)
        {
            var existingTag = data.data.tags.FirstOrDefault(t => t.name.Equals(category, StringComparison.InvariantCultureIgnoreCase));

            if (existingTag is null)
            {
                // Create a new tag
                var tag = new Tag
                {
                    id          = (data.data.tags.Count > 0) ? data.data.tags.Max(t => t.id) + 1 : 1,
                    name        = category,
                    slug        = GenerateSlug(category),
                    description = ""
                };

                data.data.tags.Add(tag);
                existingTag = tag;
            }

            return(existingTag.id);
        }
Example #6
0
        private static void ProcessTags(GhostFormat data, int postId, post post)
        {
            if (data.data.tags == null)
            {
                data.data.tags = new List<Tag>();
            }

            if (data.data.posts_tags == null)
            {
                data.data.posts_tags = new List<Posts_Tags>();
            }

            // Extract the categories
            foreach (var category in post.categories)
            {
                // find category in ghost data
                var tagId = GetOrCreateTag(data, category);

                // Create tag/post mapping

                if (data.data.posts_tags == null)
                {
                    data.data.posts_tags = new List<Posts_Tags>();
                }

                data.data.posts_tags.Add(new Posts_Tags()
                {
                    post_id = postId,
                    tag_id = tagId
                });

            }
        }
Example #7
0
        private static void ParsePosts(GhostFormat data, List<post> postList, Options options)
        {
            int id = 1;
            foreach (var post in postList.OrderBy(p => p.pubDate))
            {
                if (data.data.posts == null)
                {
                    data.data.posts = new List<Post>();
                }

                post.content = SanitiseImagesAndLinks(post.content, options);

                var ghostPost = new Post();
                ghostPost.id = id;
                ghostPost.title = post.title;
                ghostPost.slug = post.slug;
                ghostPost.html = post.content;
                ghostPost.markdown = ConvertHtmlToMarkdown(post.content);
                ghostPost.featured = 0;
                ghostPost.page = 0;
                ghostPost.status = (post.ispublished) ? "published" : "draft";
                ghostPost.language = "en_GB";
                ghostPost.meta_title = null;
                ghostPost.meta_description = null;
                ghostPost.author_id = 1;  // Default to 1
                ghostPost.created_at = GetUnixTime(post.lastModified);
                ghostPost.created_by = 1;  // Default to 1
                ghostPost.updated_at = GetUnixTime(post.lastModified);
                ghostPost.updated_by = 1;  // Default to 1
                ghostPost.published_at = GetUnixTime(post.pubDate);
                ghostPost.published_by = 1;  // Default to 1

                data.data.posts.Add(ghostPost);

                // Look at tags
                ProcessTags(data, id, post);

                id++;
            }
        }
Example #8
0
        private static int GetOrCreateTag(GhostFormat data, string category)
        {
            var existingTag = data.data.tags.FirstOrDefault(t => t.name.Equals(category, StringComparison.InvariantCultureIgnoreCase));

            if (existingTag == null)
            {
                // Create a new tag
                var tag = new Tag();
                tag.id = (data.data.tags.Count > 0) ? data.data.tags.Max(t => t.id) + 1 : 1;
                tag.name = category;
                tag.slug = GenerateSlug(category);
                tag.description = "";

                data.data.tags.Add(tag);
                existingTag = tag;
            }

            return existingTag.id;
        }
Example #9
0
 //private static void CreateZipFileWithImages(string inputDirectory, string outputPath, string tempFolder)
 //{
 //    Log("Copying Images Zip file...");
 //    var imagePath = Path.Combine(tempFolder, "images");
 //    Directory.CreateDirectory(imagePath);
 //    CopyAll(new DirectoryInfo(Path.Combine(inputDirectory, "files")), new DirectoryInfo(imagePath));
 //    var outputFilename = Path.Combine(outputPath, "output.zip");
 //    if (File.Exists(outputFilename))
 //    {
 //        File.Delete(outputFilename);
 //    }
 //    ZipFile.CreateFromDirectory(tempFolder, outputFilename);
 //    //foreach (var filename in GetFiles(Path.Combine(inputDirectory, "files"),  "*.png|*.gif|*.jpg", SearchOption.AllDirectories))
 //    //{
 //    //    // Copy file to output path
 //    //    File.Copy(filename, imagePath);
 //    //    File
 //    //}
 //}
 private static void SaveJsonFile(GhostFormat buildGhostObject, string outputFolder)
 {
     MemoryStream stream1 = new MemoryStream();
     var serialiser = new DataContractJsonSerializer(typeof (GhostFormat));
     serialiser.WriteObject(stream1, buildGhostObject);
     stream1.Position = 0;
     using (var filestream = File.Create(Path.Combine(outputFolder, "output.json")))
     {
         stream1.CopyTo(filestream);
     }
 }