OSDN Git Service

git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@890 1ca29b6e-896d...
[nucleus-jp/nucleus-plugins.git] / NP_Wikistyle / trunk / NP_Wikistyle.php
1 <?php
2 // plugin needs to work on Nucleus versions <=2.0 as well
3 if (!function_exists('removeBreaks')) {
4         function removeBreaks($var)
5         {
6                 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
7         }
8 }
9
10
11 class NP_Wikistyle extends NucleusPlugin {
12
13         function getName()
14         {
15                 return 'Wikistyle'; 
16         }
17         
18         function getAuthor()
19         { 
20                 return 'nakahara21';
21         }
22         
23         function getURL()
24         {
25                 return 'http://nakahara21.com';
26         }
27         
28         function getVersion()
29         {
30                 return '0.51';
31         }
32         
33         function getDescription()
34         { 
35                 return 'convert WikiTag';
36         }
37
38         function supportsFeature($what) {
39                 switch($what){
40                         case 'SqlTablePrefix':
41                                 return 1;
42                         default:
43                                 return 0;
44                 }
45         }
46
47         function getEventList()
48         {
49                 return array(
50                                         'PreItem'
51                                 );
52         }
53
54         function event_PreItem(&$data)
55         {
56                 $this->currentItem =& $data["item"];
57                 $this->convert_wikitag($this->currentItem->body);
58                 if ($this->currentItem->more) {
59                         $this->convert_wikitag($this->currentItem->more);
60                 }
61         }
62
63         function convert_wikitag(&$text)
64         {
65                 $text = removeBreaks($text);
66                 $tmp_lines = explode("\n", $text);
67                 $tmp_lines[] = "&nbsp;";
68                 $text = "";
69                 $level = array();
70                 $templevel = array();
71                 
72                 $endline = count($tmp_lines) - 1;
73                 for ($i=0;$i<=$endline;$i++) {
74 //                      $tmp_lines[$i] = trim($tmp_lines[$i]);
75                         $level = '';
76                 //__________
77                 $replaceFrom = array(
78                         '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',           
79                         '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',
80                         '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/ie',
81                         '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/ie'                   
82                 );
83                 $replaceTo = array(
84                         '$this->createLinkCodeWiki("\\1", "\\2","https")',              
85                         '$this->createLinkCodeWiki("\\1", "\\2","http")',
86                         '$this->createLinkCodeWiki("\\1", "\\2","ftp")',
87                         '$this->createLinkCodeWiki("\\1", "\\3","mailto")'                      
88                 );
89
90                         if ($level[p] = preg_match('/^(https:\/\/|http:\/\/|www\.|ftp:\/\/|ftp\.|mailto:)/ie', $tmp_lines[$i])) {
91                                 $tmp_lines[$i] = preg_replace($replaceFrom, $replaceTo, $tmp_lines[$i]);
92                                 $text .= $tmp_lines[$i] . '<br />';
93                         }
94                 //__________
95                         if (($level[h] = strspn($tmp_lines[$i], '*')) > 6) {
96                                 $level[h] = 6; // limitation ;(
97                         }
98                         if ($level[h]) {
99                                 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[h]));
100                                 $text .= '<h' . intval($level[h]) . ' class="wiki">' . $tmp_lines[$i] . '</h' . intval($level[h]) . '>';
101                         }
102                 //__________
103                         if (($level[u] = strspn($tmp_lines[$i], '-')) > 3) { 
104                                 $level[r] = 4; 
105                                 $level[u] = 3; // limitation ;(
106                         }
107                         if ($level[u] && !$level[r]) {
108                                 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[u]));
109 //                              $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
110 //_-------------
111                                 if ($temptoplevel == 'u' && $templevel[o]) {
112                                         $tmp_lines[$i] = str_repeat("</ol>\n",$templevel[o]) . "\n" . '<li>' . $tmp_lines[$i] . '</li>';
113                                         $templevel[o] = 0;
114                                 }else{
115                                         $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
116                                 }
117 //_-------------
118                                 
119                                 $difflevel = $level[u] - $templevel[u];
120                                 if ($difflevel < 0) {
121                                         $text .= str_repeat("</ul>\n", 0 - $difflevel);
122                                 }
123                                 if ( $difflevel > 0) {
124                                         $text .= str_repeat("<ul>\n", $difflevel);
125                                 }
126                                 
127                                 $text .= $tmp_lines[$i];
128                                 if (!array_sum($templevel)) {
129                                         $temptoplevel = 'u';
130                                 }
131                                 $templevel[u] = $level[u];
132                         }
133                 //__________
134                         if ($level[r]) {
135                                 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[r]));
136                                 $text .= '<div class="hr"><hr /></div>';
137                         }
138                 //__________
139                         if (($level[o] = strspn($tmp_lines[$i],'+')) > 3) { 
140                                 $level[o] = 3; // limitation ;(
141                         }
142                         if ($level[o]) {
143                                 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[o]));
144
145 //                              $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
146 //_-------------
147                                 if ($temptoplevel == 'o' && $templevel[u]) {
148                                         $tmp_lines[$i] = str_repeat("</ul>\n", $templevel[u]) . "\n" . '<li>' . $tmp_lines[$i] . '</li>';
149                                         $templevel[u] = 0;
150                                 } else {
151                                         $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
152                                 }
153 //_-------------
154                                 
155                                 $difflevel = $level[o] - $templevel[o];
156                                 if ($difflevel < 0) {
157                                         $text .= str_repeat("</ol>\n", 0 - $difflevel);
158                                 }
159                                 if ( $difflevel > 0) {
160                                         $text .= str_repeat("<ol>\n", $difflevel);
161                                 }
162                                 
163                                 $text .= $tmp_lines[$i];
164                                 if (!array_sum($templevel)) {
165                                         $temptoplevel = 'o';
166                                 }
167                                 $templevel[o] = $level[o];
168                         }
169                 //__________
170                         if (array_sum($level) == 0) {
171                                 if (array_sum($templevel)) {
172                                         if ($templevel[u]) {
173                                                 $text .= str_repeat("</ul>\n", $templevel[u]);
174                                         }
175                                         if ($templevel[o]) {
176                                                 $text .= str_repeat("</ol>\n", $templevel[o]);
177                                         }
178                                         $templevel = array();
179                                         $temptoplevel = '';
180                                 }
181                                 if ($tmp_lines[$i] && $i != $endline) {
182                                         $text .= $tmp_lines[$i]."<br />";
183                                 }
184                         }
185                         if ($tmp_lines[$i] && $i != $endline) {
186                                 $text .= "\n";
187                         }
188                 }
189         } 
190
191         function createLinkCodeWiki($pre, $url, $protocol = 'http')
192         {
193                 $post = '';
194         
195                 // it's possible that $url ends with an entities 
196                 // since htmlspecialchars is applied before URL linking
197                 if (preg_match('/(&\w+;)+$/i', $url, $matches)) {
198                         $post = $matches[0];    // found entities (1 or more)
199                         $url = substr($url, 0, strlen($url) - strlen($post));
200                 }
201
202                 if (!ereg('^'.$protocol.'://',$url)) {
203                         $linkedUrl = $protocol . (($protocol == 'mailto') ? ':' : '://') . $url;
204                 } else {
205                         $linkedUrl = $url;
206                 }
207                         
208                         
209                 if ($protocol != 'mailto') {
210                         $displayedUrl = $linkedUrl;
211                 } else {
212                         $displayedUrl = $url;
213                 }
214                 return $pre . '<a href="' . htmlspecialchars($linkedUrl) . '" target="_blank">' . htmlspecialchars($displayedUrl) . '</a>' . $post;
215         }
216         
217
218 }
219 ?>