Ejemplo n.º 1
0
        public BlogIndex Parse(DirectoryPath path)
        {
            var directory = _fileSystem.GetDirectory(path);

            if (!directory.Exists)
            {
                return(new BlogIndex(Enumerable.Empty <BlogPost>()));
            }

            var posts = new List <BlogPost>();
            var files = directory.GetFiles("*", SearchScope.Current);

            foreach (var file in files)
            {
                var filename = BlogFilename.Parse(file.Path);
                if (filename != null)
                {
                    // Read the file.
                    var content = _parser.Parse(file.Path);
                    if (!content.FrontMatter.ContainsKey("content-type"))
                    {
                        content.FrontMatter.Add("content-type", "markdown");
                    }

                    // Process the content.
                    var body = _processor.PreProcess(content.Body);
                    body = _converter.ConvertToHtml(content, body);
                    body = _processor.PostProcess(body) ?? body;

                    // Get the excerpts.
                    var excerpts = _converter.ConvertToHtml(content, content.Excerpt);

                    // Rewrite the feed body.
                    // This is kind of a hack, but we need to make sure that all links are absolute.
                    var feedBody = RewriteRelativeLinks(body);

                    var author = content.GetFrontMatter("author");

                    // Add the blog post.
                    posts.Add(new BlogPost(filename.Slug,
                                           content.GetFrontMatter("title"), body, feedBody, excerpts, filename.PostedAt,
                                           GetCategories(content), author == null ? "Cake Team" : author));
                }
            }

            // Hack. Don't judge me... :)
            var maxDate  = posts.Max(x => x.PostedAt);
            var lastPost = posts.FirstOrDefault(x => x.PostedAt == maxDate);

            if (lastPost != null)
            {
                lastPost.IsLatest = true;
            }

            return(new BlogIndex(posts));
        }
Ejemplo n.º 2
0
        private Topic ReadTopic(XmlReader reader, DirectoryPath root)
        {
            var file = reader.GetAttribute("file");
            var url  = reader.GetAttribute("importurl");

            if (string.IsNullOrWhiteSpace(file) &&
                string.IsNullOrWhiteSpace(url))
            {
                return(null);
            }

            var id     = reader.GetAttribute("id");
            var title  = reader.GetAttribute("title");
            var hidden = string.Equals("true", reader.GetAttribute("hidden"), StringComparison.OrdinalIgnoreCase);

            if (string.IsNullOrWhiteSpace(id))
            {
                if (!string.IsNullOrWhiteSpace(url))
                {
                    throw new InvalidOperationException("A remote document require a local ID.");
                }
                if (!string.IsNullOrWhiteSpace(file))
                {
                    id = Path.GetFileNameWithoutExtension(file);
                }
                else if (!string.IsNullOrWhiteSpace(title))
                {
                    id = title.ToSlug();
                }
                else
                {
                    throw new InvalidOperationException("Could not generate ID from topic.");
                }
            }

            var body = ReadBody(root, file, url) ?? string.Empty;

            if (!string.IsNullOrWhiteSpace(body))
            {
                // Read the file and separate front matter from content.
                var content = _contentParser.ParseString(body);
                if (content != null)
                {
                    body = _contentProcessor.PreProcess(content.Body);
                    body = _contentConverter.ConvertToHtml(content, body);
                }

                // Process the content.
                body = _contentProcessor.PostProcess(body) ?? body;
            }

            return(new Topic(id, title, body, hidden, file, url));
        }
Ejemplo n.º 3
0
        private Topic ReadTopic(XmlReader reader, DirectoryPath root)
        {
            var file = reader.GetAttribute("file");

            if (string.IsNullOrWhiteSpace(file))
            {
                return(null);
            }

            var id    = reader.GetAttribute("id");
            var title = reader.GetAttribute("title");
            var body  = string.Empty;

            if (string.IsNullOrWhiteSpace(id))
            {
                if (!string.IsNullOrWhiteSpace(file))
                {
                    id = Path.GetFileNameWithoutExtension(file);
                }
                else if (!string.IsNullOrWhiteSpace(title))
                {
                    id = title.ToSlug();
                }
                else
                {
                    throw new InvalidOperationException("Could not generate ID from topic.");
                }
            }

            var path = root.CombineWithFilePath(file);

            if (_fileSystem.Exist(path))
            {
                // Read the file and separate front matter from content.
                var content = _contentParser.Parse(path);
                if (content != null)
                {
                    body = _contentProcessor.PreProcess(content.Body);
                    body = _contentConverter.ConvertToHtml(content, body);
                }

                // Process the content.
                body = _contentProcessor.PostProcess(body) ?? body;
            }

            return(new Topic(id, title, body, file));
        }