1<?php 2/** 3 * DokuWiki Plugin AmCharts (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Sylvain Menu <35niavlys@gmail.com> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11 12class syntax_plugin_amcharts 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('<amchart.+?</amchart>',$mode,'plugin_amcharts'); 39 } 40 41 /** 42 * Handle matches of the amchart 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), 8, -10); 52 list($opts, $amdata) = explode('>', $match, 2); 53 preg_match_all('/(\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 $amdata = preg_replace_callback( 64 '#//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"#ms', 65 function($matches){ 66 $m = $matches[0]; 67 return substr($m, 0, 1)==='/' ? ' ' : $m; 68 }, $amdata 69 ); // remove comments (respecting quoted strings) 70 $amdata = explode("\n", $amdata); 71 $amdata = implode("", array_map(trim, $amdata)); 72 $chartid = uniqid('__amchart_'); 73 $amdata = base64_encode($amdata); 74 75 return array($chartid, $amdata, $opts); 76 } 77 78 /** 79 * Render xhtml output or metadata 80 * 81 * @param string $mode Renderer mode (supported modes: xhtml) 82 * @param Doku_Renderer $renderer The renderer 83 * @param array $data The data from the handler() function 84 * @return bool If rendering was successful. 85 */ 86 public function render($mode, Doku_Renderer $renderer, $data) { 87 if($mode != 'xhtml') return false; 88 89 list($chartid, $amdata, $opts) = $data; 90 $s = ''; 91 $c = ''; 92 foreach($opts as $n => $v) { 93 if(in_array($n, array('width','height')) && $v) { 94 $s .= $n.':'.hsc($v).';'; 95 } elseif($n=='align' && in_array($v, array('left','right','center'))) { 96 $c = 'media'.$v; 97 } 98 } 99 if($s) $s = ' style="'.$s.'"'; 100 if($c) $c = ' class="'.$c.'"'; 101 $renderer->doc .= '<div id="'.$chartid.'"'.$c.$s.' data-amchart="'.$amdata.'"></div>'."\n"; 102 103 return true; 104 } 105}