1<?php 2/** 3 * DokuWiki Plugin c3chart (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Jason Xun Xu <dev@jasonxu.net> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12class syntax_plugin_c3chart extends DokuWiki_Syntax_Plugin { 13 /** 14 * @return string Syntax mode type 15 */ 16 public function getType() { 17 return 'substition'; 18 } 19 /** 20 * @return string Paragraph type 21 */ 22 public function getPType() { 23 return 'block'; 24 } 25 /** 26 * @return int Sort order - Low numbers go before high numbers 27 */ 28 public function getSort() { 29 return 200; 30 } 31 32 /** 33 * Connect lookup pattern to lexer. 34 * 35 * @param string $mode Parser mode 36 */ 37 public function connectTo($mode) { 38 $this->Lexer->addSpecialPattern('<c3.+?</c3>',$mode,'plugin_c3chart'); 39 } 40 41 /** 42 * Handle matches of the c3chart syntax 43 * 44 * @param string $match The match of the syntax 45 * @param int $state The state of the handler 46 * @param int $pos The position in the document 47 * @param Doku_Handler $handler The handler 48 * @return array Data for the renderer 49 */ 50 public function handle($match, $state, $pos, Doku_Handler $handler){ 51 $match = substr(trim($match), 3, -5); 52 list($opts, $c3data) = explode('>', $match, 2); 53 preg_match_all('/(\\w+)\s*=\\s*([^"\'\\s>]*)/', $opts, $matches, PREG_SET_ORDER); 54 $opts = array( 55 'width' => $this->getConf('width'), 56 'height' => $this->getConf('height'), 57 'align' => $this->getConf('align'), 58 ); 59 foreach($matches as $m) { 60 $opts[strtolower($m[1])] = $m[2]; 61 } 62 63 $c3data = preg_replace_callback( 64 '#//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"#ms', 65 function($matches){ 66 $m = $matches[0]; 67 return substr($m, 0, 1)==='/' ? ' ' : $m; 68 }, $c3data 69 ); // remove comments (respecting quoted strings) 70 $c3data = explode("\n", $c3data); 71 $c3data = implode("", array_map(trim, $c3data)); 72 if($c3data[0]=='{') $c3data = substr($c3data, 1, -1); 73 $chartid = uniqid('__c3chart_'); 74 $c3data = base64_encode('{"bindto": "#'.$chartid.'",'.$c3data.'}'); 75 76 return array($chartid, $c3data, $opts); 77 } 78 79 /** 80 * Render xhtml output or metadata 81 * 82 * @param string $mode Renderer mode (supported modes: xhtml) 83 * @param Doku_Renderer $renderer The renderer 84 * @param array $data The data from the handler() function 85 * @return bool If rendering was successful. 86 */ 87 public function render($mode, Doku_Renderer $renderer, $data) { 88 if($mode != 'xhtml') return false; 89 90 list($chartid, $c3data, $opts) = $data; 91 $s = ''; 92 $c = ''; 93 foreach($opts as $n => $v) { 94 if(in_array($n, array('width','height')) && $v) { 95 $s .= $n.':'.hsc($v).';'; 96 } elseif($n=='align' && in_array($v, array('left','right','center'))) { 97 $c = 'media'.$v; 98 } 99 } 100 if($s) $s = ' style="'.$s.'"'; 101 if($c) $c = ' class="'.$c.'"'; 102 $renderer->doc .= '<div id="'.$chartid.'"'.$c.$s.' data-c3chart="'.$c3data.'"></div>'."\n"; 103 104 return true; 105 } 106} 107 108// vim:ts=4:sw=4:et: 109