Пример #1
0
        private void FormatPosts(string originalFolderPath, string targetFolderPath)
        {
            foreach (string file in Directory.GetFiles(originalFolderPath, "*.xml"))
            {
                if (!file.EndsWith("dayentry.xml", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                XmlDocument         docOrig = LoadDocument(file);
                XmlNamespaceManager nsm     = LoadNamespaceManager(docOrig);

                foreach (XmlNode entry in docOrig.SelectNodes("//Entry", nsm))
                {
                    Post post = new Post();
                    post.Categories   = FormatCategories(entry.SelectSingleNode("Categories")).ToArray();
                    post.Title        = entry.SelectSingleNode("Title").InnerText;
                    post.Slug         = FormatterHelpers.FormatSlug(post.Title);
                    post.PubDate      = DateTime.Parse(entry.SelectSingleNode("//Created").InnerText);
                    post.LastModified = DateTime.Parse(entry.SelectSingleNode("//Modified").InnerText);
                    post.Content      = FormatFileReferences(entry.SelectSingleNode("Content").InnerText);
                    post.Author       = entry.SelectSingleNode("Author").InnerText;
                    post.IsPublished  = bool.Parse(ReadValue(entry.SelectSingleNode("IsPublic"), "true"));

                    string newFile = Path.Combine(targetFolderPath, entry.SelectSingleNode("EntryId").InnerText + ".xml");
                    Storage.Save(post, newFile);
                }
            }
        }
Пример #2
0
        private void FormatSlug(XmlDocument doc)
        {
            XmlNode slug = doc.SelectSingleNode("//slug");

            if (slug != null)
            {
                slug.InnerText = FormatterHelpers.FormatSlug(slug.InnerText);
            }
        }
        Post ParsePost(XElement postData, Dictionary <string, string> authors, Dictionary <string, string> categories)
        {
            Post post = new Post()
            {
                Title        = postData.Element(ns + "title").Value,
                Content      = postData.Element(ns + "content").Value,
                PubDate      = (DateTime)postData.Attribute("date-created"),
                LastModified = (DateTime)postData.Attribute("date-modified"),
                IsPublished  = (bool)postData.Attribute("approved"),
                Categories   = postData.Descendants(ns + "category").Select(x => categories[x.Attribute("ref").Value]).ToArray()
            };

            XElement name = postData.Element(ns + "post-name");

            if (name != null)
            {
                post.Slug = name.Value;
            }
            else
            {
                post.Slug = FormatterHelpers.FormatSlug(post.Title);
            }

            XElement author = postData.Descendants(ns + "author").FirstOrDefault();

            if (author != null)
            {
                var thisArthor = author.Attribute("ref").Value;
                foreach (var thisKey in authors.Keys)
                {
                    // Fixes issue when author name has different cases from the Key value of Authors
                    if (thisArthor.Equals(thisKey, StringComparison.CurrentCultureIgnoreCase))
                    {
                        post.Author = authors[thisKey];
                    }
                }
            }

            foreach (XElement commentData in postData.Descendants(ns + "comment"))
            {
                if (commentData.Attribute("approved").Value == "true")
                {
                    post.Comments.Add(new Comment()
                    {
                        ID      = commentData.Attribute("id").Value,
                        PubDate = (DateTime)commentData.Attribute("date-created"),
                        Content = commentData.Element(ns + "content").Value,
                        Author  = (string)commentData.Attribute("user-name"),
                        Website = (string)commentData.Attribute("user-url"),
                        Email   = (string)commentData.Attribute("user-email"),
                    });
                }
            }

            return(post);
        }
        private void FormatPosts(string originalFolderPath, string targetFolderPath)
        {
            foreach (string file in Directory.GetFiles(originalFolderPath, "*.xml"))
            {
                XmlDocument         docOrig          = LoadDocument(file);
                XmlNamespaceManager nsm              = LoadNamespaceManager(docOrig);
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(docOrig.NameTable);
                namespaceManager.AddNamespace("content", docOrig.DocumentElement.GetNamespaceOfPrefix("content"));
                namespaceManager.AddNamespace("dc", docOrig.DocumentElement.GetNamespaceOfPrefix("dc"));
                namespaceManager.AddNamespace("wp", docOrig.DocumentElement.GetNamespaceOfPrefix("wp"));


                foreach (XmlNode entry in docOrig.SelectNodes("//item", nsm))
                {
                    Post          post             = new Post();
                    XmlNodeList   categories       = entry.SelectNodes("category[@domain='category']");
                    List <string> resultCategories = new List <string>();
                    foreach (XmlNode category in categories)
                    {
                        resultCategories.Add(category.InnerText);
                    }

                    post.Categories   = resultCategories.ToArray();
                    post.Title        = entry.SelectSingleNode("title").InnerText;
                    post.Slug         = FormatterHelpers.FormatSlug(post.Title);
                    post.PubDate      = DateTime.Parse(entry.SelectSingleNode("pubDate").InnerText);
                    post.LastModified = DateTime.Parse(entry.SelectSingleNode("pubDate").InnerText);


                    post.Content     = FormatFileReferences(entry.SelectSingleNode("content:encoded", namespaceManager).InnerText);
                    post.Author      = entry.SelectSingleNode("dc:creator", namespaceManager).InnerText;
                    post.IsPublished = ReadValue(entry.SelectSingleNode("wp:status", namespaceManager), "publish") == "publish";

                    // FormatComments()
                    foreach (XmlNode comment in entry.SelectNodes("wp:comment", namespaceManager))
                    {
                        FomartComment(ref post, comment, namespaceManager);
                    }


                    string newFile = Path.Combine(targetFolderPath, entry.SelectSingleNode("wp:post_id", namespaceManager).InnerText + ".xml");
                    Storage.Save(post, newFile);
                }
            }
        }
Пример #5
0
 private string FormatSlug(XmlNode node)
 {
     return(FormatterHelpers.FormatSlug(node.InnerText));
 }