* * REMARK: depends on InlineJS embedder (syntax component) * SYNTAX: * */ // must be run within Dokuwiki if (!defined('DOKU_INC')) die(); if (!defined('NL')) define('NL',"\n"); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_canvas_canvas extends DokuWiki_Syntax_Plugin { protected $mode; protected $entry_pattern = ')'; protected $exit_pattern = ''; function __construct() { $this->mode = substr(get_class($this), 7); } function getType() { return 'protected'; } //function getPType() { return 'block'; } function getSort() { return 160; } function connectTo($mode) { $this->Lexer->addEntryPattern($this->entry_pattern, $mode, $this->mode); } function postConnect() { $this->Lexer->addExitPattern($this->exit_pattern, $this->mode); } /** * handle syntax */ public function handle($match, $state, $pos, Doku_Handler $handler){ global $conf; // check whether inlinejs plugin exists if (!plugin_isdisabled('inlinejs')) { $inlinejs = plugin_load('syntax', 'inlinejs_embedder'); if ($inlinejs->getConf('follow_htmlok') && !$conf['htmlok']) return false; } else { msg($this->getPluginName().': Plugin InlineJS disabled.',-1); return false; } switch ($state) { case DOKU_LEXER_ENTER: // at least one delimiter required to have unique canvas id. //if (strpos($match,' ') === false) { // msg($this->getPluginName().': syntax wrong.',-1); // return false; //} return array($state, $match); case DOKU_LEXER_UNMATCHED: // javascript code return array($state, $match); case DOKU_LEXER_EXIT: return array($state, ''); } return false; } /** * Render */ public function render($format, Doku_Renderer $renderer, $indata) { if (empty($indata)) return false; list($state, $data) = $indata; if ($format != 'xhtml') return false; switch ($state) { case DOKU_LEXER_ENTER: // get canvas type and id if ( substr($data, 1, 7) == 'canvas:') { $match = trim(substr($data, 8, -1)); } else { $match = trim(substr($data, 1, -1)); } list($ctype, $cid, $cparam) = explode(' ', $match, 3); if (empty($cid)) { msg($this->getPluginName().': syntax wrong -- '.hsc($data),-1); } $param = $this->getArguments($cparam, 'width'); // prepare canvas $renderer->doc.= $this->_htmlCanvas($cid, $ctype, $param); // open script tag to output embedded javascript $renderer->doc.= ''.NL; break; } return true; } /* --------------------------------------------------------- * build html of canvas * * @param $cid (string) canvas id * @param $ctype (string) canvas type * @param $opts (array) canvas options (width, height, ...) * --------------------------------------------------------- */ protected function _htmlCanvas($cid, $ctype, $opts) { // check whether canvas id is given? if (empty($cid)) return false; // set default canvas size if (!array_key_exists('width', $opts)) $opts['width'] = '300px'; if (!array_key_exists('height',$opts)) $opts['height'] = '150px'; // prepare plot container switch ($ctype) { case "jqplot": // see its project page https://bitbucket.org/cleonello/jqplot/overview // jqPlot is currently available for use in all personal or commercial projects // under both the MIT and GPL version 2.0 licenses. This means that you can // choose the license that best suits your project and use it accordingly. $html.= '