OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@1059 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_HighlightSource / trunk / NP_HighlightSource.php
1 <?
2 // plugin needs to work on Nucleus versions <=2.0 as well
3 if (!function_exists('removeBreaks')){
4         function removeBreaks($var) {                   return preg_replace("/<br \/>([\r\n])/","$1",$var); }
5 }
6
7 class NP_HighlightSource extends NucleusPlugin {
8
9 function getName() { return 'HighlightSource';    }
10 function getAuthor() { return 'nakahara21';    }
11 function getURL() { return 'http://xx.nakahara21.net'; }
12 function getVersion() { return '0.8'; }
13 function getDescription() { return 'HighlightSource'; }
14         function supportsFeature($what) {
15                 switch($what){
16                         case 'SqlTablePrefix':
17                                 return 1;
18                         default:
19                                 return 0;
20                 }
21         }
22         function install() {
23                 $this->createOption("Li", "Show line number?", "yesno", "no");
24         }
25
26         function getEventList() { return array('PreItem'); }
27
28         function event_PreItem($data){
29                 $this->currentItem = &$data["item"]; 
30                 $this->currentItem->more = preg_replace("/(<\/hs>)<br \/>\r\n/","$1",$this->currentItem->more);
31                 $this->currentItem->body = preg_replace_callback('#<hs>(.*?)<\/hs>#s', array(&$this, 'phpHighlight'), $this->currentItem->body); 
32                 $this->currentItem->more = preg_replace_callback('#<hs>(.*?)<\/hs>#s', array(&$this, 'phpHighlight'), $this->currentItem->more);
33         }
34
35         function phpHighlight($matches){
36                 ini_set('highlight.string', '#CC0000'); //#CC0000 default
37                 ini_set('highlight.comment', '#FF9900');        //#FF9900 default
38                 ini_set('highlight.keyword', '#006600');        //#006600 default
39                 ini_set('highlight.bg', '#dddddd');     //#dddddd default
40                 ini_set('highlight.default', '#0000CC');        //#0000CC default
41                 ini_set('highlight.html', '#000000');   //#000000 default
42                 
43                 $code = trim(removeBreaks($matches[1]), "\r,\n"); 
44                 
45                 if(substr(trim($code),0,2) != "<?"){
46                         $code = "<?php\n".$code;
47                         $sflag = 1;
48                 }
49                 if(substr(trim($code),-2,2) != "?>"){
50                         $code = $code."\n?>";
51                         $eflag = 1;
52                 }
53
54 //              $code = stripslashes($code);
55                 $code = highlight_string($code, true);
56                 
57                 $source = explode('<br />', $code);
58                 for($i=0;$i<count($source);$i++){
59                         $precode = $source[$i];
60                         $source[$i] = $precolor.trim($source[$i])."</font>";
61                         $source[$i] = preg_replace("/<font color\=\"#([a-z|A-Z|0-9]+)\"><\/font>/s","&nbsp;",$source[$i]);
62                         $ppp = preg_match_all("/<font color\=\"#([a-z|A-Z|0-9]+)\">/s",$precode,$pmat,PREG_SET_ORDER);
63                         if($pmat){
64                                 $las = count($pmat) - 1;
65                                 $pcolor = $pmat[$las][1];
66                         }
67                         $precolor = '<font color="#'.$pcolor.'">';
68                 } 
69                 $code = @join("<br />\n", $source) ;
70
71                 if($sflag)
72                         $code = ereg_replace("(<code><font color\=\"#[a-z|A-Z|0-9]+\">)([\r\n])(<font color\=\"#[a-z|A-Z|0-9]+\">)&lt;\?php<br />\n</font>", '\\1<!--hss-->', $code);
73                 else
74                         $code = ereg_replace("(<code><font color\=\"#[a-z|A-Z|0-9]+\">)([\r\n])", '\\1<!--hss-->', $code);
75
76                 if($eflag)
77                         $code = ereg_replace("<br />\n</font><font color\=\"#([a-z|A-Z|0-9]+)\">\?&gt;</font>\n</font>\n</code>", "</font><!--hss--></font></code>", $code);
78                 else
79                         $code = ereg_replace("</font>\n</font>\n</code>", "</font><!--hss--></font></code>", $code);
80
81                 if($this->getOption('Li') == 'no')
82                         return '<div class="code">'.$code.'</div>';
83
84                 $code = explode('<!--hss-->', $code);
85                         $source = explode('<br />', $code[1]);
86                         for($i=0;$i<count($source);$i++){
87                                 $source[$i] = '<li>'.trim($source[$i]).'</li>';
88                         } 
89                         $text = '<div class="code">';
90                         $text .= $code[0];
91                         $text .= "<ol>". @join('', $source) . "\n</ol>";
92                         $text .= $code[2];
93                         $text .= '</div>';
94
95                 return $text;
96         }
97
98 }
99 ?>