Пример #1
0
        public void AddPost(Post post)
        {
            // Get any associated collections for this Post.
            List<string> collectionsToAmend = new List<string>();
            List<string> collectionsToCreate = new List<string>();

            switch (this.Type)
            {
                case CollectionType.Archives:

                    string collectionsReference = Directory.GetFiles(this.AbsoluteFolderPath, post.DateCreated.ToString("yyyy-MM.json")).FirstOrDefault();

                    // If we have a reference,
                    if (!string.IsNullOrEmpty(collectionsReference))
                    {
                        collectionsToAmend.Add(collectionsReference);
                    }
                    else
                    {
                        collectionsToCreate.Add(post.DateCreated.ToString("yyyy-MM"));
                    }

                    break;

                case CollectionType.Categories:

                    foreach (string category in post.Categories)
                    {
                        string collectionReference = Directory.GetFiles(this.AbsoluteFolderPath, category + ".json").FirstOrDefault();

                        if (!string.IsNullOrEmpty(collectionReference))
                        {
                            collectionsToAmend.Add(collectionReference);
                        }
                        else
                        {
                            collectionsToCreate.Add(category);
                        }
                    }

                    break;
                case CollectionType.Tags:

                    foreach (string tag in post.Tags)
                    {
                        string collectionReference = Directory.GetFiles(this.AbsoluteFolderPath, tag + ".json").FirstOrDefault();

                        if (!string.IsNullOrEmpty(collectionReference))
                        {
                            collectionsToAmend.Add(collectionReference);
                        }
                        else
                        {
                            collectionsToCreate.Add(tag);
                        }
                    }

                    break;
            }

            // Amend any collections that need amending.
            foreach (string collectionReference in collectionsToAmend)
            {
                Collection collection = new Collection();

                collection = this.Get(collectionReference);
                collection.References.Add(post.CompleteFilePath);

                this.Save(collection);
            }

            // Create any collections that need creating.
            foreach (string collectionName in collectionsToCreate)
            {
                Collection collection = new Collection();

                switch (this.Type)
                {
                    case CollectionType.Archives:
                        collection.Type = "archive";
                        break;
                    case CollectionType.Tags:
                        collection.Type = "tag";
                        break;
                    case CollectionType.Categories:
                        collection.Type = "category";
                        break;
                }

                collection.Name = collectionName;
                collection.References.Add(post.CompleteFilePath);

                this.Save(collection);
            }
        }
Пример #2
0
        /// <summary>
        /// Parses the given human-readable Input file to a Post object.
        /// </summary>
        /// <param name="reference"></param>
        /// <returns></returns>
        internal IContent ParseInputFile(string reference)
        {
            IContent content = null;

            switch (this._ContentType)
            {
                case ContentType.Post:
                    content = new Post();
                    break;
                case ContentType.Page:
                    content = new Page();
                    break;
            }

            string rawText = Utilities.RawFileContents(reference);

            // ------------------------------------------------------------------------------------------
            // Generate a new Post object from the contents of the Input file.
            // ------------------------------------------------------------------------------------------

            bool processingMeta = false;
            bool processingContent = false;

            // Iterate over each line of the file.
            foreach (string line in rawText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None))
            {
                // Work out what's going on.
                if (line == "---" && processingMeta && !processingContent) { processingMeta = false; processingContent = true; }
                if (line == "---" && !processingMeta && !processingContent) { processingMeta = true; }

                // Now do the processing.
                if (processingMeta)
                {
                    if (line != "---")
                    {
                        switch (this._ContentType)
                        {
                            case Managers.ContentType.Post:
                                this.ProcessMeta(line, (Post)content);
                                break;
                            case Managers.ContentType.Page:
                                this.ProcessMeta(line, (Page)content);
                                break;
                        }
                    }

                }
                else if (processingContent && line != "---")
                {
                    content.Content += line + Environment.NewLine;
                }
            }

            // ------------------------------------------------------------------------------------------
            // Carry out any other processing of the Post that may be required.
            // ------------------------------------------------------------------------------------------

            Markdown contentMarkdown = new Markdown();
            content.Content = contentMarkdown.Transform(content.Content);

            return content;
        }
Пример #3
0
        /// <summary>
        /// Carries out necessary preprocessing of a post such as adding to the archive, collating tags etc.
        /// </summary>
        /// <param name="post"></param>
        internal void Preprocess(Post post)
        {
            // Add to archive (i.e. by Date).
            CollectionsManager collectionsManager = new CollectionsManager(CollectionType.Archives);

            collectionsManager.AddPost(post);

            // Any tags? Update them.
            if (post.Tags != null)
            {
                collectionsManager = new CollectionsManager(CollectionType.Tags);

                collectionsManager.AddPost(post);
            }

            // Any categories? Add this post to them.
            if (post.Categories != null)
            {
                collectionsManager = new CollectionsManager(CollectionType.Categories);

                collectionsManager.AddPost(post);
            }

            // Flag the post as being processed and write to disk.
            post.IsProcessed = true;

            string postJson = JsonConvert.SerializeObject(post);
            File.WriteAllText(post.CompleteFilePath, postJson);
        }
Пример #4
0
        /// <summary>
        /// Processes the meta information specific to a Post.
        /// </summary>
        /// <param name="line"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        private Post ProcessMeta(string line, Post post)
        {
            if (line.StartsWith("title: "))
            {
                post.Title = line.Replace("title: ", "");
            }

            if (line.StartsWith("description: "))
            {
                post.Description = line.Replace("description: ", "");
            }

            if (line.StartsWith("author: "))
            {
                post.Author = line.Replace("author: ", "");
            }

            if (line.StartsWith("date: "))
            {
                post.DateCreated = Convert.ToDateTime(line.Replace("date: ", ""));
            }

            if (line.StartsWith("tags: "))
            {
                string tagCsv = line.Replace("tags: ", "");

                post.Tags = tagCsv.Split(',').ToList<string>();
            }

            if (line.StartsWith("categories: "))
            {
                string tagCsv = line.Replace("categories: ", "");

                post.Tags = tagCsv.Split(',').ToList<string>();
            }

            return post;
        }
Пример #5
0
        /// <summary>
        /// Parses the given JSON file into a Post object.
        /// </summary>
        /// <param name="reference"></param>
        /// <returns></returns>
        internal IContent ParseJsonFile(string reference)
        {
            IContent content = null;

            switch (this._ContentType)
            {
                case Managers.ContentType.Page:
                    content = new Page();
                    break;
                case Managers.ContentType.Post:
                    content = new Post();
                    break;
            }

            // Load the post if we have a reference.
            if (!string.IsNullOrEmpty(reference))
            {
                // Grab the raw data.
                string postJson = Utilities.RawFileContents(reference);

                // Deserialise the data.
                switch (this._ContentType)
                {
                    case Managers.ContentType.Page:
                        content = Utilities.DeserializeJson<Page>(postJson);
                        break;
                    case Managers.ContentType.Post:
                        content = Utilities.DeserializeJson<Post>(postJson);
                        break;
                }

                content.CompleteFilePath = reference;

                // Process the file if necessary.
                if (!content.IsProcessed)
                {
                    switch (this._ContentType)
                    {
                        case Managers.ContentType.Page:
                            this.Preprocess((Page)content);
                            break;
                        case Managers.ContentType.Post:
                            this.Preprocess((Post)content);
                            break;
                    }
                }
            }

            return content;
        }