1<?php 2/** 3 * Box Plugin: Draw highlighting boxes around wiki markup 4 * 5 * Syntax: <box width% classes|title> 6 * width% width of the box, must use % unit 7 * classes one or more classes used to style the box, several predefined styles included in style.css 8 * padding can be defined with each direction or as composite 9 * margin can be defined with each direction or as composite 10 * title (optional) all text after '|' will be rendered above the main code text with a 11 * different style. 12 * 13 * Acknowledgements: 14 * Rounded corners based on snazzy borders by Stu Nicholls (http://www.cssplay.co.uk/boxes/snazzy) 15 * which is in turn based on nifty corners by Alessandro Fulciniti (http://pro.html.it/esempio/nifty/) 16 * 17 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 18 * @author Christopher Smith <chris@jalakai.co.uk> 19 * @author i-net software <tools@inetsoftware.de> 20 */ 21 22if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 23if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 24 25/** 26 * All DokuWiki plugins to extend the parser/rendering mechanism 27 * need to inherit from this class 28 */ 29class syntax_plugin_box2 extends DokuWiki_Syntax_Plugin { 30 31 var $title_mode = array(); 32 var $box_content = array(); 33 34 // the following are used in rendering and are set by _xhtml_boxopen() 35 var $_xb_colours = array(); 36 var $_content_colours = ''; 37 var $_title_colours = ''; 38 39 function getType(){ return 'protected';} 40 function getAllowedTypes() { return array('container','substition','protected','disabled','formatting','paragraphs'); } 41 function getPType(){ return 'block';} 42 43 // must return a number lower than returned by native 'code' mode (200) 44 function getSort(){ return 195; } 45 46 // override default accepts() method to allow nesting 47 // - ie, to get the plugin accepts its own entry syntax 48 function accepts($mode) { 49 if ($mode == substr(get_class($this), 7)) return true; 50 51 return parent::accepts($mode); 52 } 53 54 /** 55 * Connect pattern to lexer 56 */ 57 function connectTo($mode) { 58 $this->Lexer->addEntryPattern('<box>(?=.*?</box.*?>)',$mode,'plugin_box2'); 59 $this->Lexer->addEntryPattern('<box\s[^\r\n\|]*?>(?=.*?</box.*?>)',$mode,'plugin_box2'); 60 $this->Lexer->addEntryPattern('<box\|(?=[^\r\n]*?\>.*?</box.*?\>)',$mode,'plugin_box2'); 61 $this->Lexer->addEntryPattern('<box\s[^\r\n\|]*?\|(?=[^\r\n]*?>.*?</box.*?>)',$mode,'plugin_box2'); 62 } 63 64 function postConnect() { 65 $this->Lexer->addPattern('>', 'plugin_box2'); 66 $this->Lexer->addExitPattern('</box.*?>', 'plugin_box2'); 67 } 68 69 private function isTitleMode() { 70 return $this->title_mode[count($this->title_mode)-1]; 71 } 72 73 private function isBoxContent() { 74 return $this->box_content[count($this->box_content)-1]; 75 } 76 77 /** 78 * Handle the match 79 */ 80 function handle($match, $state, $pos, Doku_Handler $handler){ 81 82 switch ($state) { 83 case DOKU_LEXER_ENTER: 84 $data = $this->_boxstyle(trim(substr($match, 4, -1))); 85 if (substr($match, -1) == '|') { 86 $this->title_mode[] = true; 87 return array('title_open',$data, $pos); 88 } else { 89 return array('box_open',$data, $pos); 90 } 91 92 case DOKU_LEXER_MATCHED: 93 if ($this->isTitleMode()) { 94 array_pop( $this->title_mode ); 95 return array('box_open','', $pos); 96 } else { 97 return array('data', $match, $pos); 98 } 99 100 case DOKU_LEXER_UNMATCHED: 101 if ($this->isTitleMode()) { 102 return array('title', $match, $pos); 103 } 104 105 $handler->_addCall('cdata',array($match), $pos); 106 return false; 107 case DOKU_LEXER_EXIT: 108 $pos += strlen($match); // has to be done becvause the ending tag comes after $pos 109 $data = trim(substr($match, 5, -1)); 110 $title = ($data && $data[0] == "|") ? substr($data,1) : ''; 111 112 return array('box_close', $title, $pos); 113 114 } 115 return false; 116 } 117 118 /** 119 * Create output 120 */ 121 function render($mode, Doku_Renderer $renderer, $indata) { 122 global $ID, $ACT; 123 124 // $pos is for the current position in the wiki page 125 if (empty($indata)) return false; 126 list($instr, $data, $pos) = $indata; 127 128 if($mode == 'xhtml'){ 129 switch ($instr) { 130 case 'title_open' : 131 $this->title_mode[] = true; 132 $renderer->doc .= $this->_xhtml_boxopen($renderer, $pos, $data); 133 $renderer->doc .= '<h2 class="box_title"' . $this->_title_colours . '>'; 134 break; 135 136 case 'box_open' : 137 if ($this->isTitleMode()) { 138 array_pop( $this->title_mode ); 139 $this->box_content[] = true; 140 $renderer->doc .= "</h2>\n<div class=\"box_content\"" . $this->_content_colours . '>'; 141 } else { 142 $renderer->doc .= $this->_xhtml_boxopen($renderer, $pos, $data); 143 144 if ( strlen( $this->_content_colours ) > 0 ) { 145 $this->box_content[] = true; 146 $renderer->doc .= '<div class="box_content"' . $this->_content_colours . '>'; 147 } 148 } 149 break; 150 151 case 'title': 152 case 'data' : 153 $output = $renderer->_xmlEntities($data); 154 155 if ( $this->isTitleMode() ) { 156 $hid = $renderer->_headerToLink($output,true); 157 $renderer->doc .= '<a id="' . $hid . '" name="' . $hid . '">' . $output . '</a>'; 158 break; 159 } 160 161 $renderer->doc .= $output; 162 break; 163 164 case 'box_close' : 165 if ( $this->isBoxContent() ) { 166 array_pop( $this->box_content ); 167 $renderer->doc .= "</div>\n"; 168 } 169 170 if ($data) { 171 $renderer->doc .= '<p class="box_caption"' . $this->_title_colours . '>' . $renderer->_xmlEntities($data) . "</p>\n"; 172 } 173 174 // insert the section edit button befor the box is closed - array_pop makes sure we take the last box 175 if ( method_exists($renderer, "finishSectionEdit") ) { 176 $renderer->nocache(); 177 $renderer->finishSectionEdit($pos); 178 } 179 180 $renderer->doc .= "\n" . $this->_xhtml_boxclose(); 181 182 break; 183 } 184 185 return true; 186 } 187 return false; 188 } 189 190 function _boxstyle($str) { 191 if (!strlen($str)) return array(); 192 193 $styles = array(); 194 195 $tokens = preg_split('/\s+/', $str, 9); // limit is defensive 196 foreach ($tokens as $token) { 197 if (preg_match('/^\d*\.?\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) { 198 $styles['width'] = $token; 199 continue; 200 } 201 202 if (preg_match('/^( 203 (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))| #colorvalue 204 (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)) #rgb triplet 205 )$/x', $token)) { 206 if (preg_match('/^#[A-Za-z0-9_-]+$/', $token)) { 207 $styles['id'] = substr($token, 1); 208 continue; 209 } 210 211 $styles['colour'][] = $token; 212 continue; 213 } 214 215 if ( preg_match('/^(margin|padding)(-(left|right|top|bottom))?:\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) { 216 $styles['spacing'][] = $token; 217 } 218 219 // restrict token (class names) characters to prevent any malicious data 220 if (preg_match('/[^A-Za-z0-9_-]/',$token)) continue; 221 $styles['class'] = (isset($styles['class']) ? $styles['class'].' ' : '').$token; 222 } 223 if (!empty($styles['colour'])) { 224 $styles['colour'] = $this->_box_colours($styles['colour']); 225 } 226 227 return $styles; 228 } 229 230 function _box_colours($colours) { 231 $triplets = array(); 232 233 // only need the first four colours 234 if (count($colours) > 4) $colours = array_slice($colours,0,4); 235 foreach ($colours as $colour) { 236 $triplet[] = $this->_colourToTriplet($colour); 237 } 238 239 // there must be one colour to get here - the primary background 240 // calculate title background colour if not present 241 if (empty($triplet[1])) { 242 $triplet[1] = $triplet[0]; 243 } 244 245 // calculate outer background colour if not present 246 if (empty($triplet[2])) { 247 $triplet[2] = $triplet[0]; 248 } 249 250 // calculate border colour if not present 251 if (empty($triplet[3])) { 252 $triplet[3] = $triplet[0]; 253 } 254 255 // convert triplets back to style sheet colours 256 $style_colours['content_background'] = 'rgb('.join(',',$triplet[0]).')'; 257 $style_colours['title_background'] = 'rgb('.join(',',$triplet[1]).')'; 258 $style_colours['outer_background'] = 'rgb('.join(',',$triplet[2]).')'; 259 $style_colours['borders'] = 'rgb('.join(',',$triplet[3]).')'; 260 261 return $style_colours; 262 } 263 264 function _colourToTriplet($colour) { 265 if ($colour[0] == '#') { 266 if (strlen($colour) == 4) { 267 // format #FFF 268 return array(hexdec($colour[1].$colour[1]),hexdec($colour[2].$colour[2]),hexdec($colour[3].$colour[3])); 269 } else { 270 // format #FFFFFF 271 return array(hexdec(substr($colour,1,2)),hexdec(substr($colour,3,2)), hexdec(substr($colour,5,2))); 272 } 273 } else { 274 // format rgb(x,y,z) 275 return explode(',',substr($colour,4,-1)); 276 } 277 } 278 279 function _xhtml_boxopen($renderer, $pos, $styles) { 280 $class = 'class="box' . (isset($styles['class']) ? ' '.$styles['class'] : '') . (method_exists($renderer, "startSectionEdit") ? " " . $renderer->startSectionEdit($pos, array( 'target' => 'section', 'name' => 'box-' . $pos)) : "") . '"'; 281 $style = isset($styles['width']) ? "width: {$styles['width']};" : ''; 282 $style .= isset($styles['spacing']) ? implode(';', $styles['spacing']) : ''; 283 284 if (isset($styles['colour'])) { 285 $style .= 'background-color:'.$styles['colour']['outer_background'].';'; 286 $style .= 'border-color: '.$styles['colour']['borders'].';'; 287 288 $this->_content_colours = 'style="background-color: '.$styles['colour']['content_background'].'; border-color: '.$styles['colour']['borders'].'"'; 289 $this->_title_colours = 'style="background-color: '.$styles['colour']['title_background'].';"'; 290 291 } else { 292 $this->_content_colours = ''; 293 $this->_title_colours = ''; 294 } 295 296 if (strlen($style)) $style = ' style="'.$style.'"'; 297 if (array_key_exists('id', $styles)) { 298 $class = 'id="' . $styles['id'] . '" ' . $class; 299 } 300 301 $this->_xb_colours[] = $colours; 302 303 $html = "<div $class$style>\n"; 304 305 // Don't do box extras if there is no style for them 306 if ( !empty($colours) ) { 307 $html .= '<b class="xtop"><b class="xb1"' . $colours . '> </b><b class="xb2"' . $colours . '"> </b><b class="xb3"' . $colours . '> </b><b class="xb4"' . $colours . '> </b></b>' . "\n"; 308 $html .= '<div class="xbox"' . $colours . ">\n"; 309 } 310 311 return $html; 312 } 313 314 function _xhtml_boxclose() { 315 316 $colours = array_pop($this->_xb_colours); 317 318 // Don't do box extras if there is no style for them 319 if ( !empty($colours) ) { 320 $html = "</div>\n"; 321 $html .= '<b class="xbottom"><b class="xb4"' . $colours . '> </b><b class="xb3"' . $colours . '> </b><b class="xb2"' . $colours . '> </b><b class="xb1"' . $colours . '> </b></b>' . "\n"; 322 } 323 $html .= '</div> <!-- Extras -->' . "\n"; 324 325 return $html; 326 } 327 328} 329 330//Setup VIM: ex: et ts=4 enc=utf-8 :