凡事从积极的态度做起
记述技术(Web/BI/biztalk infopath/sharepoint)、人、事

修改BlogEngine.NET:给摘要显示添加第一副图片的显示

星期三, 29 十月 2008 11:30 by cleo

wordpress之类的博客程序,可以在摘要中显示图片,我很是羡慕。

既然BlogEngine.NET是开源的,所以我们很容易修改代码来加上这个功能。(当然我的显示并不完美,算是抛砖引玉。)

 

第一步:找到显示摘要的地方。

通过Studio打开项目,很容易找到,控制摘要显示的地方在:

BlogEngine.Core.Web.Controls.PostViewBase类

初始状态:

   1: /// <summary>
   2: /// Gets the body of the post. Important: use this instead of Post.Content.
   3: /// </summary>
   4: public string Body
   5: {
   6:     get
   7:     {
   8:         string body = Post.Content;
   9:         if (ShowExcerpt)
  10:         {
  11:             string link = " <a href=\"" + Post.RelativeLink.ToString() + "\">[" + (Page as BlogBasePage).Translate("more") + "]</a>";
  12:  
  13:             if (!string.IsNullOrEmpty(Post.Description))
  14:             {
  15:                 body =  Post.Description + link;
  16:             }
  17:             else
  18:             {
  19:                 body = Utils.StripHtml(Post.Content);
  20:                 if (body.Length > 300)
  21:                     body =  body.Substring(0, 300) + "..." + link ;
  22:             }
  23:         }
  24:  
  25:         ServingEventArgs arg = new ServingEventArgs(body, this.Location);
  26:         Post.OnServing(Post, arg);
  27:  
  28:         if (arg.Cancel)
  29:         {
  30:             if (arg.Location == ServingLocation.SinglePost)
  31:             {
  32:                 Response.Redirect("~/error404.aspx", true);
  33:             }
  34:             else
  35:             {
  36:                 this.Visible = false;
  37:             }
  38:         }
  39:  
  40:         return arg.Body ?? string.Empty;
  41:     }
  42: }

修改后:

   1: /// <summary>
   2: /// Gets the body of the post. Important: use this instead of Post.Content.
   3: /// </summary>
   4: public string Body
   5: {
   6:     get
   7:     {
   8:         string body = Post.Content;
   9:         if (ShowExcerpt)
  10:         {
  11:             string link = " <a href=\"" + Post.RelativeLink.ToString() + "\">[" + (Page as BlogBasePage).Translate("more") + "]</a>";
  12:  
  13:             if (!string.IsNullOrEmpty(Post.Description))
  14:             {
  15:                 body = cleo.getImage(Post.Content) + Post.Description + link;
  16:             }
  17:             else
  18:             {
  19:                 body = Utils.StripHtml(Post.Content);
  20:                 if (body.Length > 300)
  21:                     body =  cleo.getImage(Post.Content) + body.Substring(0, 300) + "..." + link ;
  22:             }
  23:         }
  24:  
  25:         ServingEventArgs arg = new ServingEventArgs(body, this.Location);
  26:         Post.OnServing(Post, arg);
  27:  
  28:         if (arg.Cancel)
  29:         {
  30:             if (arg.Location == ServingLocation.SinglePost)
  31:             {
  32:                 Response.Redirect("~/error404.aspx", true);
  33:             }
  34:             else
  35:             {
  36:                 this.Visible = false;
  37:             }
  38:         }
  39:  
  40:         return arg.Body ?? string.Empty;
  41:     }
  42: }

 

引用的cleo类:

   1: public class cleo
   2:    {
   3:        public static string getImage(string input)
   4:        {              
   5:            if (input == null)
   6:                return "";
   7:  
   8:            string pain = input;//字符串
   9:            string pattern = @"<img(.|\n)+?>";
  10:  
  11:            Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
  12:            if (m.Success)
  13:            {
  14:                string src = getSrc (m.Value);
  15:                string img = string.Format("<img align=\"right\" style=\"border-bottom: 0px; border-left: 0px; display: inline; margin-left: 5px; border-top: 0px; margin-right: 5px; border-right: 0px\" border=\"0\" width=\"100\" {0}  />",src);
  16:                return img;
  17:            }
  18:            else
  19:            {
  20:                return "";
  21:            }
  22:            
  23:        }
  24:  
  25:        static string getSrc(string input)
  26:        {           
  27:            string pattern = "src=[\'|\"](.+?)[\'|\"]";
  28:  
  29:            Regex reImg = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
  30:            Match mImg = reImg.Match(input);
  31:            if (mImg.Success)
  32:            {
  33:                return mImg.Value;
  34:            }
  35:            return "";
  36:         
  37:        }
  38:    }

最后的显示效果:

conceptDisplayImage

需要改进的地方:

如何图片位置何如控制?大小如何更好的控制?更加美观。

第一个打分

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  
Categories:   BlogEngine.NET
Actions:   E-mail | del.icio.us | Permalink | 评论 (0) | Comment RSSRSS comment feed

添加评论


(将显示你的Gravatar图标)  

  Country flag

biuquote
  • 评论
  • 在线预览
Loading



 
酷站目录