The usual practice of a blog system is to display only the abstract of an article on the blog homepage. Click the title to view the full text. It is a special issue to show which part is used as the abstract. Different systems have their own different processing methods, and some use the abstract and extended parts as two input boxes, the user determines which part is used as the abstract, and the upper and lower parts are complete html. There is no interception problem, just like the Serendipity system used on this site. You can also insert a <! -- More --> MARK: When the blog list page is displayed, the article is intercepted with this mark. However, I have not tried to insert this mark in the middle of a table. However, this practice allows users to decide on how to intercept the article, even if the interception is wrong, you can reset it.
At the beginning of the design, our company's blog system was influenced by the opinions of many people (mainly the opinions of many people who have not actually written blogs ), the opinion is that it is too troublesome to use two boxes. I am afraid I don't understand most of the insert tags. We want to provide users with the simplest operation. Users only need to write their articles, how to intercept it to the system. As a result, I am stuck in a loop of crazy bug change.
How to extract 400 words from the entire HTML segment as the abstract? Maybe deleting all <> tags is the easiest way. However, you still want to retain your personality in the blog text, and someone wants to use a large font or use a large font for an article, to be more prominent, and so on. In short, users only want to make their blogs more beautiful, so they don't care how they intercept them.
If you simply extract 400 words, it is likely to be captured in the middle of a tag, these half labels may cause the subsequent large text to be used as the text in the label but not displayed until the next Terminator is encountered. Therefore, you must first follow the <> marker to ensure that the position to be intercepted is not in the middle of the two marks. The specific method is to find a <symbol, look at the mark in it, and find the end mark corresponding to it. For example, if you find <P, you can find it </P>, find <Div, and find </div>. Then I found that if half a table is intercepted, the layout of the entire page will be misplaced, And the sidebar of the blog will all go below the body, because it will be processed as a cell in the table. If you want to see
Table is truncated before the table. As a result, no word exists in the abstract of many articles. Then we found that not only the table will damage the layout, but the layout will also be misplaced if the front and back of the DIV do not match, because our blog template uses the DIV layout. There is a problem with the previous truncation method, because Div can be nested. When two <div> tags appear consecutively, the first Div tag appears, directly jump to the </div> mark at the end of the first entry, and the second <div> will have no Terminator. In addition, font and span can be nested.
Later, I figured out another way to simply filter out all the Divs, Font, and span, and replace all the <p> </P> with the BR. Finally, basically, there will be no truncation error,, the segmentation and indentation of many articles are defined by style in the div (I don't know where these users' articles are copied from ), as a result, many blog homepage texts are converted into the same 12px text, which is very unsightly. Later, a user complained that his or her blog page had an error and could not be opened. Go in and check out that there are more than N in his blog posts. <! -- [If [and so on. After such a symbol is displayed, the regular expression used for truncation will return an error. Then, all the annotation symbols are deleted before the truncation. Two days later, he reported an error again. There is still <! [If tags. God, where did you copy the article?
There is no way, that is, to retain the format, and to be compatible with a variety of unexpected tag structures, this problem must be resolved.
Idea: the safest way to intercept text without interrupting the format is to retain all formats. So I thought of a way to only limit the number of words in the text content, leaving all the HTML tags. For example, if you want to take 400 characters, you can start from the first character of the body. If you want to use a common text, add the result variable and add the stenographer to 1. If the counter has reached 400, ignore this word. If the character is <, put all the text in the result variable until>. In this way, the extracted summary contains 400 characters and a bunch of HTML tags in complete format. (The premise is that HTML is complete when it is pasted in ).
However, if there are still some BRS, P, and Li after reaching 400 words, there will be a lot of white space behind the abstract, of course not. Therefore, we need to delete these items. It is too troublesome to count P and change P to BR. If the counter has reached 400, all BR tags will be ignored. This is true for Li, but it must be special because it cannot be truncated between two li. Similarly, you can perform the same processing on <tr> <TD>, so that there will be no half blank tables.
In general, this effect is acceptable. Although the source code contains a lot of useless code (especially those who paste the article from the word), the interception is quite perfect on the surface.
/// <Summary> /// extract the abstract, clear HTML code /// </Summary> /// <Param name = "content"> </param> /// <Param name = "length"> </Param >/// <Param name = "striphtml"> </param> /// <returns> </returns> Public static string getcontentsummary (string content, int length, bool striphtml) {If (string. isnullorempty (content) | length = 0) Return ""; if (striphtml) {RegEx Re = new RegEx ("<[^>] *> "); content = Re. replace (Conte NT, ""); content = content. replace ("",""). replace ("", ""); If (content. length <= length) return content; else return content. substring (0, length) + "...... ";} Else {If (content. length <= length) return content; int Pos = 0, NPOs = 0, size = 0; bool firststop = false, notr = false, Noli = false; stringbuilder sb = new stringbuilder (); While (true) {If (Pos> = content. length) break; string cur = content. substring (Pos, 1); If (cur = "<") {string next = content. substring (Pos + 1, 3 ). tolower (); If (next. indexof ("p") = 0 & next. indexof ("pre ")! = 0) {NPOs = content. indexof (">", POS) + 1;} else if (next. indexof ("/P") = 0 & next. indexof ("/PR ")! = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) Sb. append ("<br/>");} else if (next. indexof ("Br") = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) Sb. append ("<br/>");} else if (next. indexof ("IMG") = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) {sb. append (content. substring (Pos, NPOs-Pos); size + = NPOs-pos + 1 ;}} else if (next. indexof ("Li") = = 0 | next. indexof ("/Li") = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) {sb. append (content. substring (Pos, NPOs-Pos);} else {If (! Noli & next. indexof ("/Li") = 0) {sb. append (content. substring (Pos, NPOs-Pos); Noli = true ;}} else if (next. indexof ("TR") = 0 | next. indexof ("/TR") = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) {sb. append (content. substring (Pos, NPOs-Pos);} else {If (! Notr & next. indexof ("/TR") = 0) {sb. append (content. substring (Pos, NPOs-Pos); notr = true ;}} else if (next. indexof ("TD") = 0 | next. indexof ("/TD") = 0) {NPOs = content. indexof (">", POS) + 1; if (size <length) {sb. append (content. substring (Pos, NPOs-Pos);} else {If (! Notr) {sb. append (content. substring (Pos, NPOs-Pos) ;}} else {NPOs = content. indexof (">", POS) + 1; sb. append (content. substring (Pos, NPOs-Pos);} If (NPOs <= POS) NPOs = POS + 1; Pos = NPOs;} else {If (size <length) {sb. append (cur); size ++;} else {If (! Firststop) {sb. append ("...... "); Firststop = true ;}} POS ++ ;}return sb. tostring ();}}