private Entry ConvertPostToEntry(Post post) { var entry = new Entry(); entry.PageId = this.GetPageIdFromPost(post); entry.Body = post.Body; entry.PublishedAtUtc = post.PublishedAtUtc; entry.LastModifiedAtUtc = DateTimeOffset.UtcNow; entry.Slug = post.Slug; entry.Title = post.Title; entry.Tags = new Collection<string>(post.Tags.Select(t => t.Slug).ToList()); return entry; }
private Post ParsePostElement(XElement postElement) { var postTitleElement = postElement.Element("title"); var postUsernameElement = postElement.Element(DublinCoreNamespace + "creator"); var postBodyElement = postElement.Element(RssContentNamespace + "encoded"); var postPublishedAtUtcElement = postElement.Element(WordpressNamespace + "post_date_gmt"); var postSlugElement = postElement.Element(WordpressNamespace + "post_name"); if (postTitleElement == null || postUsernameElement == null || postBodyElement == null || postPublishedAtUtcElement == null || postSlugElement == null) { throw new XmlException("Unable to parse malformed post."); } var post = new Post { Author = this.GetAuthorByUsername(postUsernameElement.Value), Body = postBodyElement.Value, PublishedAtUtc = DateTimeOffset.Parse(postPublishedAtUtcElement.Value), Slug = postSlugElement.Value, Title = postTitleElement.Value }; var categories = new List<Category>(); var tags = new List<Tag>(); var wpCategoriesElements = postElement.Elements("category"); foreach (var wpCategory in wpCategoriesElements) { var domainAttribute = wpCategory.Attribute("domain"); if (domainAttribute == null) { throw new XmlException("Unable to parse malformed wordpress categorization."); } if (domainAttribute.Value == "category") { string categorySlug = wpCategory.Attribute("nicename").Value; var category = this.GetCategoryBySlug(categorySlug); categories.Add(category); } else if (domainAttribute.Value == "post_tag") { string tagSlug = wpCategory.Attribute("nicename").Value; var tag = this.GetTagBySlug(tagSlug); tags.Add(tag); } } post.Categories = categories; post.Tags = tags; return post; }
private string GetPageIdFromPost(Post post) { var pageCategory = post.Categories.FirstOrDefault(); if (pageCategory == null) { return Page.HomePage.Id; } var page = this.importedPages .FirstOrDefault(p => p.Slug == pageCategory.Slug); if (page == null) { return Page.HomePage.Id; } return page.Id; }
public void SavePostAsEntry(Post post) { var entry = this.ConvertPostToEntry(post); this.session.Store(entry); }