Пример #1
0
        public static PostHomeFeaturedPost PostToFeaturedPost(Blog.Post post, BlogDataDataContext database)
        {
            PostHomeFeaturedPost featuredPost = new PostHomeFeaturedPost();

            string rootUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
            string postUrl = rootUrl + post.Slug;

            featuredPost.Title = post.Title;
            featuredPost.Url   = postUrl;

            string imageUrl = Regex.Match(post.Body, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value;

            if (!String.IsNullOrEmpty(imageUrl) && !String.IsNullOrWhiteSpace(imageUrl))
            {
                featuredPost.FeaturedImageUrl = imageUrl;
            }

            return(featuredPost);
        }
Пример #2
0
        public static PostHomeTable GetTable(int page)
        {
            PostHomeTable table = new PostHomeTable();
            List <PostHomeFeaturedPost> featuredPosts = new List <PostHomeFeaturedPost>();
            List <PostHomeTableRow>     rows          = new List <PostHomeTableRow>();

            using (BlogDataDataContext database = new BlogDataDataContext())
            {
                var allPosts        = database.Posts.Where(x => x.Status == "publish" && x.Visibility == "Public").ToList().OrderByDescending(x => x.Timestamp);
                var firstThreePosts = allPosts.Take(3);
                foreach (var featuredPost in firstThreePosts)
                {
                    featuredPosts.Add(PostHomeFeaturedPost.PostToFeaturedPost(featuredPost, database));
                }

                var posts = allPosts.Skip((page - 1) * 10).Take(10);
                foreach (var post in posts)
                {
                    rows.Add(PostHomeTableRow.PostToRow(post, database));
                }
                table.Rows = rows;

                string rootUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";

                int    numberOfPages = (int)Math.Ceiling((double)allPosts.Count() / (double)10);
                string pagination    = "<ul class='pagination'>";
                if (page != 1)
                {
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page - 1) + "'>« Previous Page</a></li>";
                    if ((page - 3) > 1)
                    {
                        pagination += "<li><a href='" + rootUrl + "Page/" + 1 + "'>1</a></li>";
                        pagination += "<li class='disabled'><a href='#'>...</a></li>";
                    }
                    else if (page == 4)
                    {
                        pagination += "<li><a href='" + rootUrl + "Page/" + 1 + "'>1</a></li>";
                    }
                }

                if ((page - 2) > 0)
                {
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page - 2) + "'>" + (page - 2) + "</a></li>";
                }
                if ((page - 1) > 0)
                {
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page - 1) + "'>" + (page - 1) + "</a></li>";
                }
                pagination += "<li class='active'><a href='" + rootUrl + "Page/" + (page) + "'>" + (page) + "</a></li>";
                if ((page + 1) < numberOfPages)
                {
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page + 1) + "'>" + (page + 1) + "</a></li>";
                }
                if ((page + 2) < numberOfPages)
                {
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page + 2) + "'>" + (page + 2) + "</a></li>";
                }

                if (page != numberOfPages)
                {
                    if ((page + 3) < numberOfPages)
                    {
                        pagination += "<li class='disabled'><a href='#'>...</a></li>";
                        pagination += "<li><a href='" + rootUrl + "Page/" + numberOfPages + "'>" + numberOfPages + "</a></li>";
                    }
                    else if (page == (numberOfPages - 1) || page == (numberOfPages - 2) || page == (numberOfPages - 3))
                    {
                        pagination += "<li><a href='" + rootUrl + "Page/" + numberOfPages + "'>" + numberOfPages + "</a></li>";
                    }
                    pagination += "<li><a href='" + rootUrl + "Page/" + (page + 1) + "'>Next Page »</a></li>";
                }

                pagination         += "</ul>";
                table.Pagination    = pagination;
                table.FeaturedPosts = featuredPosts;
            }


            return(table);
        }