Esempio n. 1
0
        public override BlogPost Parse(XmlElement entryNode, bool includeCategories, Uri documentUri)
        {
            BlogPost post = new BlogPost();
            AtomEntry atomEntry = new AtomEntry(_atomVer, _atomNS, CategoryScheme, _nsMgr, documentUri, entryNode);

            post.Title = atomEntry.Title;
            post.Excerpt = atomEntry.Excerpt;
            post.Id = PostUriToPostId(atomEntry.EditUri);
            post.Permalink = atomEntry.Permalink;

            string content = atomEntry.ContentHtml;
            if (content.Trim() != string.Empty)
            {
                HtmlExtractor ex = new HtmlExtractor(content);
                int start, length;
                if (Options.SupportsExtendedEntries && ex.Seek("<a name=\"more\">").Success)
                {
                    start = ex.Element.Offset;
                    length = ex.Element.Length;
                    if (ex.Seek("</a>").Success)
                    {
                        post.SetContents(content.Substring(0, start), content.Substring(ex.Element.Offset + ex.Element.Length));
                    }
                    else
                    {
                        post.SetContents(content.Substring(0, start), content.Substring(start + length));
                    }
                }
                else
                {
                    post.Contents = content;
                }
            }

            post.DatePublished = atomEntry.PublishDate;
            if (Options.SupportsCategories && includeCategories)
                post.Categories = atomEntry.Categories;

            return post;
        }
        private BlogPost ParseBlogPost(XmlNode postNode, bool includeCategories)
        {
            // create blog post
            BlogPost blogPost = new BlogPost();

            // get node values
            blogPost.Id = NodeText(postNode.SelectSingleNode("member[name='postid']/value"));
            if (blogPost.Id == String.Empty)
                blogPost.Id = NodeText(postNode.SelectSingleNode("member[name='page_id']/value"));

            SetPostTitleFromXmlValue(blogPost, NodeText(postNode.SelectSingleNode("member[name='title']/value")));

            // attempt to discover the permalink
            blogPost.Permalink = NodeText(postNode.SelectSingleNode("member[name='permaLink']/value"));
            if (blogPost.Permalink == String.Empty)
                blogPost.Permalink = NodeText(postNode.SelectSingleNode("member[name='permalink']/value"));
            if (blogPost.Permalink == String.Empty)
                blogPost.Permalink = NodeText(postNode.SelectSingleNode("member[name='link']/value"));

            // contents and extended contents
            string mainContents = NodeText(postNode.SelectSingleNode("member[name='description']/value"));
            string extendedContents = NodeText(postNode.SelectSingleNode("member[name='mt_text_more']/value"));
            blogPost.SetContents(mainContents, extendedContents);

            // date published
            XmlNode dateCreatedNode = postNode.SelectSingleNode("member[name='date_created_gmt']/value");
            if (dateCreatedNode == null)
                dateCreatedNode = postNode.SelectSingleNode("member[name='dateCreated']/value");
            blogPost.DatePublished = ParseBlogDate(dateCreatedNode);

            // extract comment field
            if (Options.SupportsCommentPolicy)
            {
                string commentPolicy = NodeText(postNode.SelectSingleNode("member[name='mt_allow_comments']/value"));
                if (commentPolicy != String.Empty)
                {
                    switch (commentPolicy)
                    {
                        case "0":
                            blogPost.CommentPolicy = BlogCommentPolicy.None;
                            break;
                        case "1":
                            blogPost.CommentPolicy = BlogCommentPolicy.Open;
                            break;
                        case "2":
                            blogPost.CommentPolicy = BlogCommentPolicy.Closed;
                            break;
                        default:
                            Trace.Fail("Unexpected value for mt_allow_comments: " + commentPolicy);
                            break;
                    }
                }
            }

            // extract trackback field
            if (Options.SupportsPingPolicy)
            {
                string allowTrackbacks = NodeText(postNode.SelectSingleNode("member[name='mt_allow_pings']/value"));
                if (allowTrackbacks != String.Empty)
                    blogPost.TrackbackPolicy = (allowTrackbacks == "1") ? BlogTrackbackPolicy.Allow : BlogTrackbackPolicy.Deny;
            }

            // extract keywords field
            if (Options.SupportsKeywords)
            {
                string keywords = NodeText(postNode.SelectSingleNode("member[name='mt_keywords']/value"));
                blogPost.Keywords = HtmlUtils.UnEscapeEntities(keywords, HtmlUtils.UnEscapeMode.Default);
            }

            // extract excerpt field
            if (Options.SupportsExcerpt)
            {
                blogPost.Excerpt = NodeText(postNode.SelectSingleNode("member[name='mt_excerpt']/value"));
            }

            // extract ping url array
            if (Options.SupportsTrackbacks)
            {

                XmlNode pingUrlsNode = postNode.SelectSingleNode("member[name='mt_tb_ping_urls']/value");
                if (pingUrlsNode != null)
                {
                    ArrayList sentPingUrls = new ArrayList();
                    if (Options.TrackbackDelimiter == TrackbackDelimiter.ArrayElement)
                    {
                        XmlNodeList pingUrlNodes = pingUrlsNode.SelectNodes("array/data/value");
                        foreach (XmlNode node in pingUrlNodes)
                            sentPingUrls.Add(node.InnerText.Trim());
                    }
                    else
                    {
                        string delimiter = null;
                        if (Options.TrackbackDelimiter == TrackbackDelimiter.Space)
                            delimiter = " ";
                        else if (Options.TrackbackDelimiter == TrackbackDelimiter.Comma)
                            delimiter = ",";
                        sentPingUrls.AddRange(StringHelper.Split(pingUrlsNode.InnerText, delimiter));
                    }

                    blogPost.PingUrlsSent = sentPingUrls.ToArray(typeof(string)) as string[];
                }
            }

            // extract slug
            if (Options.SupportsSlug)
            {
                blogPost.Slug = NodeText(postNode.SelectSingleNode("member[name='wp_slug']/value"));
                if (blogPost.Slug == String.Empty)
                    blogPost.Slug = NodeText(postNode.SelectSingleNode("member[name='mt_basename']/value"));
            }

            // extract password
            if (Options.SupportsPassword)
            {
                blogPost.Password = NodeText(postNode.SelectSingleNode("member[name='wp_password']/value"));
            }

            // extract author
            if (Options.SupportsAuthor)
            {
                string authorId = NodeText(postNode.SelectSingleNode("member[name='wp_author_id']/value"));

                // account for different display name syntax in various calls
                string authorName = NodeText(postNode.SelectSingleNode("member[name='wp_author_display_name']/value"));

                blogPost.Author = new PostIdAndNameField(authorId, authorName);
            }

            // extract page parent
            if (Options.SupportsPageParent)
            {
                string pageParentId = NodeText(postNode.SelectSingleNode("member[name='wp_page_parent_id']/value"));
                // convert 0 to empty string for parent-id
                if (pageParentId == "0")
                    pageParentId = String.Empty;

                string pageParentTitle = NodeText(postNode.SelectSingleNode("member[name='wp_page_parent_title']/value"));
                blogPost.PageParent = new PostIdAndNameField(pageParentId, pageParentTitle);
            }

            // extract page order
            if (Options.SupportsPageOrder)
            {
                blogPost.PageOrder = NodeText(postNode.SelectSingleNode("member[name='wp_page_order']/value"));
            }

            // extract categories
            if (includeCategories && Options.SupportsCategories)
            {
                if (Options.SupportsCategoriesInline && !Options.SupportsHierarchicalCategories)
                {
                    blogPost.Categories = MetaweblogExtractCategories(blogPost.Id, postNode);
                }
                else
                {
                    blogPost.Categories = MovableTypeExtractCategories(blogPost.Id, postNode);
                }
            }

            // allow post-processing of extracted blog post
            BlogPostReadFilter(blogPost);

            // return the post
            return blogPost;
        }
        private void ParsePostContent(XmlNode xmlNode, BlogPost blogPost)
        {
            // get raw content (decode base64 if necessary)
            string content;
            XmlNode base64Node = xmlNode.SelectSingleNode("base64");
            if (base64Node != null)
            {
                byte[] contentBytes = Convert.FromBase64String(base64Node.InnerText);
                content = _utf8EncodingNoBOM.GetString(contentBytes);
            }
            else // no base64 encoding, just read text
            {
                content = xmlNode.InnerText;
            }

            // parse out the title and contents of the post
            HtmlExtractor ex = new HtmlExtractor(content);
            if (ex.Seek("<title>").Success)
            {
                SetPostTitleFromXmlValue(blogPost, ex.CollectTextUntil("title"));
                content = content.Substring(ex.Parser.Position).TrimStart('\r', '\n');

            }

            if (content.Trim() != string.Empty)
            {
                HtmlExtractor ex2 = new HtmlExtractor(content);
                if (Options.SupportsExtendedEntries && ex2.Seek("<lj-cut>").Success)
                    blogPost.SetContents(content.Substring(0, ex2.Element.Offset), content.Substring(ex2.Element.Offset + ex2.Element.Length));
                else
                    blogPost.Contents = content;
            }

        }