2 /* plugin needs to work on Nucleus versions <=2.0 as well
3 if (!function_exists('sql_table')){
4 function sql_table($name) {
5 return 'nucleus_' . $name;
9 class NP_ChoppedDisc extends NucleusPlugin
11 function getEventList()
18 return 'Chopped description';
28 return 'http://nakahara21.com';
36 function getDescription()
38 return 'Chopped description. <%ChoppedDisc(250,1)%>';
41 function supportsFeature($what)
44 case 'SqlTablePrefix':
51 function parseHighlight($query)
55 $query = preg_replace('/\'|"/','',$query);
57 if (!query) return array();
59 $aHighlight = explode(' ', $query);
61 for ($i = 0; $i<count($aHighlight); $i++) {
62 $aHighlight[$i] = trim($aHighlight[$i]);
63 // if (strlen($aHighlight[$i]) < 3)
64 // unset($aHighlight[$i]);
67 // if (count($aHighlight) == 1)
68 // return $aHighlight[0];
73 function splitLastStr($str, $width=5)
75 $posn = (mb_strwidth($str) > $width)? mb_strwidth($str) - $width: 0;
76 $resArray[0] = ($posn) ? mb_strcut($str, 0, $posn, _CHARSET) : '';
77 $resArray[1] = ($posn) ? mb_strcut($str, $posn, $width + 2, _CHARSET) : $str;
81 function chopStr($str, $query, $maxLength)
84 $searchclass =& new SEARCH($query);
85 $highlight = $searchclass->inclusive;
86 $this->highlights = $this->parseHighlight($highlight);
88 if(mb_strwidth($str) <= $maxLength)
92 $tLength = mb_strwidth($toated);
93 $maxLength = $maxLength - $tLength;
95 $text = highlight($str, $this->highlights, '<\0>');
96 $text = '< >' . $text;
97 preg_match_all('/(<[^>]+>)([^<>]*)/', $text, $matches);
98 for($i=0;$i<count($matches[1]);$i++){
99 $matches[1][$i] = ereg_replace("<|>", '', $matches[1][$i]);
105 for ($i=0;$i<count($this->highlights);$i++) {
106 for ($e=0;$e<count($matches[1]);$e++) {
107 if (eregi($this->highlights[$i], $matches[1][$e])) {
117 $tt = mb_strcut($matches[2][0], 0, $maxLength, _CHARSET);
118 if(mb_strwidth($matches[2][0]) > $maxLength) {
121 } elseif($hitkey[1]) {
123 foreach ($hitkey as $keyval) {
124 $hitWordArray[] = $matches[1][$keyval];
127 $list[0] = array("qlen" => 0, "q" => '');
128 $trimLength = intval(($maxLength - mb_strwidth(join("",$hitWordArray))) / (count($hitWordArray) +1));
132 while ($i <= count($hitWordArray)) {
133 // $hitWord = $hitWordArray[$i];
134 $tempArray = ($hitWord = $hitWordArray[$i]) ? explode($hitWord, $left, 2) : array($left, '');
135 $preStr = ($hitWord) ? $this->splitLastStr($tempArray[0], 5) : array($left, '');
137 $left = $preStr[1].$hitWord.$tempArray[1];
139 $list[$i]['str'] = $preStr[0];
140 $list[$i]['len'] = mb_strwidth($preStr[0]);
142 $tempTrimLen = $trimLength + $list[$i]['qlen'];
144 if($list[$i]['len'] < $tempTrimLen){
145 $list[$i]['trimlen'] = 0;
146 $addsum += $tempTrimLen - $list[$i]['len'];
148 $list[$i]['trimlen'] = $list[$i]['len'] - $tempTrimLen;
151 if (!$hitWord) break;
153 $list[$i]['q'] = $hitWord;
154 $list[$i]['qlen'] = mb_strwidth($hitWord);
158 for ($i=0;$i<count($list);$i++) {
159 if ($list[$i]['trimlen'] && ($addsum > 0)) {
160 $list[$i]['trimlen'] = min($list[$i]['trimlen'], $addsum);
161 $addsum -= $list[$i]['trimlen'];
162 $list[$i]['trimlen'] = $trimLength + $list[$i]['trimlen'] + $list[$i]['qlen'];
163 } elseif($list[$i]['trimlen']) {
164 $list[$i]['trimlen'] = $trimLength + $list[$i]['qlen'];
166 $list[$i]['trimlen'] = $list[$i]['len'];
173 $list[0]['len'] - $list[0]['trimlen'],
174 $list[0]['trimlen'] + 2,
176 if ($list[0]['len'] > $list[0]['trimlen']) {
180 for ($i=1;$i<count($list);$i++) {
181 $tt .= mb_strcut($list[$i]['str'], 0, $list[$i]['trimlen'], _CHARSET);
182 if ($list[$i]['len'] > $list[$i]['trimlen']) {
187 $hitWord = $this->highlights[0];
188 $keyLength = mb_strwidth($hitWord);
190 $splitStr = preg_quote($hitWord);
191 list($preStr, $hStr) = preg_split("/$splitStr/i",$str,2);
193 $preStrLength = mb_strwidth($preStr);
194 $hStrLength = mb_strwidth($hStr);
195 $halfLength = intval(($maxLength - $keyLength) / 2);
197 $hTrimLength = $preTrimLength = $halfLength;
198 $minLength = min($preStrLength, $hStrLength, $halfLength);
199 if ($preStrLength == $minLength) {
200 $hTrimLength = $maxLength - $keyLength - $preStrLength;
201 $preTrimLength = $preStrLength;
203 if ($hStrLength == $minLength) {
204 $preTrimLength = $maxLength - $keyLength - $hStrLength;
205 $hTrimLength = $hStrLength;
208 $tt = mb_strcut($preStr, $preStrLength - $preTrimLength, $preStrLength, _CHARSET);
209 $tt .= $matches[1][1];
210 $tt .= mb_strcut($hStr, 0, $hTrimLength,_CHARSET);
212 if ($preTrimLength < $preStrLength) {
215 if ($hTrimLength < $hStrLength) {
224 $temp_str_array = mb_split($query,$str);
225 $num = count($temp_str_array);
226 foreach($temp_str_array as $key => $temp_str){
227 $lastp = mb_strwidth($temp_str);
229 $temp_s = mb_strcut($temp_str, 0, 40, _CHARSET);
230 if($key>0 || ($key==0 && $lastp>40))
232 $temp_s .= mb_strcut($temp_str, $lastp-40, $lastp, _CHARSET);
233 $str_array[$key] = $temp_s;
234 // if($key>0 && $lastp<83)
235 if(($key>0 && $lastp<83) || $key == $num-1)
236 $str_array[$key] = $temp_str;
238 $temp_str_length += mb_strwidth($str_array[$key]);
239 if($temp_str_length > $maxLength)
240 $str_array[$key] = $temp_str;
242 $str = @join($query,$str_array);
247 function doTemplateVar(&$item, $maxLength = 250, $addHighlight = 0)
249 global $CONF, $manager, $member, $catid;
252 if ($manager->pluginInstalled('NP_HighlightSource')) {
253 $tempPlugin =& $manager->getPlugin('NP_HighlightSource');
254 preg_replace_callback("#<hs(|:[^>]+?)>(.*?)</hs>#s", array(&$tempPlugin, 'phpHighlight'), $item->body);
255 preg_replace_callback("#<hs(|:[^>]+?)>(.*?)</hs>#s", array(&$tempPlugin, 'phpHighlight'), $item->more);
258 $syndicated = strip_tags($item->body);
259 $syndicated .= strip_tags($item->more);
260 $syndicated = preg_replace("/[\r\n]/", "", $syndicated);
262 $syndicated = $this->chopStr($syndicated, $query, $maxLength);
266 echo highlight(htmlspecialchars($syndicated), $this->highlights, '<span class="highlight">\0</span>');
268 echo htmlspecialchars($syndicated);
273 global $currentTemplateName;
274 $template =& $manager->getTemplate($currentTemplateName);
275 echo highlight($syndicated, $this->highlights, $template['SEARCH_HIGHLIGHT']);