Automatically generate article summary [JavaScript version]_javascript tips

Source: Internet
Author: User
by Dknt from Bbs.blueidea.com

Search the search this, find no answer. So I wrote one, a lot of programs around this problem, often artificial to decide where to truncate, too much trouble.

Implementation content: Truncate a section of text containing HTML code, but there is no containment tag does not have a closed problem.

a PHP version of Here! : automatically generate article summary [PHP version]。

The core components are as follows:
Copy Code code 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.exec (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;
}


Test cases are as follows:
<script src= "Http://cs02.100steps.net/new/onejsneeded.js" ></script> <body> [input] truncate AS <input id= "Length" type= "text" size= "a" value= "1"/> charactors <textarea name= "Original" cols= "" rows= " 0 <b > any text and pictures in this article are not allowed to be reproduced in any way, and offenders will resort to legal means to deal with!!! Please take it seriously! </b> <p align=center><b>; </P> <P> This is a doll, he is my brother. is a very cool and very cute puppy. </p> <P></P> <P> He is two years old this year, although very small, but the momentum is not inferior to the big dog. Very daring to fight evil forces </P> <P> Doll's distinctive feature is cute, especially when the back of the ear is particularly cute, as if a clever kitten. He's going to get up and dance, like he's going to do when you're going to give him something tasty. After all, this is his way of working for a living, these days, do not have the technology to eat food. </P> <P> He has another feature that is cool, even though we are brothers, he has never been sucking up to our human problems. In the afternoon, when the sun shines through the window, the doll will find the right place, lie on the floor, and let the beam caress him heartily. His posture is so serene and dignified, as if a holy poet, chin gently raised his gaze into the endless contemplation of the window, the eyes of the Saint-like light. When you gently walk past him, he will leisurely turn his head to look at you, with a cold and calm eyes, and then turn the heads to the original direction, chin again gently lift, immersed, infatuated with ...</p> <P> He is so pathetic, As a greenhouse puppy he can only stay at home every day to sleep or naughty. His life is so empty, so boring, so helpless. Because of the intellectual problem, he often do something stupid, also often directly or indirectly hurt himself (directly is eating big things stuck in the throat and so on, the indirect is to pee everywhere I cleaned up). But can you blame him? He's not as stupid as he thinks he is. The IQ is not high is decided by heaven,He can't control it either. Doll is so stupid, a pure silly dog, I have not been able to teach him C language programming. </P> <P> Doll is a sense of loyalty and very heavy feelings, when you are sad he will slowly walk by your side, he looked at you with that kind of caring eyes, as if in the Mexican language said "What can I do for you?" When you are about to leave the house the night before, he will come to your bed, you hold him up, he will tears look at you, full of deep feeling so that you can not sleep a long time <P> silly doll, now not in my side, I miss him. </P> <P> Below is the expression of a silly doll show!!! </P> <p align=center></p> <p align=center> look up the moon </P> <p align=center></p> &L T P align=center> "Hum, why bully me, hate" </P> <p align=center></p> <p align=center> "Ah, miserable, this how to do ah, not to go ... "</P> <p align=center></p> <p align=center>" Alas, worry ah, really no way. " </P> <p align=center></p> <p align=center> "Hum, I am poor ah, let me go, I will cry, so sad ah ..." </P> <p align=center></p> <p align=center> "Hey! What's wrong with you! I'm not talking to you! "</P> <p align=center></p> <p align=center>" Ah, kind of, you live griping at everything it! </P> <p align=center></p> <p align=center> "Hey! I am the ferocious big tiger! "</P></B> </textarea> [briefing] <input TyPe= "button" value= "Get Briefing" onclick= "Do ()"/> <textarea id= "Brief" cols= "M" rows= "ten" > </textarea&gt ; <script> function Do () {var soriginal = HtmlDecode (document.getElementById ("Original"). InnerHTML); var ilength = parseint (document.getElementById ("Length"). Value); if (isNaN (ilength) | | | ilength<=0) {alert ("Length Error"); var sbrief = Generate_brief (soriginal,ilength); document.getElementById ("Brief"). InnerHTML = HtmlEncode (sbrief); }; function Generate_brief (text,length) {if (Text.length < length) return text; var foremost = Text.substr (0,length); var re =/< (\/?) (body| Script| p| div| F12 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.exec (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; } </script> </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

This test case should be good, if not, please open this page under pure ie.

The main problem is that the article can not be more than 20000 words, but also a HTMLencode, HtmlDecode did not write in. There's no way to embed it.

HTMLencode, HtmlDecode in http://cs02.100steps.net/new/onejsneeded.js definition. I didn't write it, if I need to get it myself.
My space speed may be slower for everyone, then I have no way ...

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.