1<?php 2/** 3 * DokuWiki Plugin colorswatch (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Henning Kockerbeck <henning.kockerbeck@isatis-online.de> 7 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) { 11 die(); 12} 13 14class syntax_plugin_colorswatch_colorswatch extends DokuWiki_Syntax_Plugin 15{ 16 /** 17 * @return string Syntax mode type 18 */ 19 public function getType() 20 { 21 return 'substition'; 22 } 23 24 /** 25 * @return string Paragraph type 26 */ 27 public function getPType() 28 { 29 return 'normal'; 30 } 31 32 /** 33 * @return int Sort order - Low numbers go before high numbers 34 */ 35 public function getSort() 36 { 37 // we don't really use this 38 // just use constant from code plugin 39 return 195; 40 } 41 42 /** 43 * Connect lookup pattern to lexer. 44 * 45 * @param string $mode Parser mode 46 */ 47 public function connectTo($mode) 48 { 49 # color codes #rrggbb 50 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{6}>', $mode, 'plugin_colorswatch_colorswatch'); 51 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{6}:[\p{Xwd}_ -]+>', $mode, 'plugin_colorswatch_colorswatch'); 52 53 # color codes #rrggbbaa 54 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{8}>', $mode, 'plugin_colorswatch_colorswatch'); 55 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{8}:[\p{Xwd}_ -]+>', $mode, 'plugin_colorswatch_colorswatch'); 56 57 # color codes #rgb 58 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{3}>', $mode, 'plugin_colorswatch_colorswatch'); 59 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{3}:[\p{Xwd}_ -]+>', $mode, 'plugin_colorswatch_colorswatch'); 60 61 # color codes #rgba 62 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{4}>', $mode, 'plugin_colorswatch_colorswatch'); 63 $this->Lexer->addSpecialPattern('<colorswatch #[0-9a-fA-F]{4}:[\p{Xwd}_ -]+>', $mode, 'plugin_colorswatch_colorswatch'); 64 } 65 66 /** 67 * Handle matches of the colorswatch syntax 68 * 69 * @param string $match The match of the syntax 70 * @param int $state The state of the handler 71 * @param int $pos The position in the document 72 * @param Doku_Handler $handler The handler 73 * 74 * @return array Data for the renderer 75 */ 76 public function handle($match, $state, $pos, Doku_Handler $handler) 77 { 78 $data = array(); 79 80 if ($state == DOKU_LEXER_SPECIAL) 81 { 82 preg_match('/<colorswatch (#[0-9a-fA-F]{3,8})(:([\p{Xwd}_ -]+))?>/', $match, $match_data); 83 $data['code'] = $match_data[1]; 84 $data['name'] = $match_data[3]; 85 } 86 87 return $data; 88 } 89 90 /** 91 * Render xhtml output or metadata 92 * 93 * @param string $mode Renderer mode (supported modes: xhtml) 94 * @param Doku_Renderer $renderer The renderer 95 * @param array $data The data from the handler() function 96 * 97 * @return bool If rendering was successful. 98 */ 99 public function render($mode, Doku_Renderer $renderer, $data) 100 { 101 if ($mode !== 'xhtml') { 102 return false; 103 } 104 105 $code = hsc($data['code']); 106 107 if ($data['name'] != '') 108 { 109 $name = hsc($data['name']); 110 } 111 else 112 { 113 $name = $code; // It's already escaped above. 114 } 115 116 $size_class = $this->getConf('colorswatch_size'); 117 118 $renderer->doc .= <<<EOT 119<div class="colorswatch $size_class"><div class="colorswatch_swatch" style="background-color: $code;"> </div><div class="colorswatch_info">$name</div></div> 120EOT; 121 122 return true; 123 } 124} 125 126