📄 evahtmlparser.cpp
字号:
/*************************************************************************** * Copyright (C) 2005 by yunfan * * yunfan_zg@163.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/#include "evahtmlparser.h"#include "evapacket.h"#include <arpa/inet.h>#include <qregexp.h>#include <qfile.h>#include <qpixmap.h>unsigned int EvaHtmlParser::tmpNum = 0;std::list<CustomizedPic> EvaHtmlParser::convertToHtml(QString &txt, bool isURLOn, bool isAbsImgPath, bool useRealFileName){ // FIXME by this way, some html code may not be displayed properly txt.replace("&", "&amp;"); txt.replace("<", "<lt;"); txt.replace(">", ">gt;"); txt.replace(""", ""quot;"); txt.replace("/<D>", "/dd"); txt.replace("/<J>", "/mn"); txt.replace("/<H>", "/hl"); txt.replace("/<M>", "/mamao"); txt.replace("/<QQ>", "/qz"); txt.replace("/<L>", "/fw"); txt.replace("/<O>", "/oh"); txt.replace("/<B>", "/bj"); txt.replace("/<U>", "/qsh"); txt.replace("/<W>", "/xig"); txt.replace("/<!!>", "/xy"); txt.replace("/<~>", "/duoy"); txt.replace("/<Z>", "/xr"); txt.replace("/<*>", "/xixing"); txt.replace("/<00>", "/nv"); txt.replace("/<11>", "/nan"); txt.replace("<", "<"); txt.replace(">", ">"); txt.replace("\"", """); txt.replace("\n", "<br>"); QRegExp rx2("/(:o|jy|:~|pz|:\\*|se|:\\||fd|dy|:<|ll|:$|hx|:x|bz|:z|shui|:'|dk" "|:\\-\\||gg|:@|fn|:P|tp|:D|cy|:\\)|wx|:\\(|ng|:\\+|kuk|:#|feid" "|:Q|zk|:t|tu|;P|tx|;\\-D|ka|;d|baiy|;o|am|:g|jie|\\|\\-\\)|kun" "|:\\!|jk|:L|lh|:\\>|hanx|:;|db|;f|fendou|:\\-S|zhm|\\?|yiw" "|;x|xu|;@|yun|:8|zhem|;\\!|shuai|\\!\\!\\!|kl|xx|qiao|bye|zj" "|go|shan|shake|fad|love|aiq|jump|tiao|find|zhao|&|mm|pig" "|zt|cat|maom|dog|xg|hug|yb|$|qianc|\\(\\!\\)|dp|cup|bei" "|cake|dg|li|shd|bome|zhd|kn|dao|footb|zq|music|yy|shit" "|bb|coffee|kf|eat|fa|pill|yw|rose|mg|fade|dx|kiss|wen" "|heart|xin|break|xs|meeting|hy|gift|lw|phone|dh|time|sj" "|email|yj|tv|ds|sun|ty|moon|yl|strong|qiang|weak|ruo" "|share|ws|v|shl|dd|mn|hl|mamao|qz|fw|oh|bj|qsh|xig|xy|duoy" "|xr|xixing|nv|nan)"); int pos=0; QString smiley; int fileIndex; QString img; while(pos>=0){ pos = rx2.search(txt, pos); if (pos > -1) { smiley = rx2.cap(0); QChar suffix = txt[pos + smiley.length() ]; if(!suffix.isNull() && ( (suffix >= 'a' && suffix <= 'z' ) || (suffix >= 'A' && suffix <= 'Z' ) )){ pos += smiley.length(); continue; } fileIndex = EvaUtil::textToFileIndex(smiley.ascii()); if(fileIndex!=-1){ img = "<img src=\""+ (isAbsImgPath?(absImagePath+"/"):"") + QString::number(fileIndex)+".gif\">"; txt.replace(pos, smiley.length(), img); pos += img.length(); }else pos += smiley.length(); } } std::list< CustomizedPic > picList = convertCustomizedPictures(txt, useRealFileName); if(isURLOn){ QString email; QString wraped; QString aRefBegin = "<a href=\""; QString aRefEnd = "</a>"; QString emailTag = "mailto:"; QRegExp rx3("\\b[a-z0-9._%-]+@[a-z0-9._%-]+\\.[a-z]{2,4}\\b"); rx3.setCaseSensitive(false); pos = 0; while(pos>=0){ pos = rx3.search(txt, pos); if (pos > -1) { email = rx3.cap(0); email.stripWhiteSpace(); wraped = aRefBegin + emailTag + email + "\">" + email + aRefEnd; txt.replace(pos, strlen(email.ascii()), wraped); pos += strlen(wraped.ascii()); } } QString url; QRegExp rx4("(http|ftp)://[a-z0-9._%-]+\\.[a-z0-9._%\\-]+\\.[a-z0-9]{1,4}[a-z0-9._%\\-/\\?=~&#]*"); rx4.setCaseSensitive(false); pos = 0; while(pos>=0){ pos = rx4.search(txt, pos); if (pos > -1) { url = rx4.cap(0); url.stripWhiteSpace(); wraped = aRefBegin + url + "\">" + url + aRefEnd; txt.replace(pos, strlen(url.ascii()), wraped); pos += strlen(wraped.ascii()); } } } bool isIgnore = false; for(pos = 0; pos < (int)txt.length(); pos ++ ){ QChar ch = txt.at(pos);//printf("ch(%d): %c\t", pos, ch.latin1()); if(ch == '<') { isIgnore = true; continue; } if(ch == '>') { isIgnore = false; continue; } if(ch == ' ' && !isIgnore && isAbsImgPath){ txt.replace(pos, 1, " "); pos+= (QString(" ").length() -1 ); } if(ch == '\t' && !isIgnore && isAbsImgPath){ txt.replace(pos, 1, " "); pos+= (QString(" ").length() - 1); } } return picList;}// for personal chatting onlyconst int EvaHtmlParser::convertToPlainTxt(QString &html, QString &sendFileNameBase){ QRegExp rx("<img src=\\d\\d?\\.gif >"); QRegExp rx1("\\d\\d?"); QRegExp picRx("<img src=[a-zA-Z0-9_/\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG) >"); QRegExp picRx1("[a-zA-Z0-9_\\.\\s\\-\\{\\}\\[\\]\\(\\)]{10,100}\\.(jpg|JPG|gif|GIF|bmp|BMP|jpeg|JPEG)"); int pos=0; QString imgTxt, strFile, smiley; int fileIndex, len; while(pos>=0){ pos = rx.search(html, pos); if (pos > -1) { imgTxt = rx.cap(0); len = rx.matchedLength(); int p=0; p = rx1.search(imgTxt, 0); if(p>-1) strFile = rx1.cap(0); fileIndex = strFile.toInt(); if(fileIndex<0 || fileIndex > 95) { pos += len; continue; } smiley = QString(EvaUtil::fileIndexToText(fileIndex).c_str()) + " "; html.replace(imgTxt, smiley); pos += smiley.length(); } } pos =0; int numImage = 0; sendFileNameBase = ""; while(pos>=0){ pos = picRx.search(html, pos); if(pos > -1) { imgTxt = picRx.cap(0); len = picRx.matchedLength(); int p=0; p = picRx1.search(imgTxt, 0); if(p>-1){ strFile = picRx1.cap(0); numImage++; if(numImage == 1) sendFileNameBase = strFile.left(strFile.length() - 4);; } smiley = generateSendFormat32(sendFileNameBase, strFile, numImage); html.replace(imgTxt, smiley); pos += smiley.length(); } } QString start = "<p>"; pos = html.find(start, 0); if(pos!= -1){ html = html.right(html.length() - pos - start.length()); } html.replace("<br /></p>\n</body></html>", ""); html.replace("</p>\n</body></html>", ""); html.replace("\n",""); html.replace("<br>", "\n"); html.replace("<p>", ""); html.replace("<br />","\n"); html.replace("</p>", "\n"); html.replace("&", "&"); html.replace("<", "<"); html.replace(">", ">"); html.replace(""", "\""); html.replace(" ", " "); return numImage;}// imgNo starts from 1QString EvaHtmlParser::generateSendFormat32(const QString sendFileNameBase, const QString &file, const int imgNo){ QPixmap pix(absCustomCachesPath + "/" + file); pix.save(absCustomCachesPath + "/" + sendFileNameBase + QString::number(imgNo - 1) + ".jpg", "JPEG", 100); //QFile info(absCustomCachesPath + "/" + file); //info.remove(); QString ext = file.right(4); QString extCode; if(ext == ".jpg") extCode = "A"; else if(ext == ".gif") extCode = "C"; else extCode = "A"; QChar code = 0x40 + imgNo; // note that imgNo starts from 1 QString noCode = code; QString contents; if(imgNo == 1) // for the first occurred we put its name here contents = sendFileNameBase + QString::number(imgNo - 1) + ".jpg"; contents += (extCode + noCode); return "[ZDY][32]"+contents+"[/32][/ZDY]";}std::list< CustomizedPic > EvaHtmlParser::convertCustomizedPictures( QString & text, bool useRealFileName){ QRegExp rx("\\[ZDY\\]\\[[0-9][0-9]\\][^/]+\\[/[0-9][0-9]\\]\\[/ZDY\\]"); int pos=0; QString contents; QString img; int type; while(pos>=0){ pos = rx.search(text, pos); if (pos > -1) { contents = rx.cap(0); bool ok; type = contents.mid(6,2).toInt(&ok); CustomizedPic args; args.type = 0; // 0 means error; args.isExisted = false; if(ok){ switch(type){ case 32: img = processPic32(contents, &args); break; case 33: img = processPic33(contents, &args); tmpNum++; break; case 34: img = processPic34(contents); break; case 36: img = processPic36(contents, &args); tmpNum++; break; case 37: img = processPic37(contents); break; default: printf("unknown code: %d\n", type); img = "<img src=\"" + ((absCustomCachesPath)?absCustomCachesPath:"~/.eva/customCaches")+"/unknown.png\">"; } if(useRealFileName){ text.replace(contents, "<img src=\"" + args.fileName+ "\">"); pos += args.fileName.length(); }else{ text.replace(contents, img); pos += img.length(); } if(type == 33 || type == 36) picList.push_back(args); } else pos+=contents.length(); } } std::list< CustomizedPic > retList; std::list< CustomizedPic >::iterator iter; for(iter=picList.begin(); iter!=picList.end(); ++iter){ if(!iter->isExisted) retList.push_back(*iter); } return retList;}// for 0x32QString EvaHtmlParser::processPic32( const QString &src, CustomizedPic * args ){ QString imgName = src.mid(9, src.length()-9-11);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -