Skip to content

discuz7.1php、C语言代码高亮方法

2009 十一月 7
by 荒野无灯

1,修改/include/discuzcode.func.php

1
2
3
4
5
6
7
8
9
function codedisp($code) {
    global $discuzcodes;
    $discuzcodes['pcodecount']++;
    $code = dhtmlspecialchars(str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code)));
    $code = str_replace("\n", "<li>", $code);
    $discuzcodes['codehtml'][$discuzcodes['pcodecount']] = tpl_codedisp($discuzcodes, $code);
    $discuzcodes['codecount']++;
    return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]";
}

下添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//////Discuz!7.1实现PHP代码高亮  by 荒野无灯   http://www.ihacklog.com//////
require_once('./include/my/geshi.php');


 function PerformHighlightGeshi($content, $options,$lang='php') {
       /* Geshi configuration */
    if (!$geshi) {
     $geshi= new GeSHi();
   $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS,1);
}
    $geshi->set_source($content);
  $geshi->set_language($lang);
    $geshi->set_overall_class('codecolorer');
  //  $geshi->set_tab_width($options['tab_size']);

      $geshi->enable_classes();
      if ($options['nowrap']) {
        $geshi->set_overall_style('white-space:nowrap');
      } else {
        $geshi->set_overall_style('');
      }

    if (!is_null($options['strict'])) $geshi->enable_strict_mode($options['strict']);
    if ($options['no_links']) $geshi->enable_keyword_links(false);
    if ($options['inline']) {
      $geshi->set_header_type(GESHI_HEADER_NONE);
    } else {
      $geshi->set_header_type(GESHI_HEADER_DIV);
    }

    $result = $geshi->parse_code();

    if ($geshi->error()) {
      return $geshi->error();
    }


    if ($options['line_numbers'] && !$options['inline'])
 {
      $table = '<table cellspacing="0" cellpadding="0"><tbody><tr><td ';
        $table .= 'class="line-numbers"';
         $table .= '><div>';
      for ($i = 0, $count = substr_count($result, '<br />') + 1; $i < $count; $i++)
    {
        $table .= ($i + $options['first_line']) . '<br />';
      }
      $result = $table . '</div></td><td>' . $result . '</td></tr></tbody></table>';
    }

   $theme = empty($options['inline_theme']) ? 'default' : $options['inline_theme'];
   $css_class = 'codecolorer-container ' . $lang . ' ' . $theme;
      if ($options['noborder']) $css_class .= ' codecolorer-noborder';
      $result = '<div class="' . $css_class . '" ' . $style . '>' . $result . '</div>';
       return $result;
  }


 


function mycodedisp($code,$lang) {
    global $discuzcodes;
    $discuzcodes['pcodecount']++;



      $options['line_numbers']=0;
     $options['inline']=0;
     $options['nowrap']=0;
    $code =PerformHighlightGeshi(str_replace('\\"', '"', preg_replace("/^[\n\r]*(.+?)[\n\r]*$/is", "\\1", $code)), $options,$lang) ;
       
      $discuzcodes['codehtml'][$discuzcodes['pcodecount']] ="<div id=\"code$discuzcodes[codecount]\">$code</div><em onclick=\"copycode($('code$discuzcodes[codecount]'));\">复制".$lang."代码到剪贴板</em>";
         

        $discuzcodes['codecount']++;
        return "[\tDISCUZ_CODE_$discuzcodes[pcodecount]\t]";
}


//////Discuz!7.1实现PHP代码高亮  by 荒野无灯   http://www.ihacklog.com//////

再在这个文件中查找:

1
2
3
4
5
6
function discuzcode($message, $smileyoff, $bbcodeoff, $htmlon = 0, $allowsmilies = 1, $allowbbcode = 1, $allowimgcode = 1, $allowhtml = 0, $jammer = 0, $parsetype = '0', $authorid = '0', $allowmediacode = '0', $pid = 0) {
    global $discuzcodes, $credits, $tid, $discuz_uid, $highlight, $maxsmilies, $db, $tablepre, $hideattach;

    if($parsetype != 1 && !$bbcodeoff && $allowbbcode && (strpos($message, '[/code]') || strpos($message, '[/CODE]')) !== FALSE) {
        $message = preg_replace("/\s*\[code\](.+?)\[\/code\]\s*/ies", "codedisp('\\1')", $message);
    }

在其下加上如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
//////Discuz!7.1实现PHP代码高亮  by 荒野无灯   http://www.ihacklog.com//////
    if($parsetype != 1 && !$bbcodeoff && $allowbbcode && (strpos($message, '[/php]') || strpos($message, '[/php]')) !== FALSE) {

        $message = preg_replace("/\s*\[php\](.+?)\[\/php\]\s*/ies", "mycodedisp('\\1','php')", $message );

}

  if($parsetype != 1 && !$bbcodeoff && $allowbbcode && (strpos($message, '[/c]') || strpos($message, '[/c]')) !== FALSE) {

        $message = preg_replace("/\s*\[c\](.+?)\[\/c\]\s*/ies", "mycodedisp('\\1','c')", $message );

}
 //////Discuz!7.1实现PHP代码高亮  by 荒野无灯   http://www.ihacklog.com//////

2,下载bb_php.gif 和bb_c.gif图标文件,上传到images/common/目录下。

3,下载下面这个文件夹,解压后上传到/include目录下。

4,修改templates/default/header.htm

1
<script type="text/javascript" src="{$jspath}common.js?{VERHASH}"></script>

这一行下面加上如下代码:

1
<link rel="stylesheet" href="include/my/codecolorer.css" type="text/css" media="screen" />

5,到dz后台添加自定义 Discuz! 代码按钮:
discuz_bbcode

php_discuz_highlight

dz_highlight_result

ps:如果插入代码的框想要像我图中所示是一个文本域的话,要修改一下/include/js/post.js ,查找:

1
var promptlang = custombbcodes[tag]['prompt'].split("\t");

将它下面的:

1
str += (promptlang[i - 1] ? promptlang[i - 1] : '请输入第 ' + i + ' 个参数:') + '<br /><input type="text" id="' + ctrlid + '_param_' + i + '" style="width: 98%" value="" class="txt" />' + (i < params ? '<br />' : '');

替换为:

1
str += (promptlang[i - 1] ? promptlang[i - 1] : '请输入第 ' + i + ' 个参数:') + '<br /><textarea id="' + ctrlid + '_param_' + i + '" style="width: 98%" cols="50" rows="5" class="txtarea"></textarea>' + (i < params ? '<br />' : '');

喜欢这篇文章吗?

请订阅本站 RSS feed填写您的邮件地址,订阅我们的精彩内容:,欢迎点击这里捐赠以支持荒野无灯转播到腾讯微博 转播到腾讯微博

作者:荒野无灯
出处:Hacklog【Hacklog】

声明: 本站遵循 署名-非商业性使用-相同方式共享 3.0 共享协议. 转载请注明转自Hacklog【荒野无灯weblog】

本文链接: http://ihacklog.com/?p=2993

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <q cite=""> <strong>

 :wink:  :-|  :-x  :twisted:  :)  8-O  :(  :roll:  :-P  :oops:  :-o  :mrgreen:  :lol:  :idea:  :-D  :evil:  :cry:  8)  :arrow:  :-?  :?:  :!:

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS