Automatically generate the document summary [JavaScript version]

Source: Internet
Author: User

By dknt From bbs.blueidea.com

I searched for this and found that no answer was found. So I wrote one by myself. Many programs bypass this issue and often manually decide where to truncate it, which is too troublesome.

Implementation content: truncate a piece of text containing HTML code, but there will be no issue that the congestion mark is not closed.

Here is a PHP version! : Automatically generate the article abstract [PHP version].

The core part is as follows:
Copy codeThe Code is as follows:
Function Generate_Brief (text, length ){
If (text. length <length) return text;
Var Foremost = text. substr (0, length );

Var re =/<(\/?) (BODY | SCRIPT | P | DIV | H1 | H2 | H3 | H4 | H5 | H6 | ADDRESS | PRE | TABLE | TR | TD | TH | INPUT | SELECT | TEXTAREA | OBJECT | A | UL | OL | LI | BASE | META | LINK | HR | BR | PARAM | IMG | AREA | INPUT | SPAN) [^>] * (> ?) /Ig;

Var Singlable =/BASE | META | LINK | HR | BR | PARAM | IMG | AREA | INPUT/I
Var Stack = new Array (), posStack = new Array ();
While (true ){
Var newone = re.exe c (Foremost );
If (newone = null) break;

If (newone [1] = ""){
Var Elem = newone [2];
If (Elem. match (Singlable) & newone [3]! = ""){
Continue;
}
Stack. push (newone [2]. toUpperCase ());
PosStack. push (newone. index );

If (newone [3] = "") break;
} Else {
Var StackTop = Stack [Stack. length-1];
Var End = newone [2]. toUpperCase ();
If (StackTop = End ){
Stack. pop ();
PosStack. pop ();
If (newone [3] = ""){
Foremost = Foremost + "> ";
}
}

};
}
Var cutpos = posStack. shift ();
Foremost = Foremost. substring (0, cutpos );

Return Foremost;
}


The test cases are as follows:
<Body> [input] truncate as <input id = "Length" type = "text" size = "10" style = "text-align: center "value =" 500 "/> charactors <pre class =" code "name =" Original "cols =" 100 "rows =" 10 <B style = "color: red "> any text or image in this article cannot be reproduced in any way. Violators will resort to legal measures !!! Take it seriously! </B> <P align = center> <B>; </P> <P> This is a doll, and he is my brother. It's a cool, cute puppy. </P> <P> </P> <P> he is two years old this year. Although he is very small, he is not inferior to a big dog at all. </P> <P> dolls are cute, especially when carrying their ears behind them, as if they were a clever kitten. He will also stand up and dance, for example, when you want to give him something delicious, he will do it. After all, this is one of his ways of making a living. In this year, there will be no technology to eat. </P> <P> another feature is cool. Although we are brothers, we never get rid of our human stinks. In the afternoon, when the bright sun shines in through the window, the doll finds a proper position on the floor and allows the beam to touch him. His posture is so peaceful and majestic, as if a sacred poet, gently looked up at the window into endless meditation, eyes shining like a saint. When you gently walk by him, he will leisurely turn around to look at you, with a kind of indifference and calm eyes, and then turn his head to the original direction, again gently raised, immersed, infatuated... </P> <P> he is so pitiful. As a greenhouse puppy, he can only stay at home and go to bed or be naughty every day. His life is so empty, so boring, so helpless. Because of his intellectual problems, he often does stupid things, and often directly or indirectly hurt himself (just eat things like dadongsika and hold his throat, in other words, I just picked up my system ). But does it blame him? He is not as stupid as he thinks. If the IQ is not high, it is determined by God, and he cannot control it. The doll is so silly, a pure silly dog, I finally failed to teach him C language programming. </P> <P> the doll is loyal and emotional. When you are sad, he will slowly walk by you. He looks at you with that kind of care, it seems that you are saying "What can I do for you? ". One night before you leave the house, he will go to your bed again. When you pick him up, he will look at you with tears, with deep affection, you can't fall asleep for a long time <P> silly doll. Now it's not around me, and I miss him very much. </P> <P> The following figure shows the expression of the silly doll !!! </P> <P align = center> Bright Moon </P> <P align = center> </P> <P align = center> "Hum, why bully me? I hate "</P> <P align = center>". What can I do now, not going down... "</P> <P align = center>" Alas, no way. "</P> <P align = center>" oh, sorry, let me go. I'm crying, sad... "</P> <P align = center>" Hello! What's wrong with you! I'm not listening to you! "</P> <P align = center>" oh, you're tired of it! "</P> <P align = center>" Hey! I am a fierce big tiger! "</P> </B> </textarea> [briefing] <input type =" button "value =" Get Briefing "onClick =" Do () "/> <pre class =" code "id =" Brief "cols =" 100 "rows =" 10 "> </textarea> </body>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
This test case should be easy to use. If not, open this page in pure IE.

The main problem is that the article cannot exceed 20000 characters, and there is another HTMLencode, HTMLdecode is not written in. There is no way to embed it.

HTMLencode, HTMLdecode defined in http://cs02.100steps.net/new/onejsneeded.js. It's not what I wrote. If you need to get it by yourself.
My space speed may be slow for everyone, so I can't do it anymore...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.