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