Code
JS实现自动排版
function ispun(c1,c2)
{
if ((c1 == '\xa1' && c2 == '\xa3') || (c1 == '\xa3' && c2 == '\xac')|| (c1 == '\xa3' && c2 == '\xbb') || (c1 == '\xa3' && c2 == '\xba') || (c1 == '\xa3' && c2 == '\xa1') || (c1 == '\xa1' && c2 == '\xb1') || (c3 == '\xa3' && c2 == '\xbf') || (c3 == '\xa1' && c2 == '\xb7'))
return 1;
return 0;
}
function ishalfpun(c2)
{
if (c2 == '\x2c' || c2 =='\x2e' || c2 =='\x3b' || c2 =='\x2e' || c2 =='\x3a' || c2 =='\x21' || c2 =='\x3f' || c2 =='\x3e')
return 1;
return 0;
}
function formattext(text,addp)
{
//首先把"<p>"、"<br>"、"</p>"、"<p> "全部滤掉
var naivete_array =text.split("<p> ");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("<p>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("<br>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("</p>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
oldlen = text.length;
tmpstr = "";
del = '1';
for(i=0; i<oldlen; i++)
{
c1 = text.charAt(i);
if (c1 == '\r')
{
del = '1';
continue;
}
else
{
if(c1 > '\xff')
{
if ((c1 == ' ' && (del == '1')) || c1 == '\x09')
{
continue;
}
else
{
if (c1 == ' ' || c1 == '\n' || c1 == '\x09')
del = '1';
else
del = '0';
tmpstr += c1;
}
}
else
{
if(c1 > '\x80')
{
del = '1';
tmpstr += c1;
}
else
{
if ((c1 == ' ' && (del == '1')) || c1 == '\x09')
{
continue;
}
else
{
if (c1 == ' ' || c1 == '\n' || c1 == '\x09')
del = '1';
else
del = '0';
tmpstr += c1;
}
}
}
}
}
text = tmpstr;
oldlen = text.length;
tmpstr = "";
for(i=0; i<oldlen; i++)
{
c1 = text.charAt(i);
c2 = text.charAt(i+1);
c3 = text.charAt(i+2);
if (c1 == '\n' && c2 == '\n' && c3 == '\n')
continue;
tmpstr += c1;
}
text = tmpstr;
oldlen = text.length;
//result = (addp) ? "<p>" : " "; //用全角空格代替
result = (addp) ? "<p> " : " "; //用全角空格代替
count = 4;
oneretn = 0;
for(i=0; i<oldlen-1; i++)
{
c1 = text.charAt(i);
c2 = text.charAt(i+1);
c3 = text.charAt(i+2);
c4 = text.charAt(i+3);
c5 = text.charAt(i+4);
c6 = text.charAt(i+5);
c7 = text.charAt(i+6);
c8 = text.charAt(i+7);
if (c1 == '\n')
{
if (c2 == '\n')
{
if (oneretn == 1)
result += (addp) ? "</p>\n<p>" : "\n";
else
result += (addp) ? "</p>\n\n<p>" : "\n\n";
//result += " ";
result += " "; //用全角空格代替
count = 4;
i++;
oneretn = 0;
}
continue;
}
else
{
if (c1 == " " && count == 0)
{
continue;
}
else
{
if ((c1 == '\xa1' && c2 == '\xa1') && count == 0)
{
i++;
continue;
}
}
oneretn = 0;
if(c1 > '\xff')
{
result += c1;
count+=1;
if (c2 != '\n')
{
if (ishalfpun(c2))
{
result += c2;
count+=1;
i++;
}
else
{
if (c2 == '\x22' || c2== '\x27')
{
result += c2;
count+=1;
i++;
if (c3 != '\n')
{
if (ishalfpun(c3))
{
result += c3;
count+=1;
i++;
}
}
else
{
if (ishalfpun(c4))
{
result += c4;
count+=1;
i+=2;
}
}
}
}
}
else
{
if (ishalfpun(c3))
{
result += c3;
count+=1;
i+=2;
}
else
{
if (c3 == '\x22' || c3== '\x27')
{
result += c3;
count+=1;
i+=2;
if (c4 != '\n')
{
if (ishalfpun(c4))
{
result += c4;
count+=1;
i+=2;
}
}
else
{
if (ishalfpun(c5))
{
result += c5;
count+=1;
i+=3;
}
}
}
}
}
}
else if(c1 > '\x80')
{
if (c1 == '\xa1' && c2 == '\xa1')
{
i++;
continue;
}
result += c1;
result += c2;
count+=2;
i++;
if (c3 == '\n')
{
if (c4 == '\xa1' && c5 == '\xa3') //。
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xac') //,
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xbb') //;
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xba') //:
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xa1') //!
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if ((c4 == '\xa1' && c5 == '\xb1') || (c4 == '\xa1' && c5 == '\xaf'))//”
{
result += c4;
result += c5;
count+=2;
i+=3;
if (c6 == '\n')
{
if (ispun(c7,c8) == 1)
{
result += c7;
result += c8;
count+=2;
i+=3;
}
}
else
{
if ((a =ispun(c6,c7)) == 1)
{
result += c6;
result += c7;
count+=2;
i+=2;
}
}
}
else
{
if (c3 == '\xa3' && c4 == '\xbf') //?
{
result += c3;
result += c4;
count+=2;
i+=3;
}
else
{
if (c3 == '\xa1' && c4 == '\xb7') //》
{
result += c3;
result += c4;
count+=2;
i+=3;
}
else
{
}
}
}
}
}
}
}
}
}
else
{
if (c3 == '\xa1' && c4 == '\xa3')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xac')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xbb')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xba')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xa1')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if ((c3 == '\xa1' && c4 == '\xb1') || (c3 == '\xa1' && c4 == '\xaf'))
{
result += c3;
result += c4;
count+=2;
i+=2;
if (c5 == '\n')
{
if (ispun(c6,c7) == 1)
{
result += c6;
result += c7;
count+=2;
i+=3;
}
}
else
{
if (ispun(c5,c6) == 1)
{
result += c5;
result += c6;
count+=2;
i+=2;
}
}
}
else
{
if (c3 == '\xa3' && c4 == '\xbf')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa1' && c4 == '\xb7')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
}
}
}
}
}
}
}
}
}
}
else
{
result += c1;
count++;
}
}
}
if(i<oldlen)
result += text.charAt(i);
if(addp)
result +="</p>\n";
//"<p> "=>"<p> "
var naivete_array =result.split("<br> ");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
if(result != ""){ result = result +"<br> "+ naivete_array[loop];}
else{ result = naivete_array[loop];}
}
}
var naivete_array =result.split("<p> ");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
if(naivete_array[loop] !=""){
result = result +"<p> "+ naivete_array[loop];
}
}
}
//最后把结尾处的"<br> <br>"滤掉
var naivete_array =result.split("<br> <br>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
//最后把结尾处的"<p> </p>"滤掉
var naivete_array =result.split("<p> </p>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
//最后把结尾处的" <br>"滤掉
var naivete_array =result.split(" <br>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
/*
//最后把"http://xxxx[ ]"替换为"<a href=http://xxx>http://xxx</a>"滤掉????有点问题
var naivete_array =result.split("http:");
var mytag=0;
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
mytag=naivete_array[loop].indexOf(' ');
if(mytag<0){
mytag=naivete_array[loop].indexOf(".html");
if(mytag>0){mytag+=5;}
}
if(mytag<0){
mytag=naivete_array[loop].indexOf(".shtml");
if(mytag>0){mytag+=6;}
}
if(naivete_array[loop].substring(0,2) =='//')
{
naivete_array[loop]='http:'+naivete_array[loop];
mytag=mytag+5;
}
if(naivete_array[loop].substring(0,7) =='http://' && mytag>5 && mytag<150)
{
myurl=naivete_array[loop].substring(0,mytag);
myurl_href='<a href='+myurl+'>'+myurl+'</a>';
if(loop>0){
if(naivete_array[loop-1].substring(naivete_array[loop-1].length-1,naivete_array[loop-1].length)=='>' || naivete_array[loop-1].substring(naivete_array[loop-1].length-5,naivete_array[loop-1].length)=='href=')
{
result = result + naivete_array[loop];
}
else
{
result = result + naivete_array[loop].replace(myurl,myurl_href);
}
}
}
else{result = result + naivete_array[loop];}
}
}
*/
naivete_array =result.split("<p>");
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
naivete_array =result.split("</p>");
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
result="\n"+result
//result=result.replace(" ","");
//result=result.replace("<p>," ");
//result=result.replace("</p>,"");
return result;
}
该文章转载自德仔工作室:http://www.dezai.cn/article_show.asp?ArticleID=5187
JS实现自动排版
function ispun(c1,c2)
{
if ((c1 == '\xa1' && c2 == '\xa3') || (c1 == '\xa3' && c2 == '\xac')|| (c1 == '\xa3' && c2 == '\xbb') || (c1 == '\xa3' && c2 == '\xba') || (c1 == '\xa3' && c2 == '\xa1') || (c1 == '\xa1' && c2 == '\xb1') || (c3 == '\xa3' && c2 == '\xbf') || (c3 == '\xa1' && c2 == '\xb7'))
return 1;
return 0;
}
function ishalfpun(c2)
{
if (c2 == '\x2c' || c2 =='\x2e' || c2 =='\x3b' || c2 =='\x2e' || c2 =='\x3a' || c2 =='\x21' || c2 =='\x3f' || c2 =='\x3e')
return 1;
return 0;
}
function formattext(text,addp)
{
//首先把"<p>"、"<br>"、"</p>"、"<p> "全部滤掉
var naivete_array =text.split("<p> ");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("<p>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("<br>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
naivete_array =text.split("</p>");
if (naivete_array.length >=0)
{
text="";
for (loop=0; loop < naivete_array.length;loop++)
{
text = text + naivete_array[loop];
}
}
oldlen = text.length;
tmpstr = "";
del = '1';
for(i=0; i<oldlen; i++)
{
c1 = text.charAt(i);
if (c1 == '\r')
{
del = '1';
continue;
}
else
{
if(c1 > '\xff')
{
if ((c1 == ' ' && (del == '1')) || c1 == '\x09')
{
continue;
}
else
{
if (c1 == ' ' || c1 == '\n' || c1 == '\x09')
del = '1';
else
del = '0';
tmpstr += c1;
}
}
else
{
if(c1 > '\x80')
{
del = '1';
tmpstr += c1;
}
else
{
if ((c1 == ' ' && (del == '1')) || c1 == '\x09')
{
continue;
}
else
{
if (c1 == ' ' || c1 == '\n' || c1 == '\x09')
del = '1';
else
del = '0';
tmpstr += c1;
}
}
}
}
}
text = tmpstr;
oldlen = text.length;
tmpstr = "";
for(i=0; i<oldlen; i++)
{
c1 = text.charAt(i);
c2 = text.charAt(i+1);
c3 = text.charAt(i+2);
if (c1 == '\n' && c2 == '\n' && c3 == '\n')
continue;
tmpstr += c1;
}
text = tmpstr;
oldlen = text.length;
//result = (addp) ? "<p>" : " "; //用全角空格代替
result = (addp) ? "<p> " : " "; //用全角空格代替
count = 4;
oneretn = 0;
for(i=0; i<oldlen-1; i++)
{
c1 = text.charAt(i);
c2 = text.charAt(i+1);
c3 = text.charAt(i+2);
c4 = text.charAt(i+3);
c5 = text.charAt(i+4);
c6 = text.charAt(i+5);
c7 = text.charAt(i+6);
c8 = text.charAt(i+7);
if (c1 == '\n')
{
if (c2 == '\n')
{
if (oneretn == 1)
result += (addp) ? "</p>\n<p>" : "\n";
else
result += (addp) ? "</p>\n\n<p>" : "\n\n";
//result += " ";
result += " "; //用全角空格代替
count = 4;
i++;
oneretn = 0;
}
continue;
}
else
{
if (c1 == " " && count == 0)
{
continue;
}
else
{
if ((c1 == '\xa1' && c2 == '\xa1') && count == 0)
{
i++;
continue;
}
}
oneretn = 0;
if(c1 > '\xff')
{
result += c1;
count+=1;
if (c2 != '\n')
{
if (ishalfpun(c2))
{
result += c2;
count+=1;
i++;
}
else
{
if (c2 == '\x22' || c2== '\x27')
{
result += c2;
count+=1;
i++;
if (c3 != '\n')
{
if (ishalfpun(c3))
{
result += c3;
count+=1;
i++;
}
}
else
{
if (ishalfpun(c4))
{
result += c4;
count+=1;
i+=2;
}
}
}
}
}
else
{
if (ishalfpun(c3))
{
result += c3;
count+=1;
i+=2;
}
else
{
if (c3 == '\x22' || c3== '\x27')
{
result += c3;
count+=1;
i+=2;
if (c4 != '\n')
{
if (ishalfpun(c4))
{
result += c4;
count+=1;
i+=2;
}
}
else
{
if (ishalfpun(c5))
{
result += c5;
count+=1;
i+=3;
}
}
}
}
}
}
else if(c1 > '\x80')
{
if (c1 == '\xa1' && c2 == '\xa1')
{
i++;
continue;
}
result += c1;
result += c2;
count+=2;
i++;
if (c3 == '\n')
{
if (c4 == '\xa1' && c5 == '\xa3') //。
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xac') //,
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xbb') //;
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xba') //:
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if (c4 == '\xa3' && c5 == '\xa1') //!
{
result += c4;
result += c5;
count+=2;
i+=3;
}
else
{
if ((c4 == '\xa1' && c5 == '\xb1') || (c4 == '\xa1' && c5 == '\xaf'))//”
{
result += c4;
result += c5;
count+=2;
i+=3;
if (c6 == '\n')
{
if (ispun(c7,c8) == 1)
{
result += c7;
result += c8;
count+=2;
i+=3;
}
}
else
{
if ((a =ispun(c6,c7)) == 1)
{
result += c6;
result += c7;
count+=2;
i+=2;
}
}
}
else
{
if (c3 == '\xa3' && c4 == '\xbf') //?
{
result += c3;
result += c4;
count+=2;
i+=3;
}
else
{
if (c3 == '\xa1' && c4 == '\xb7') //》
{
result += c3;
result += c4;
count+=2;
i+=3;
}
else
{
}
}
}
}
}
}
}
}
}
else
{
if (c3 == '\xa1' && c4 == '\xa3')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xac')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xbb')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xba')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa3' && c4 == '\xa1')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if ((c3 == '\xa1' && c4 == '\xb1') || (c3 == '\xa1' && c4 == '\xaf'))
{
result += c3;
result += c4;
count+=2;
i+=2;
if (c5 == '\n')
{
if (ispun(c6,c7) == 1)
{
result += c6;
result += c7;
count+=2;
i+=3;
}
}
else
{
if (ispun(c5,c6) == 1)
{
result += c5;
result += c6;
count+=2;
i+=2;
}
}
}
else
{
if (c3 == '\xa3' && c4 == '\xbf')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
if (c3 == '\xa1' && c4 == '\xb7')
{
result += c3;
result += c4;
count+=2;
i+=2;
}
else
{
}
}
}
}
}
}
}
}
}
}
else
{
result += c1;
count++;
}
}
}
if(i<oldlen)
result += text.charAt(i);
if(addp)
result +="</p>\n";
//"<p> "=>"<p> "
var naivete_array =result.split("<br> ");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
if(result != ""){ result = result +"<br> "+ naivete_array[loop];}
else{ result = naivete_array[loop];}
}
}
var naivete_array =result.split("<p> ");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
if(naivete_array[loop] !=""){
result = result +"<p> "+ naivete_array[loop];
}
}
}
//最后把结尾处的"<br> <br>"滤掉
var naivete_array =result.split("<br> <br>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
//最后把结尾处的"<p> </p>"滤掉
var naivete_array =result.split("<p> </p>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
//最后把结尾处的" <br>"滤掉
var naivete_array =result.split(" <br>");
if (naivete_array.length >1)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
/*
//最后把"http://xxxx[ ]"替换为"<a href=http://xxx>http://xxx</a>"滤掉????有点问题
var naivete_array =result.split("http:");
var mytag=0;
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
mytag=naivete_array[loop].indexOf(' ');
if(mytag<0){
mytag=naivete_array[loop].indexOf(".html");
if(mytag>0){mytag+=5;}
}
if(mytag<0){
mytag=naivete_array[loop].indexOf(".shtml");
if(mytag>0){mytag+=6;}
}
if(naivete_array[loop].substring(0,2) =='//')
{
naivete_array[loop]='http:'+naivete_array[loop];
mytag=mytag+5;
}
if(naivete_array[loop].substring(0,7) =='http://' && mytag>5 && mytag<150)
{
myurl=naivete_array[loop].substring(0,mytag);
myurl_href='<a href='+myurl+'>'+myurl+'</a>';
if(loop>0){
if(naivete_array[loop-1].substring(naivete_array[loop-1].length-1,naivete_array[loop-1].length)=='>' || naivete_array[loop-1].substring(naivete_array[loop-1].length-5,naivete_array[loop-1].length)=='href=')
{
result = result + naivete_array[loop];
}
else
{
result = result + naivete_array[loop].replace(myurl,myurl_href);
}
}
}
else{result = result + naivete_array[loop];}
}
}
*/
naivete_array =result.split("<p>");
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
naivete_array =result.split("</p>");
if (naivete_array.length >=0)
{
result="";
for (loop=0; loop < naivete_array.length;loop++)
{
result = result + naivete_array[loop];
}
}
result="\n"+result
//result=result.replace(" ","");
//result=result.replace("<p>," ");
//result=result.replace("</p>,"");
return result;
}
该文章转载自德仔工作室:http://www.dezai.cn/article_show.asp?ArticleID=5187