当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

ASP.NET基于行数预测的长文章分页

由于目前文章上传大多采用HTML编辑器,使得里面参杂的HTML代码严重影响到场文章分页的效果。我现在想说的是一种基于文章行数预测为思路的一种分页方式.操作过程也很简单.

大家都是写程序的,费话少说,看代码 

9       
10        private static List<string> HtmlLineTest(ref string content, int pageid, string url)
11        {
12            //文章分段数组
13            List<string> segs = new List<string>();
14            int segmax = 30;  // 计划每一面的行数
15            int linewords = 50;
16            int segmin = 0;
17
18            string s;
19
20            //int pageheight = 25;
21            int line = 0;
22            对含有HTML代码的文章进行整理#region 对含有HTML代码的文章进行整理
23            content = Regex.Replace(content, "<p[^<>]*>", "", RegexOptions.IgnoreCase);
24            content = Regex.Replace(content, "<div[^<>]*>", "", RegexOptions.IgnoreCase);
25            content = Regex.Replace(content, "</p>", "<br />", RegexOptions.IgnoreCase);
26            content = Regex.Replace(content, "</div>", "", RegexOptions.IgnoreCase);
27            #endregion
28            MatchCollection mcc = Regex.Matches(content, "<br */*>|<img[^<>]+>", RegexOptions.IgnoreCase);
29            int idx = 0;
30            StringBuilder __article = new StringBuilder();
31            foreach (Match m in mcc)
32            {
33                if (m.Value.StartsWith("<br", StringComparison.OrdinalIgnoreCase))
34                {
35                    s = content.Substring(idx, m.Index - idx);
36                    if (!s.StartsWith("  "))
37                        __article.Append("  ");
38                    __article.Append(s);
39                    idx = m.Index + m.Value.Length;
40                    int lth = s.Length / linewords;
41                    if (s.Length % linewords < linewords/2)
42                        lth++;
43                    line += lth == 0 ? 1 : lth;
44                    segmin += lth == 0 ? 1 : lth;
45                    __article.Append("<br />");
46                    if (segmax <= segmin)
47                    {
48                        segs.Add(__article.ToString());
49                        _string.Remove(0, __article.Length);
50                        segmin = 0;
51                    }
52                }
53                else if (m.Value.StartsWith("<img", StringComparison.OrdinalIgnoreCase))
54                {
55                    int pos = m.Value.IndexOf("height");
56                    if (pos > -1)
57                    {
58                       
59                        string ms = m.Value.Substring(pos + 6);
60                        pos = int.Parse(Regex.Match(ms, "\\d+").Value);
61                        line += pos / 25;
62                        segmin += pos / 25;
63                    }
64                    else
65                    {
66                        // 图片没有发现表示高度的属性
67                        line += 400 / 25;
68                        segmin += 400 / 25;
69                    }
70                    idx = m.Index + m.Value.Length;
71                    __article.Append(m.Value);
72                    __article.Append("<br />");
73                    if (segmax <= segmin)
74                    {
75                        segs.Add(_string.ToString());
76                        __article.Remove(0, _string.Length);
77                        segmin = 0;
78                    }
79                }
80            }
81            if(__article.Length>0)
82                segs.Add(_string.ToString());
83            if (mcc.Count == 0)
84                segs.Add(content);
85            return segs;
86        }
当然了.这是服务端的执行代码.为了能够有效检测图片的长宽,我们把这些工作放在客户端来完成
 

 1
 2    // ++++++++++++++++++++++++++
 3    //         这段代码是针对fckeditor编码器出+ JQuery 写的
 4    //++++++++++++++++++++++++++
 5    function ImgStyleTest()
 6    {
 7        var FckDocument = $('iframe').eq(1)[0].contentWindow.document;
 8        var HtmlContainer = FckDocument.getElementsByTagName("iframe")[0].contentWindow.document;
 9        var imgs = HtmlContainer.getElementsByTagName("img");
10        for (i=0; i<imgs.length;i++ )
11        {
12             var imgObj = new Image();
13             imgObj.src = imgs[i].src;
14             // 检测图片有没有加载完成
15             if(imgObj.complete) {            
16                 var width = imgObj.width;
17                 var height = imgObj.height;
18                 // 对长度超过600的图片进行缩放
19                 if(width>600) {               
20                    height = 600 * height / width;
21                    width = 600;
22                 }
23                 imgs[i].style.width= width;
24                 imgs[i].style.height= height;       
25             } else {
26                setTimeout("ff()",1000);
27             }
28
29        }
30    }

这种做法是针对图片和文章的段进行文章分页的.如果某些文章有些段特长那就无能为力了,但是可以结合长段用字段长度分割相结合的方式进行分页.

相关内容
赞助商链接