1<?php 2 3/** 4 * DokuWiki Plugin randomtable roller (Syntax Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Oscar Merida <oscar@oscarm.org> 8 */ 9class syntax_plugin_randomtables_roller extends \dokuwiki\Extension\SyntaxPlugin 10{ 11 /** @inheritDoc */ 12 public function getType(): string 13 { 14 return 'protected'; 15 } 16 17 /** @inheritDoc */ 18 public function getPType(): string 19 { 20 return 'block'; 21 } 22 23 /** @inheritDoc */ 24 public function getSort(): int 25 { 26 return 201; 27 } 28 29 /** @inheritDoc */ 30 public function connectTo($mode): void 31 { 32 $this->Lexer->addEntryPattern('\<ROLL_ON\s+', $mode, 'plugin_randomtables_roller'); 33 } 34 35 /** @inheritDoc */ 36 public function postConnect(): void 37 { 38 $this->Lexer->addExitPattern('>', 'plugin_randomtables_roller'); 39 } 40 41 /** @inheritDoc */ 42 public function handle($match, $state, $pos, Doku_Handler $handler): array 43 { 44 switch ($state) { 45 case DOKU_LEXER_UNMATCHED: 46 return [$state, $match]; 47 case DOKU_LEXER_EXIT: 48 return [$state, '']; 49 case DOKU_LEXER_ENTER: 50 default: 51 return [$state, $match]; 52 } 53 } 54 55 /** @inheritDoc */ 56 public function render($mode, Doku_Renderer $renderer, $data): bool 57 { 58 if ($mode !== 'xhtml') { 59 return false; 60 } 61 [$state, $match] = $data; 62 63 switch ($state) { 64 case DOKU_LEXER_ENTER: 65 $renderer->doc .= '<div class="randomtable-well">' . PHP_EOL; 66 break; 67 case DOKU_LEXER_UNMATCHED: 68 $id = $renderer->_xmlEntities($data[1]); 69 $renderer->doc .= '<button class="randomtable" data-src="' . $id . '" data-target="results-' 70 . $id . '">Roll</button><div id="results-' . $id . '" class="results"></div>'; 71 break; 72 case DOKU_LEXER_EXIT: 73 $renderer->doc .= '</div>' . PHP_EOL; 74 break; 75 } 76 return true; 77 } 78} 79