打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
dokuwiki将编辑器修改为可视化,并支持代码高亮和QQ截图拷贝


1:Dokuwiki环境搭建


1.1:Dokuwiki自带安装文件

安装文件入口:/install.php
界面安装很方便

1.2:Dokuwiki自带zh-cn包


2:直接拷贝图片到编辑器


2.1:自带编辑器介绍

自带编辑器不支持所见所得,依靠一些标签来建立目录和页面排榜,尽管标签很强大,但是对于初学者或者不熟悉的人来说,比较麻烦.

2.2:更换流程及需求分析思考

思考: 要将编辑器改为所见所得,那么就两种方法,修改原先的编辑器或者更换编辑器.
流程:
2.2.1:去网上插件模板找找,是否存在这样的编辑器
2.2.2:更换为其他编辑器
2.2.3:修改原先的编辑器
总结:
我去网上找了,界面的插件倒确实不少,甚至有把FCK作为插件整合进来的,但是依然缺少一个功能就是将图片拷贝到编辑器里.
如果是自己编写这样的编辑器,显然代价太高最终决定是更换编辑器

2.3:更换编辑器为xheditor

2.3.1:将xheditor下载下来,并放入dokuwiki目录下的/lib文件夹下,新建一个目录叫xheditor-1.1.14(目前最新版本为1.1.14)
2.3.2:替换/inc/form.php里的函数form_wikitext(attrs)

源程序:

[php] view plaincopyprint?
  1. <span style="font-size:14px;">function form_wikitext($attrs) {  
  2. // mandatory attributes  
  3. unset($attrs['name']);  
  4. unset($attrs['id']);  
  5. return '<textarea name="wikitext" id="wiki__text" '  
  6. .buildAttributes($attrs,true).'>'.DOKU_LF  
  7. .formText($attrs['_text'])  
  8. .'</textarea>';  
  9. }</span>  


替换程序:

[php] view plaincopyprint?
  1. function form_wikitext($attrs) {  
  2. // mandatory attributes  
  3. unset($attrs['name']);  
  4. unset($attrs['id']);  
  5. return '<textarea id="elm1" rows="15" cols="80" style="width:100%" name="wikitext">'.DOKU_LF  
  6. .formText($attrs['_text'])  
  7. .'</textarea>';  
  8. }  


即:采用xheditor编辑器.

2.3.3:在/lib/tpl/dokuwiki/main.php添加xheditor包

[html] view plaincopyprint?
  1. <script type="text/javascript" src="<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/xheditor-1.1.14-zh-cn.min.js"></script>  

2.3.4.在/inc/parser/xhtml.php里更改cdata函数

源程序:
[php] view plaincopyprint?
  1. function cdata($text) {  
  2. $this->doc .= $this->_xmlEntities($text);  
  3. }  
替换程序:
[php] view plaincopyprint?
  1. function cdata($text) {  
  2. $this->doc.=$text;  
  3. }  
替换原因是:因为以前是纯字符编辑器,会将一些特殊符号进行过滤,比如:<>等等.而替换之后的xheditor本身已经做了一次过滤了,再次过滤就会导致字符<变成&lt,因此去掉这段之后,就只过滤一次

2.3.5.添加js代码

[php] view plaincopyprint?
  1. $(function(){  
  2. $('#elm1').xheditor({  
  3. localUrlTest:/^https?:\/\/[^\/]*?(xheditor\.com)\//i,  
  4. remoteImgSaveUrl:'<?php echo DOKU_BASE;?>lib/xheditor1-saveremoteimg.php?prefix=<?php echo DOKU_BASE;?>'  
  5. });  
  6. });  


配置php后台上传路径

2.3.6:配置php截图上传代码:

/lib/xheditor-1.1.14/demos/saveremoteimg.php.在文件底部修改代码:
源程序:
[php] view plaincopyprint?
  1. for($i=0;$i<$urlCount;$i++){  
  2. $localUrl=saveRemoteImg($arrUrls[$i]);  
  3. if($localUrl)$arrUrls[$i]=$localUrl;  
  4. }  
  5. echo implode('|',$arrUrls);  


替换程序:
[php] view plaincopyprint?
  1. for($i=0;$i<$urlCount;$i++){  
  2. $localUrl=saveRemoteImg($arrUrls[$i]);  
  3. if($localUrl)$arrUrls[$i]=$localUrl;  
  4. }  
  5. foreach($arrUrls as $key=>$vo){  
  6. $arrUrls[$key]=$_GET['prefix'].'lib/xheditor-1.1.14/demos/'.$vo;  
  7. }  
  8. echo implode('|',$arrUrls);  

2.3.7:将上传的图片去掉多余的”符号

图片上传,发布之后.在调用图片时,会多一个中文”符号,需要修改语言包/inc/lang/zh/lang.php
源程序:
[php] view plaincopyprint?
  1. $lang['doublequoteopening'] = '“';  
  2. $lang['doublequoteclosing'] = '”';  


替换程序:
[php] view plaincopyprint?
  1. $lang['doublequoteopening'] = '';  
  2. $lang['doublequoteclosing'] = '';  


3:编辑器新加插入代码功能


3.1:/lib/tpl/dokuwiki/main.php更新js代码

同之前的上传整合起来:
新增程序:
[javascript] view plaincopyprint?
  1. <script type="text/javascript">  
  2.        var editor;  
  3.         jQuery(pageInit);  
  4.         function pageInit()  
  5.         {  
  6.             var allPlugin={  
  7.                 Code:{c:'btnCode',t:'插入代码',h:1,e:function(){  
  8.                     var _this=this;  
  9.                     var htmlCode='<div><select id="xheCodeType"><option value="html">HTML/XML</option><option value="js">Javascript</option><option value="css">CSS</option><option value="php">PHP</option><option value="java">Java</option><option value="py">Python</option><option value="pl">Perl</option><option value="rb">Ruby</option><option value="cs">C#</option><option value="c">C++/C</option><option value="vb">VB/ASP</option><option value="">其它</option></select></div><div><textarea id="xheCodeValue" wrap="soft" spellcheck="false" style="width:300px;height:100px;" /></div><div style="text-align:right;"><input type="button" id="xheSave" value="确定" /></div>';           
  10.                     var jCode=jQuery(htmlCode),jType=jQuery('#xheCodeType',jCode),jValue=jQuery('#xheCodeValue',jCode),jSave=jQuery('#xheSave',jCode);  
  11.                     jSave.click(function(){  
  12.                         _this.loadBookmark();  
  13.                         _this.pasteHTML('<pre class="prettyprint lang-'+jType.val()+'">'+_this.domEncode(jValue.val())+'</pre>');  
  14.                         _this.hidePanel();  
  15.                         return false;     
  16.                     });  
  17.                     _this.saveBookmark();  
  18.                     _this.showDialog(jCode);  
  19.                 }}  
  20.             };  
  21.             editor=jQuery('#elm1').xheditor({  
  22.                 plugins:allPlugin,  
  23.                 tools:'Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Anchor,Img,Flash,Media,Hr,Emot,Table,|,Source,Print,Fullscreen,Code',  
  24.                 loadCSS:'<style>pre{margin-left:2em;border-left:3px solid #CCC;padding:0 1em;}</style>',  
  25.                 localUrlTest:/^https?:\/\/[^\/]*?(xheditor\.com)\//i,  
  26.                 remoteImgSaveUrl:'lib/xheditor-1.1.14/demos/saveremoteimg.php?prefix=<?php echo DOKU_BASE;?>',  
  27.                 upLinkUrl:"<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/upload.php?immediate=1&prefix=<?php echo DOKU_BASE;?>",  
  28.                 upLinkExt:"zip,rar,txt",  
  29.                 upImgUrl:"<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/upload.php?immediate=1&prefix=<?php echo DOKU_BASE;?>",  
  30.                 upImgExt:"jpg,jpeg,gif,png",  
  31.                 upFlashUrl:"<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/upload.php?immediate=1&prefix=<?php echo DOKU_BASE;?>",  
  32.                 upFlashExt:"swf",  
  33.                 upMediaUrl:"<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/upload.php?immediate=1&prefix=<?php echo DOKU_BASE;?>",  
  34.                 upMediaExt:"wmv,avi,wma,mp3,mid"  
  35.             });  
  36.         }  
  37.     </script>  

3.2加入css和图片

3.2.1:css因为只有一句话,只是为了新增”插入代码”功能的一个小图标而来,因此,可以选择把这个css插入其他css里,而不必新建一个文件.我选择插入的css是:/lib/ style/screen.css:新增css:
[css] view plaincopyprint?
  1. .btnCode {  
  2. background:transparent url(../images/code.gif) no-repeat 16px 16px;  
  3. background-position:2px 2px;}  


3.2.2:将小图标 按照css添加路径放入对应css同级目录下的images文件夹内,作为插件插入代码的小图标.

4:编辑器代码高亮功能


4.1:引入google插件prettify.js插件

在/lib/tpl/dokuwiki/main.php里引入prettify.js和对应的css
[html] view plaincopyprint?
  1. <script type="text/javascript" src="<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/prettify/prettify.js"></script>  
  2. <link href="<?php echo DOKU_BASE;?>lib/xheditor-1.1.14/demos/prettify/prettify.css" type="text/css" rel="stylesheet">  

4.2:调用prettify.js

这里需要注意,调用prettify.js需要放在xheditor编辑器textarea的后面,放前面会无效.
新增js代码:
[php] view plaincopyprint?
  1. <?php tpl_content() ?>  
  2. <script type="text/javascript">prettyPrint();</script>  

4.3:修改css文件

引入代码高亮插件后,<pre>标签在dokuwiki下,会嵌套,导致样式会出现两个边框.
在prettify.css最后面,新增css
新增css文件:
[css] view plaincopyprint?
  1. pre pre{box-shadow:none;border:0px;margin:0;padding:0;}  
  2. span{font-style:normal;}  

4.4:修改”/”符号出现

原本的dokuwiki里,文本会过滤掉/符号(这个我试过了,即便是最原始的安装好dokuwiki后,在编辑器里只要存在”/”符号,都会被过滤掉.根据wiki语法文档,/会被认为是字体斜体的标志)
而我们的代码里,经常会出现”//”代表注释,因此,需要修改dokuwiki代码.
修改文件/inc/parser/lexer.php:
源程序:
[php] view plaincopyprint?
  1. function split($subject, &$split) {  
  2. if (count($this->_patterns) == 0) {  
  3. return false;  
  4. }  
  5. if (! preg_match($this->_getCompoundedRegex(), $subject, $matches)) {  
  6. if(function_exists('preg_last_error')){  
  7. $err = preg_last_error();  
  8. switch($err){  
  9. case PREG_BACKTRACK_LIMIT_ERROR:  
  10. msg('A PCRE backtrack error occured. Try to increase the pcre.backtrack_limit in php.ini',-1);  
  11. break;  
  12. case PREG_RECURSION_LIMIT_ERROR:  
  13. msg('A PCRE recursion error occured. Try to increase the pcre.recursion_limit in php.ini',-1);  
  14. break;  
  15. case PREG_BAD_UTF8_ERROR:  
  16. msg('A PCRE UTF-8 error occured. This might be caused by a faulty plugin',-1);  
  17. break;  
  18. case PREG_INTERNAL_ERROR:  
  19. msg('A PCRE internal error occured. This might be caused by a faulty plugin',-1);  
  20. break;  
  21. }  
  22. }  
  23.   
  24. $split = array($subject, "", "");  
  25. return false;  
  26. }  
  27.   
  28. $idx = count($matches)-2;  
  29. list($pre, $post) = preg_split($this->_patterns[$idx].$this->_getPerlMatchingFlags(), $subject, 2);  
  30.   
  31. $split = array($pre, $matches[0], $post);  
  32.   
  33. return isset($this->_labels[$idx]) ? $this->_labels[$idx] : true;  
  34. }  



替换程序:
[php] view plaincopyprint?
  1. function split($subject, &$split) {  
  2. if (count($this->_patterns) == 0) {  
  3. return false;  
  4. }  
  5. if (! preg_match($this->_getCompoundedRegex(), $subject, $matches)) {  
  6. if(function_exists('preg_last_error')){  
  7. $err = preg_last_error();  
  8. switch($err){  
  9. case PREG_BACKTRACK_LIMIT_ERROR:  
  10. msg('A PCRE backtrack error occured. Try to increase the pcre.backtrack_limit in php.ini',-1);  
  11. break;  
  12. case PREG_RECURSION_LIMIT_ERROR:  
  13. msg('A PCRE recursion error occured. Try to increase the pcre.recursion_limit in php.ini',-1);  
  14. break;  
  15. case PREG_BAD_UTF8_ERROR:  
  16. msg('A PCRE UTF-8 error occured. This might be caused by a faulty plugin',-1);  
  17. break;  
  18. case PREG_INTERNAL_ERROR:  
  19. msg('A PCRE internal error occured. This might be caused by a faulty plugin',-1);  
  20. break;  
  21. }  
  22. }  
  23.   
  24. $split = array($subject, "", "");  
  25. return false;  
  26. }  
  27.   
  28. $idx = count($matches)-2;  
  29. list($pre, $post) = preg_split($this->_patterns[$idx].$this->_getPerlMatchingFlags(), $subject, 2);  
  30. if(substr($this->_patterns[$idx].$this->_getPerlMatchingFlags(),0,5)=='(\/\/'){  
  31. $pre='//'.$pre;  
  32. }  
  33. $split = array($pre, $matches[0], $post);  
  34.   
  35. return isset($this->_labels[$idx]) ? $this->_labels[$idx] : true;  
  36. }  


这一步只是会把/不解析,但是仍然会转换为斜体,在修改文件/Inc/parser/xhtml.php:
源程序:
[php] view plaincopyprint?
  1. function emphasis_open() {  
  2. $this->doc .= '<em>';  
  3. }  
  4.   
  5. function emphasis_close() {  
  6. $this->doc .= '</em>';  
  7. }  


替换程序:
[php] view plaincopyprint?
  1. function emphasis_open() {  
  2. //$this->doc .= '<em>';  
  3. }  
  4. function emphasis_close() {  
  5. //$this->doc .= '</em>';  
  6. }  


这个<em>标签就是斜体,会默认将/和/之间的字符都带上这个,把这个给注释掉.OK.解决

5:效果显示图:


4.1:插入代码:

4.1.1:插入界面



4.1.2:插入整体效果图



4.1.3:发布之后效果图

4.2:截图拷贝:

4.2.1:随意截图



4.2.2:发布效果



5:总结:


5.1:一款好的内容查询是多么的重要,因为有的时候是使用call_user_func,或者那个调用的函数就是一个变量,根本无法按ctrl+追踪,只能在调用那个方法的时候把这个变量输出,然后在用zend自带的内容查询遍历文件夹
5.2:编辑器中文界面是原本就有的,只需要选择对应语言包就可以了
5.3:遗憾的是,我感觉自己并没有很完美的修改它的程序,尽管我的确只是单单修改了斜体这个功能,而没有影响其他功能.但是我原本是想直接修改传入正则那段,因为去掉了匹配斜体那段正则,我觉得才是比较完美的作法.因为Doku_LexerParallelRegex类是比较独立的,正则都是传入的.但是当我打印正则时,实在是太庞大了,所以最终我还是放弃了这个思路.
5.3.1:图片插入那块,也是全靠编辑器的功劳,我至今也不理解他能把内存的图片拷贝到编辑器的原理(尽管官网说他是漏洞,庆亮说是一个对象操作,我查查资料研究下.

5.3.2:代码高亮那块是采用Google的prettify.js,那块实际上是用js来修改源代码,在一些关键字的地方加上一些标签和css属性,于是就产生了高亮,关于这块,我是比较担心兼容性问题的(单个页面不担心,但是wiki也引入一堆js,但是目前看来,我测试了几次,都没什么问题,那应该不会出现了)


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
xhEditor
LinuxTOY 高效率编辑器 VIM-操作篇,非常适合 VIM 新手
Notepad2两款代码高亮配色 | 重华部落格
细数几款免费好用的在线HTML编辑器
PSPAD–最好用的ANSYS-APDL编辑器
Python编辑器有哪些?五大类介绍!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服