Home > Web Front-end > JS Tutorial > Automatically generate article abstracts [JavaScript version]_javascript skills

Automatically generate article abstracts [JavaScript version]_javascript skills

WBOY
Release: 2016-05-16 19:23:22
Original
1595 people have browsed it
By dknt From bbs.blueidea.com

I searched this and found that I couldn’t find the answer. So I wrote one myself. Many programs bypass this problem and often manually decide where to truncate, which is too troublesome.

Implementation content: truncate a text containing HTML code, but there will be no problem of the containment tag not being closed.

A PHP version is here! : Automatically generate article summary [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.exec( Foremost);
if(newone == null) break;

if(newone[1] == ""){
var Elem = newone[2];
if(Elem .match(Singlable) && newone[3]!= ""){
                                                                                                             posStack.push(newone .index);

if(newone[3] == "") break;
var StackTop = Stack[Stack.length-1];
var End = newone[2].toUpperCase();
if(StackTop == End){
Stack.pop();
posStack.pop();
if (newone[3] == " "){
                                                                                                               
var cutpos = posStack.shift();
Foremost = Foremost.substring(0,cutpos);

return Foremost;
}



Test examples are as follows:

[Ctrl A select all Note:
If you need to introduce external Js, you need to refresh to execute ]<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|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; } </script>
This test case should work. If not, please open this page under pure IE.

The main problem is that the article cannot exceed 20,000 words, and HTMLencode and HTMLdecode are still missing. I have no choice but to embed it.

HTMLencode, HTMLdecode are defined in http://cs02.100steps.net/new/onejsneeded.js. I didn't write it, so go get it yourself if you need it.
My space speed may be slower for everyone, so there is nothing I can do...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template