给Micolog加QQ表情评论功能
前几天看到六维博客里看到了一篇博文WP博客评论区加入QQ表情,此功能很酷,想了想就移植到Micolog了,本人不懂php,只是解剖了他的源代码,稍加改造,并加上前几天研究如何给最新评论增加过滤功能的积累,就很简单的实现了。没写过插件,只是改造了代码也不知能不能叫插件!
1.首先下载 QQ表情包
上传到你的博客程序的image下,如果你有大菠萝相册,用GAE上传程序直接上传到大菠萝程序的static下。这样可以起到分流的作用!
2.在留言输入框附近 加如下代码:
<p>
<script type="text/javascript">/*<![CDATA[*/function grin(tag) {
if (typeof tinyMCE != 'undefined') {
grin_tinymcecomments(tag);
} else {
grin_plain(tag);
}
}
function grin_tinymcecomments(tag) {
tinyMCE.execCommand('mceInsertContent', false, ' ' + tag + ' ');
}
function grin_plain(tag) {
var myField;
var myCommentTextarea = "comment";
tag = ' ' + tag + ' ';
if (document.getElementById(myCommentTextarea) && document.getElementById(myCommentTextarea).type == 'textarea') {
myField = document.getElementById(myCommentTextarea);
} else {
return false;
}
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = tag;
myField.focus();
}
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var cursorPos = endPos;
myField.value = myField.value.substring(0, startPos)
+ tag
+ myField.value.substring(endPos, myField.value.length);
cursorPos += tag.length;
myField.focus();
myField.selectionStart = cursorPos;
myField.selectionEnd = cursorPos;
}
else {
myField.value += tag;
myField.focus();
}
}
function moreSmilies() {
document.getElementById('wp-smiley-more').style.display = 'inline';
document.getElementById('wp-smiley-toggle').innerHTML = '<a href="javascript:lessSmilies()">« less</a></span>';
}
function lessSmilies() {
document.getElementById('wp-smiley-more').style.display = 'none';
document.getElementById('wp-smiley-toggle').innerHTML = '<a href="javascript:moreSmilies()">more »</a>';
}/*]]>*/</script>
<img src='http://hikeimg.appspot.com/static/images/icon/icon_smile.gif' alt=':)' onclick='grin("^~smile~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_lol.gif' alt='lol' onclick='grin("^~lol~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_cool.gif' alt='8)' onclick='grin("^~cool~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_surprised.gif' alt=':-o' onclick='grin("^~surprised~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_mrgreen.gif' alt='mrgreen' onclick='grin("^~mrgreen~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_evil.gif' alt='evil' onclick='grin("^~evil~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_arrow.gif' alt='arrow' onclick='grin("^~arrow~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_sad.gif' alt=':(' onclick='grin("^~sad~^")' class='wp-smiley-select' /> <span id="wp-smiley-more" style="display:none"><img src='http://hikeimg.appspot.com/static/images/icon/icon_wink.gif' alt='wink' onclick='grin("^~wink~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_neutral.gif' alt=':-|' onclick='grin("^~neutral~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_mad.gif' alt=':-x' onclick='grin("^~mad~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_twisted.gif' alt='twisted' onclick='grin("^~twisted~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_eek.gif' alt='8-O' onclick='grin("^~eek~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_rolleyes.gif' alt='roll' onclick='grin("^~rolleyes~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_razz.gif' alt=':-P' onclick='grin("^~razz~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_redface.gif' alt='oops' onclick='grin("^~redface~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_idea.gif' alt='idea' onclick='grin("^~idea~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_biggrin.gif' alt=':-D' onclick='grin("^~biggrin~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_cry.gif' alt='cry' onclick='grin("^~cry~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_confused.gif' alt=':-?' onclick='grin("^~confused~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_question.gif' alt='?' onclick='grin("^~question~^")' class='wp-smiley-select' /> <img src='http://hikeimg.appspot.com/static/images/icon/icon_exclaim.gif' alt='!' onclick='grin("^~exclaim~^")' class='wp-smiley-select' /> </span> <span id="wp-smiley-toggle"><a href="javascript:moreSmilies()">more »</a></span>
</p>
将http://hikeimg.appspot.com/static/images/icon/icon_exclaim.gif 的红色部分的路径替换成您上传QQ表情的路径。过几天此路径可能失效!下面遇到同样的路径也要替换!
3.在blog.py的大概279行,增加红色部分。
if not (name and email and content):
if useajax:
self.write(simplejson.dumps((False,-101,_('Please input name, email and comment .'))))
else:
self.error(-101,_('Please input name, email and comment .'))
else:
comment=Comment(author=name,
content=content.replace('^~',"<img src=http://hikeimg.appspot.com/static/images/icon/icon_").replace('~^','.gif />'),
email=email,
entry=Entry.get(key))
if url:
try:
comment.weburl=url
except:
comment.weburl='http://'+url
4.为了使首页的最新评论不显示表情或图片,要做过滤。打开model.py,大概在464行。代码修改如下,复制红色部分即可。主要[:len]的位置有所改变!
@property
def shortcontent(self,len=24): #self.content[:len].replace
scontent=self.content.replace('<br />',' ').replace('<img src=http://hikeimg.appspot.com/static/images/icon/icon_','Icon(').replace('.gif />',')').replace('<','<').replace('>','>')[:len]
return scontent
OK,完成。自己发个表情试试
转载注明来自 HOUKAI.COM

笑的好酷
,很好的功能~呵呵
不错,houkai越来越牛了。。。
(⊙o⊙)哦,我也不知道咋回事,是不是过滤出问题了?我也想游泳
就是没地方和钱……
@Qing 那就赶快加上呀 现在我不用表情都感觉不习惯了
@蓝冰 这叫站在巨人的肩上就会看得更远 跑的更快
都连接到别人的网站上去了
测试回复引用 这个做测试 因为 以前的图片路径和现在有点不同
测试回复引用 这个做测试 因为 以前的图片路径和现在有点不同
【引用 houkai】

——————————————————————
自己先测试一下
都连接到别人的网站上去了【引用 卢松松】
——————————————————————
没关系了 为了表扬此同学了
功能更强了
前几天我也尝试弄了一下,不过出了点问题..就放弃了
这个插件很好,有时间加上,不知再加个分页样式功能难不难,不用前一篇后一篇的,如果页数多,我想操作就不方便了!
@eDIKUD
分页不是很难 有空我试试吧
插件发送到你邮箱了~!
@蓝冰
非常感谢!
都连接到别人的网站上去了【引用 卢松松】
——————————————————————
老卢,连我网站也没什么错呀·!
@蓝冰
奇怪了 只能记住本页的
我准备把这些都写在js里面,只要加载js文件就可以了
你的评论也很多哦
@Qing
我已经用调用js了,可以看看我的源码
@Qing

我已经用调用js了,可以看看我的源码 <img src="http://img.houkai.com/static/i/i_lol_h.gif />【引用 houkai】
——————————————————————
我全部放在JS里面了。。。
@Qing
【引用 houkai】

我已经用调用js了,可以看看我的源码
——————————————————————
@各种虾米
没有呀 春哥的表情这这些比起来太大 不和谐
不错!
做的真好,多谢,抽时间我也加上
不错~
找个时间加上!!
怎么首页的最新评论的代码添加上显示不正常呢?
怎么首页的最新评论的代码添加上显示不正常呢?【引用 麦子】
——————————————————————
去看看,好像没什么问题呀
要把20改为24?
model.py里不是:
@property
def shortcontent(self,len=24): #self.content[:len].replace
return scontent
而是这样的啊:
@property
def shortcontent(self,len=20):
return self.content[:len]
所以只是复制红字部分进去就行?
@麦子
我觉得我两行显示评论 20有点少。是红色部分。就把那部分改掉,不全是红色部分。#self.content[:len].replace 是我以前改动之前的 我只是想留个备份。
用return scontent 换掉return self.content[:len] 就ok了。 因为字数在前面已经做了限制。后面就不用再限制了。
@houkai
晕哦!怎么这样?
: expected an indented block (model.py, line 452)
Traceback (most recent call last):
File "/base/data/home/apps/at-blog/2.335701177724283491/blog.py", line 19, in
from base import *
File "/base/data/home/apps/at-blog/2.335701177724283491/base.py", line 17, in
from model import *: expected an indented block (model.py, line 452)
OK!好了!
谢了!等海缆恢复,我跟你申请友链哈
非常感谢!赞一个……
@叶子
我这里没问题 还是上面的麦子昨天刚加上也没问题。你看你下载的是不是micolog最新版,或者在版本控制里对照一下。
最新的(⊙o⊙),估计跟其他的东西冲突了,原先用svn也没问题
后来偷懒用别人弄好的自己小修改一下,发现表情出问题,没法return,改回self.content没问题,估计是返回时scontent没被正常认到
@叶子
那你就这样
self.content=self.content.replace('……略。
return self.content
试试看行不行。
<img src="http://img.houkai.com/static/i/i_mrgreen_h.gif /> 就是这样解决的,你不是看到了么【引用 叶子 】
看看你的会不会触发bug
——————————————————————
这个我很早就知道了 用火狐就会这样 我也知道怎么改。暂时先放着吧。因为我打算大改一下。
如此说来我就不改了,等你大改之后看看效果
赞一下辛勤工作
@叶子
这次改动前面的效果可能变化不大,主要是后台和前台的细节问题。最近改的有问题了。小改之后都不能上传,有bug
我加好代码后,在首页那里看到的是
',')').replace('','>')[:len]

Icon(lol)...
我用的代码是
@property
def shortcontent(self,len=24):
scontent=self.content.replace('
',' ').replace('
return scontent
@我
在第四点的 红色部分代码里 吧Icon删掉就可以了 'Icon(').replace换成 '(').replace
@houkai
博主这么晚还在哈。非常感谢,删掉Icon后和你的现实效果一样了。
好困,睡觉去了。
博主的这个评论功能整体很炫呀,能告知下修改的思路嘛~O(∩_∩)O谢谢
@阿拉
可以看我的源码 修改了评论的css 以及用了点js.评论框部分在这xheditor.googlecode.com下载
为什么我用旧版和新版XHEDITOR发评论的时候,都无法使用中划线、背景色等标签?而且发布评论后,不会立即显示,要等下一个评论后,才显示前一个评论。
看看你这个会不会有这种问题,哈哈!
@takeasy
是不是标签被过滤掉啦?
不会立即显示应该是启用缓存的原因吧
@云在天边
好像不是吧,利用XHEDITOR可以发图片的,且可以使用字体加粗、斜体和下划线标签,字号标签偶尔也好使,但其它标签就完全无效了,怀疑是编辑器本身问题或权限不够?HOUKAI的这个也有同样问题,我更换了最新的版本,还是没有解决,可利用内建的XHEDITOR编辑器,就可以使用所有标签,所以怀疑是权限或提交问题。
@takeasy
我在后台还做了部分过滤 比如三个回车 我会替换成一个回车。至于其他我也没细究,评论框的权限是少一些,比如不能用JavaScript。
我试试中划线
@houkai
filter把大部分以<**>结构的css标签给过滤掉了。默认<font>标签也是不允许的
额,使用了编辑器后,竟然没有转义