第一次发布还失败了,原因是代码里有coolcode标签,结果让编辑器给替换了 :( 再发一次,这次把coolcode标签替换成mycodes,实际使用中要替换回来。

今天为WP增加了一个插件CoolCode,作用就是让内容里的代码能够高亮显示,经常写些程序代码,但是以前只是普通的文字,没有相应的颜色,有了这个插件,写出来的代码加上颜色,就好看多了。

经过测试,在编辑器Visual模式下写的代码不能被正常的的解析,只有在HTML模式下写出来的代码才能被正常的解析到。然而每次都要写上<mycodes lang=”javascript”></mycodes>,虽然字数不多,但是像这样简单的代码就懒得写了,于是,决定在HTML模式下,增加一个快捷按钮,就像已有的link,code一样,点击一下,输入几个参数就可以了。

CoolCode有三个参数,lang,指定被包含的程序代码是哪种语言,支持actionscript cpp css diff dtd html java javascript mysql perl php python ruby sql xml;linenum,指定是否显示行号,值是true或者false,默认是true;download,指定要下载的文件的路径加文件名。这三个参数都是可选的。了解了这些,就开始写代码了。

首先是在“\wp-includes\js\quicktags.js”里进行修改,
添加要显示的按钮:

  1. edButtons[edButtons.length] =
  2. new edButton('ed_coolcode'
  3. ,'mycodes'
  4. ,''
  5. ,'&lt;/mycodes&gt;'
  6. );

将这个按钮增加到“edShowButton”函数里:

  1. function edShowButton(button, i) {
  2.     if (button.id == 'ed_img') {
  3.         document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertImage(edCanvas);" value="' + button.display + '" />');
  4.     }
  5.     else if (button.id == 'ed_link') {
  6.         document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertLink(edCanvas, ' + i + ');" value="' + button.display + '" />');
  7.     }
  8.     else if(button.id == 'ed_coolcode') {
  9.         document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertCoolCode(edCanvas, ' + i + ',true);" value="' + button.display + '" />');
  10.     }
  11.     else {
  12.         document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" class="ed_button" onclick="edInsertTag(edCanvas, ' + i + ');" value="' + button.display + '"  />');
  13.     }
  14. }

因为这个按钮需要接收弹出框的信息,所以要单独写。 Read More…

Tags:
Posted in keyboard's joy at July 16th, 2008. No Comments.