コード例 #1
0
ファイル: Post.cs プロジェクト: alkampfergit/ShuppanButsu
 private void Apply(PostCreated @event)
 {
     content = @event.Content;
     title = @event.Title;
     slugCode = @event.SlugCode;
     blogName = @event.BlogName;
 }
コード例 #2
0
        public void CreatePostHtmlDocument(PostCreated evt)
        {
            HtmlDocument templateDocument = new HtmlDocument();
            String BlogDirectory = evt.BlogName;

            if (!String.IsNullOrEmpty(BlogDirectory) && !Directory.Exists(BlogDirectory)) Directory.CreateDirectory(BlogDirectory);
            String templateDirectory = Path.Combine(Configuration.TemplateDirectory, BlogDirectory);
            if (!String.IsNullOrEmpty(BlogDirectory) && !Directory.Exists(templateDirectory))
            {
                //Specific template for this blog does not exists, simple use the standard template
                templateDirectory = Configuration.TemplateDirectory;
            }

            String baseDirectory = Path.Combine(BlogDirectory, Configuration.BaseGenerationDirectory);
            if (!Directory.Exists(baseDirectory))
            {

                Directory.CreateDirectory(baseDirectory);
            }
            //a post is really simpler than the home page, it gets simply recreated from a template with simple substitution
            String postFileName = Path.Combine(baseDirectory, evt.SlugCode) + ".html";
            if (File.Exists(postFileName))
            {
                //oops there is a problem, the file already exists,
                throw new NotImplementedException("We need to implement the logic for post file that already exists");
            }

            var templateFileName = new FileInfo(Path.Combine(templateDirectory, "post.html"));
            if (!templateFileName.Exists)
            {
                Logger.Fatal("Template file missing: " + templateFileName.FullName);
                return;
            }

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(File.ReadAllText(templateFileName.FullName));
            if (!ReplaceNode(doc, "{title}", evt.Title, templateFileName.FullName)) return;
            if (!ReplaceNode(doc, "{content}", evt.Content, templateFileName.FullName)) return;

            doc.Save(postFileName);
        }
コード例 #3
0
ファイル: Post.cs プロジェクト: alkampfergit/ShuppanButsu
 /// <summary>
 /// Factory method to create a post
 /// </summary>
 /// <param name="factory">The factory to be used for AggregateRootCreation</param>
 /// <param name="title"></param>
 /// <param name="textContent"></param>
 /// <returns></returns>
 public static Post CreatePost(
     AggregateRootFactory factory,
     String title,
     String textContent,
     String excerpt,
     String blogName)
 {
     //Slug is created replacing any non number or letter char with a dash
     //accents are removed
     String slug = title.Slugify();
     if (String.IsNullOrEmpty(excerpt))
     {
         //Need to calculate the excerpt of the post, in this version just take some text from the
         //body of the post.
         HtmlDocument doc = new HtmlDocument();
         doc.LoadHtml(textContent);
         excerpt = doc.DocumentNode.InnerText;
         if (excerpt.Length > 200) excerpt = excerpt.Substring(0, 200) + "...";
     }
     var evt = new PostCreated(title, textContent, slug, blogName, excerpt);
     return factory.Create<Post>(evt);
 }
コード例 #4
0
        public void PostCreatedHandler(PostCreated evt)
        {
            //Verify if we need to add this post to the list.
            if (postsInHomePage.Count >= Configuration.NumberOfPostsInHomePage &&
                evt.Timestamp < postsInHomePage.Min(e => e.Timestamp))
            {
                //already reached maximum post number in home page, this post is not older than the other
                //nothing to add in the home page.
                Logger.Info("New post is older than maximum number of post to be stored in home page, it will be ignored");
                return;
            }

            HtmlDocument templateDocument = new HtmlDocument();
            String BlogDirectory = Path.Combine(Configuration.BaseGenerationDirectory, evt.BlogName);

            if (!String.IsNullOrEmpty(BlogDirectory) && !Directory.Exists(BlogDirectory))
            {
                if (Logger.IsDebugEnabled) Logger.Debug("Creating output folder for pages " + BlogDirectory);
                Directory.CreateDirectory(BlogDirectory);
                if (Logger.IsDebugEnabled) Logger.Debug("Created output folder for pages " + BlogDirectory);
            }
            String templateDirectory = Path.Combine(Configuration.TemplateDirectory, BlogDirectory);
            if (!String.IsNullOrEmpty(BlogDirectory) && !Directory.Exists(templateDirectory))
            {
                //Specific template for this blog does not exists, simple use the standard template
                if (Logger.IsDebugEnabled) Logger.Debug("Template directory " + templateDirectory + " does not exist, default directory " + Configuration.TemplateDirectory + " will be used");
                templateDirectory = Configuration.TemplateDirectory;
            }

            String templateFileName = Path.Combine(templateDirectory, "index.html");
            FileInfo finfo = new FileInfo(templateFileName);
            if (!finfo.Exists)
            {
                Logger.Fatal("Template file missing: " + finfo.FullName + " unable to create home page");
                return;
            }

            var destinationFileName = String.IsNullOrEmpty(BlogDirectory) ? "index.html" : Path.Combine(BlogDirectory, "index.html");

            templateDocument.LoadHtml(File.ReadAllText(finfo.FullName));
            //now find the div with the template of post excerpt
            var templateOfExcerpt = templateDocument.DocumentNode.SelectSingleNode("//*[@id='{posttemplate}']");
            if (templateOfExcerpt == null)
            {
                Logger.Fatal("The template in file " + finfo.FullName + " has not {posttemplate} element to use. The template is malformed");
                return;
            }

            //Template seems to be valid
            //If destination file does not exists, copy the raw template
            HtmlDocument destinationDocument = new HtmlDocument();
            if (!File.Exists(destinationFileName))
            {
                PrepareEmptyDocumentFromTemplate(finfo, destinationDocument);
            }
            else
            {
                destinationDocument.LoadHtml(File.ReadAllText(destinationFileName));
            }

            //remove from original document, remove the attribute id
            templateOfExcerpt.Remove();
            templateOfExcerpt.Attributes.Add("id", "postid-" + evt.Id);

            //now we have template, if it is a newPage we can modify this one, if it is not first time we create home page we need to copy.
            var titleNode = templateOfExcerpt.SelectSingleNode(".//*[@id='{title}']");
            if (titleNode == null)
            {
                Logger.Fatal("The template in file " + finfo.FullName + " has not {title} element to use. The template is malformed");
                return;
            }
            titleNode.Attributes["id"].Remove();
            titleNode.RemoveAllChildren();

            titleNode.AppendChild(HtmlTextNode.CreateNode(evt.Title));

            var excerptNode = templateOfExcerpt.SelectSingleNode(".//*[@id='{excerpt}']");
            if (excerptNode == null)
            {
                Logger.Fatal("The template in file " + finfo.FullName + " has not {excerpt} element to use. The template is malformed");
                return;
            }
            excerptNode.Attributes["id"].Remove();
            excerptNode.RemoveAllChildren();
            excerptNode.AppendChild(HtmlTextNode.CreateNode(evt.Excerpt));

            postsInHomePage.Add(evt);

            while (postsInHomePage.Count > Configuration.NumberOfPostsInHomePage)
            {
                var postToRemove = postsInHomePage.OrderBy(e => e.Timestamp).First();
                postsInHomePage.Remove(postToRemove);
                var docFragmentToRemove = destinationDocument.DocumentNode.SelectSingleNode("//*[@id='postid-" + postToRemove.Id + "']");
                if (docFragmentToRemove != null)
                {
                    docFragmentToRemove.Remove();
                }
                else
                {
                    if (Logger.IsWarnEnabled) Logger.Warn("Post id " + postToRemove.Id + " element was not found in the home page");
                }

            }

            //now append this to the original document and save.
            destinationDocument.DocumentNode.SelectSingleNode("//*[@id='post-container']").AppendChild(templateOfExcerpt);
            destinationDocument.Save(destinationFileName);
        }