2 // plugin needs to work on Nucleus versions <=2.0 as well
3 if (!function_exists('removeBreaks')) {
4 function removeBreaks($var)
6 return preg_replace("/<br \/>([\r\n])/", "$1", $var);
11 class NP_Wikistyle extends NucleusPlugin {
25 return 'http://nakahara21.com';
33 function getDescription()
35 return 'convert WikiTag';
38 function supportsFeature($what) {
40 case 'SqlTablePrefix':
47 function getEventList()
54 function event_PreItem(&$data)
56 $this->currentItem =& $data["item"];
57 $this->convert_wikitag($this->currentItem->body);
58 if ($this->currentItem->more) {
59 $this->convert_wikitag($this->currentItem->more);
63 function convert_wikitag(&$text)
65 $text = removeBreaks($text);
66 $tmp_lines = explode("\n", $text);
67 $tmp_lines[] = " ";
72 $endline = count($tmp_lines) - 1;
73 for ($i=0;$i<=$endline;$i++) {
74 // $tmp_lines[$i] = trim($tmp_lines[$i]);
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'
84 '$this->createLinkCodeWiki("\\1", "\\2","https")',
85 '$this->createLinkCodeWiki("\\1", "\\2","http")',
86 '$this->createLinkCodeWiki("\\1", "\\2","ftp")',
87 '$this->createLinkCodeWiki("\\1", "\\3","mailto")'
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 />';
95 if (($level[h] = strspn($tmp_lines[$i], '*')) > 6) {
96 $level[h] = 6; // limitation ;(
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]) . '>';
103 if (($level[u] = strspn($tmp_lines[$i], '-')) > 3) {
105 $level[u] = 3; // limitation ;(
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>';
111 if ($temptoplevel == 'u' && $templevel[o]) {
112 $tmp_lines[$i] = str_repeat("</ol>\n",$templevel[o]) . "\n" . '<li>' . $tmp_lines[$i] . '</li>';
115 $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
119 $difflevel = $level[u] - $templevel[u];
120 if ($difflevel < 0) {
121 $text .= str_repeat("</ul>\n", 0 - $difflevel);
123 if ( $difflevel > 0) {
124 $text .= str_repeat("<ul>\n", $difflevel);
127 $text .= $tmp_lines[$i];
128 if (!array_sum($templevel)) {
131 $templevel[u] = $level[u];
135 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[r]));
136 $text .= '<div class="hr"><hr /></div>';
139 if (($level[o] = strspn($tmp_lines[$i],'+')) > 3) {
140 $level[o] = 3; // limitation ;(
143 $tmp_lines[$i] = ltrim(substr($tmp_lines[$i], $level[o]));
145 // $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
147 if ($temptoplevel == 'o' && $templevel[u]) {
148 $tmp_lines[$i] = str_repeat("</ul>\n", $templevel[u]) . "\n" . '<li>' . $tmp_lines[$i] . '</li>';
151 $tmp_lines[$i] = '<li>' . $tmp_lines[$i] . '</li>';
155 $difflevel = $level[o] - $templevel[o];
156 if ($difflevel < 0) {
157 $text .= str_repeat("</ol>\n", 0 - $difflevel);
159 if ( $difflevel > 0) {
160 $text .= str_repeat("<ol>\n", $difflevel);
163 $text .= $tmp_lines[$i];
164 if (!array_sum($templevel)) {
167 $templevel[o] = $level[o];
170 if (array_sum($level) == 0) {
171 if (array_sum($templevel)) {
173 $text .= str_repeat("</ul>\n", $templevel[u]);
176 $text .= str_repeat("</ol>\n", $templevel[o]);
178 $templevel = array();
181 if ($tmp_lines[$i] && $i != $endline) {
182 $text .= $tmp_lines[$i]."<br />";
185 if ($tmp_lines[$i] && $i != $endline) {
191 function createLinkCodeWiki($pre, $url, $protocol = 'http')
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));
202 if (!ereg('^'.$protocol.'://',$url)) {
203 $linkedUrl = $protocol . (($protocol == 'mailto') ? ':' : '://') . $url;
209 if ($protocol != 'mailto') {
210 $displayedUrl = $linkedUrl;
212 $displayedUrl = $url;
214 return $pre . '<a href="' . htmlspecialchars($linkedUrl) . '" target="_blank">' . htmlspecialchars($displayedUrl) . '</a>' . $post;