*/ // must be run within Dokuwiki if(!defined('DOKU_INC')) die(); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_color extends DokuWiki_Syntax_Plugin { public function getType(){ return 'formatting'; } public function getAllowedTypes() { return array('formatting', 'substition', 'disabled'); } public function getSort(){ return 158; } public function connectTo($mode) { $this->Lexer->addEntryPattern('(?=.*?)',$mode,'plugin_color'); } public function postConnect() { $this->Lexer->addExitPattern('','plugin_color'); } /** * Handle the match */ public function handle($match, $state, $pos, Doku_Handler $handler){ switch ($state) { case DOKU_LEXER_ENTER : list($color, $background) = preg_split("/\//u", substr($match, 6, -1), 2); if ($color = $this->_isValid($color)) $color = "color:$color;"; if ($background = $this->_isValid($background)) $background = "background-color:$background;"; return array($state, array($color, $background)); case DOKU_LEXER_UNMATCHED : return array($state, $match); case DOKU_LEXER_EXIT : return array($state, ''); } return array(); } /** * Create output */ public function render($format, Doku_Renderer $renderer, $data) { // $data is what the function handle() return'ed. if($format == 'xhtml'){ /** @var Doku_Renderer_xhtml $renderer */ list($state,$match) = $data; switch ($state) { case DOKU_LEXER_ENTER : list($color, $background) = $match; $renderer->doc .= ""; break; case DOKU_LEXER_UNMATCHED : $renderer->doc .= $renderer->_xmlEntities($match); break; case DOKU_LEXER_EXIT : $renderer->doc .= ""; break; } return true; } return false; } /** * Validate color value $c * this is cut price validation - only to ensure the basic format is correct and there is nothing harmful * three basic formats "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])" */ private function _isValid($c) { $c = trim($c); $pattern = "/^\s*( ([a-zA-z]+)| #colorname - not verified (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))| #colorvalue (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\)) #rgb triplet )\s*$/x"; if (preg_match($pattern, $c)) return trim($c); return ""; } }