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 * title (optional) all text after '|' will be rendered above the main code text with a 9 * different style. 10 * 11 * Acknowledgements: 12 * Rounded corners based on snazzy borders by Stu Nicholls (http://www.cssplay.co.uk/boxes/snazzy) 13 * which is in turn based on nifty corners by Alessandro Fulciniti (http://pro.html.it/esempio/nifty/) 14 * 15 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 16 * @author Christopher Smith <chris@jalakai.co.uk> 17 * @author i-net software <tools@inetsoftware.de> 18 */ 19 20if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 21if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); 22require_once(DOKU_PLUGIN.'syntax.php'); 23 24/** 25 * All DokuWiki plugins to extend the parser/rendering mechanism 26 * need to inherit from this class 27 */ 28class syntax_plugin_box2 extends DokuWiki_Syntax_Plugin { 29 30 var $title_mode = false; 31 var $title_pos = array(); 32 var $title_name = 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_box'); 59 $this->Lexer->addEntryPattern('<box\s[^\r\n\|]*?>(?=.*?</box.*?>)',$mode,'plugin_box'); 60 $this->Lexer->addEntryPattern('<box\|(?=[^\r\n]*?\>.*?</box.*?\>)',$mode,'plugin_box'); 61 $this->Lexer->addEntryPattern('<box\s[^\r\n\|]*?\|(?=[^\r\n]*?>.*?</box.*?>)',$mode,'plugin_box'); 62 } 63 64 function postConnect() { 65 $this->Lexer->addPattern('>', 'plugin_box'); 66 $this->Lexer->addExitPattern('</box.*?>', 'plugin_box'); 67 } 68 69 /** 70 * Handle the match 71 */ 72 function handle($match, $state, $pos, Doku_Handler $handler){ 73 74 switch ($state) { 75 case DOKU_LEXER_ENTER: 76 $data = $this->_boxstyle(trim(substr($match, 4, -1))); 77 if (substr($match, -1) == '|') { 78 $this->title_mode = true; 79 return array('title_open',$data, $pos); 80 } else { 81 return array('box_open',$data, $pos); 82 } 83 84 case DOKU_LEXER_MATCHED: 85 if ($this->title_mode) { 86 $this->title_mode = false; 87 return array('box_open','', $pos); 88 } else { 89 return array('data', $match, $pos); 90 } 91 92 case DOKU_LEXER_UNMATCHED: 93 if ($this->title_mode) { 94 return array('data', $match, $pos); 95 } 96 97 $handler->_addCall('cdata',array($match), $pos); 98 return false; 99 case DOKU_LEXER_EXIT: 100 $pos += strlen($match); // has to be done becvause the ending tag comes after $pos 101 $data = trim(substr($match, 5, -1)); 102 $title = ($data && $data{0} == "|") ? substr($data,1) : ''; 103 104 return array('box_close', $title, $pos); 105 106 } 107 return false; 108 } 109 110 /** 111 * Create output 112 */ 113 function render($mode, Doku_Renderer $renderer, $indata) { 114 global $ID, $ACT; 115 116 // $pos is for the current position in the wiki page 117 if (empty($indata)) return false; 118 list($instr, $data, $pos) = $indata; 119 120 if($mode == 'xhtml'){ 121 switch ($instr) { 122 case 'title_open' : 123 $this->title_mode = true; 124 $this->title_pos[] = $pos; // Start Position for Section Editing 125 $renderer->doc .= $this->_xhtml_boxopen($data); 126 $renderer->doc .= "<h2 class='box_title " . (method_exists($renderer, "finishSectionEdit") ? $renderer->startSectionEdit($pos, 'section', 'box') : "") . " '{$this->_title_colours}>"; 127 break; 128 129 case 'box_open' : 130 if ($this->title_mode) { 131 $this->title_mode = false; 132 $renderer->doc .= "</h2>\n<div class='box_content'{$this->_content_colours}>"; 133 } else { 134 $this->title_pos[] = $pos; // Start Position for Section Editing 135 $this->title_name[] = 'box_' . 'no-title' . '_' . md5(time()); 136 $renderer->doc .= $this->_xhtml_boxopen($data)."<div class='box_content'{$this->_content_colours}>"; 137 } 138 break; 139 140 case 'data' : 141 $output = $renderer->_xmlEntities($data); 142 143 if ( $this->title_mode ) { 144 $this->title_name[] = 'box_' . cleanID($output) . '_' . md5($output); 145 $hid = $renderer->_headerToLink($output,true); 146 $renderer->doc .= '<a id="' . $hid . '" name="' . $hid . '">' . $output . '</a>'; 147 break; 148 } 149 150 $renderer->doc .= $output; 151 break; 152 153 case 'box_close' : 154 $renderer->doc .= "</div>\n"; 155 156 if ($data) { 157 $renderer->doc .= "<p class='box_caption'{$this->_title_colours}>".$renderer->_xmlEntities($data)."</p>\n"; 158 } 159 160 // insert the section edit button befor the box is closed - array_pop makes sure we take the last box 161 if ( $this->getConf('allowSectionEdit') && $ACT != 'preview' ) { 162 $renderer->nocache(); 163 164 165 if ( auth_quickaclcheck($ID) > AUTH_READ ) { 166 $title = array_pop($this->title_name); // Clean up 167 if ( method_exists($renderer, "finishSectionEdit") ) { 168 $renderer->finishSectionEdit($pos); 169 } 170 } 171 } 172 173 $renderer->doc .= $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 $styles['colour'][] = $token; 200 continue; 201 } 202 203 if ( preg_match('/^(margin|padding)(-(left|right|top|bottom))?:\d+(%|px|em|ex|pt|cm|mm|pi|in)$/', $token)) { 204 $styles['spacing'][] = $token; 205 } 206 207 // restrict token (class names) characters to prevent any malicious data 208 if (preg_match('/[^A-Za-z0-9_-]/',$token)) continue; 209 $styles['class'] = (isset($styles['class']) ? $styles['class'].' ' : '').$token; 210 } 211 if (!empty($styles['colour'])) { 212 $styles['colour'] = $this->_box_colours($styles['colour']); 213 } 214 215 return $styles; 216 } 217 218 function _box_colours($colours) { 219 $triplets = array(); 220 221 // only need the first four colours 222 if (count($colours) > 4) $colours = array_slice($colours,0,4); 223 foreach ($colours as $colour) { 224 $triplet[] = $this->_colourToTriplet($colour); 225 } 226 227 // there must be one colour to get here - the primary background 228 // calculate title background colour if not present 229 if (empty($triplet[1])) { 230 $triplet[1] = $triplet[0]; 231 } 232 233 // calculate outer background colour if not present 234 if (empty($triplet[2])) { 235 $triplet[2] = $triplet[0]; 236 } 237 238 // calculate border colour if not present 239 if (empty($triplet[3])) { 240 $triplet[3] = $triplet[0]; 241 } 242 243 // convert triplets back to style sheet colours 244 $style_colours['content_background'] = 'rgb('.join(',',$triplet[0]).')'; 245 $style_colours['title_background'] = 'rgb('.join(',',$triplet[1]).')'; 246 $style_colours['outer_background'] = 'rgb('.join(',',$triplet[2]).')'; 247 $style_colours['borders'] = 'rgb('.join(',',$triplet[3]).')'; 248 249 return $style_colours; 250 } 251 252 function _colourToTriplet($colour) { 253 if ($colour{0} == '#') { 254 if (strlen($colour) == 4) { 255 // format #FFF 256 return array(hexdec($colour{1}.$colour{1}),hexdec($colour{2}.$colour{2}),hexdec($colour{3}.$colour{3})); 257 } else { 258 // format #FFFFFF 259 return array(hexdec(substr($colour,1,2)),hexdec(substr($colour,3,2)), hexdec(substr($colour,5,2))); 260 } 261 } else { 262 // format rgb(x,y,z) 263 return explode(',',substr($colour,4,-1)); 264 } 265 } 266 267 function _xhtml_boxopen($styles) { 268 $class = 'class="box' . (isset($styles['class']) ? ' '.$styles['class'] : '') . '"'; 269 $style = isset($styles['width']) ? "width: {$styles['width']};" : ''; 270 $style .= isset($styles['spacing']) ? implode(';', $styles['spacing']) : ''; 271 272 if (isset($styles['colour'])) { 273 $style .= 'background-color:'.$styles['colour']['outer_background'].';'; 274 $style .= 'border-color: '.$styles['colour']['borders'].';'; 275 276 $this->_content_colours = 'style="background-color: '.$styles['colour']['content_background'].'; border-color: '.$styles['colour']['borders'].'"'; 277 $this->_title_colours = 'style="background-color: '.$styles['colour']['title_background'].';"'; 278 279 } else { 280 $this->_content_colours = ''; 281 $this->_title_colours = ''; 282 } 283 284 if (strlen($style)) $style = ' style="'.$style.'"'; 285 286 $this->_xb_colours[] = $colours; 287 288 $html = "<div $class$style>\n"; 289 290 // Don't do box extras if there is no style for them 291 if ( !empty($colours) ) { 292 $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"; 293 $html .=" <div class='xbox'$colours>\n"; 294 } 295 296 return $html; 297 } 298 299 function _xhtml_boxclose() { 300 301 $colours = array_pop($this->_xb_colours); 302 303 // Don't do box extras if there is no style for them 304 if ( !empty($colours) ) { 305 $html = " </div>\n"; 306 $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"; 307 } 308 $html .= "</div> <!-- Extras -->\n"; 309 310 return $html; 311 } 312 313} 314 315//Setup VIM: ex: et ts=4 enc=utf-8 :