1e1d9dcc8SAndreas Gohr<?php 2e1d9dcc8SAndreas Gohr 3e1d9dcc8SAndreas Gohrnamespace dokuwiki\Extension; 4e1d9dcc8SAndreas Gohr 5*1490c177SAndreas Gohruse dokuwiki\Parsing\ParserMode\Plugin; 6903d43feSMichael Großeuse Doku_Handler; 7903d43feSMichael Großeuse Doku_Renderer; 8e1d9dcc8SAndreas Gohr 9e1d9dcc8SAndreas Gohr/** 10e1d9dcc8SAndreas Gohr * Syntax Plugin Prototype 11e1d9dcc8SAndreas Gohr * 12e1d9dcc8SAndreas Gohr * All DokuWiki plugins to extend the parser/rendering mechanism 13e1d9dcc8SAndreas Gohr * need to inherit from this class 14e1d9dcc8SAndreas Gohr * 15e1d9dcc8SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 16e1d9dcc8SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 17e1d9dcc8SAndreas Gohr */ 18*1490c177SAndreas Gohrabstract class SyntaxPlugin extends Plugin 19e1d9dcc8SAndreas Gohr{ 20e1d9dcc8SAndreas Gohr use PluginTrait; 21e1d9dcc8SAndreas Gohr 22e1d9dcc8SAndreas Gohr protected $allowedModesSetup = false; 23e1d9dcc8SAndreas Gohr 24e1d9dcc8SAndreas Gohr /** 25e1d9dcc8SAndreas Gohr * Syntax Type 26e1d9dcc8SAndreas Gohr * 27e1d9dcc8SAndreas Gohr * Needs to return one of the mode types defined in $PARSER_MODES in Parser.php 28e1d9dcc8SAndreas Gohr * 29e1d9dcc8SAndreas Gohr * @return string 30e1d9dcc8SAndreas Gohr */ 31e1d9dcc8SAndreas Gohr abstract public function getType(); 32e1d9dcc8SAndreas Gohr 33e1d9dcc8SAndreas Gohr /** 34e1d9dcc8SAndreas Gohr * Allowed Mode Types 35e1d9dcc8SAndreas Gohr * 36e1d9dcc8SAndreas Gohr * Defines the mode types for other dokuwiki markup that maybe nested within the 37e1d9dcc8SAndreas Gohr * plugin's own markup. Needs to return an array of one or more of the mode types 38e1d9dcc8SAndreas Gohr * defined in $PARSER_MODES in Parser.php 39e1d9dcc8SAndreas Gohr * 40e1d9dcc8SAndreas Gohr * @return array 41e1d9dcc8SAndreas Gohr */ 42e1d9dcc8SAndreas Gohr public function getAllowedTypes() 43e1d9dcc8SAndreas Gohr { 44*1490c177SAndreas Gohr return []; 45e1d9dcc8SAndreas Gohr } 46e1d9dcc8SAndreas Gohr 47e1d9dcc8SAndreas Gohr /** 48e1d9dcc8SAndreas Gohr * Paragraph Type 49e1d9dcc8SAndreas Gohr * 50e1d9dcc8SAndreas Gohr * Defines how this syntax is handled regarding paragraphs. This is important 51e1d9dcc8SAndreas Gohr * for correct XHTML nesting. Should return one of the following: 52e1d9dcc8SAndreas Gohr * 53e1d9dcc8SAndreas Gohr * 'normal' - The plugin can be used inside paragraphs 54e1d9dcc8SAndreas Gohr * 'block' - Open paragraphs need to be closed before plugin output 55e1d9dcc8SAndreas Gohr * 'stack' - Special case. Plugin wraps other paragraphs. 56e1d9dcc8SAndreas Gohr * 57e1d9dcc8SAndreas Gohr * @see Doku_Handler_Block 58e1d9dcc8SAndreas Gohr * 59e1d9dcc8SAndreas Gohr * @return string 60e1d9dcc8SAndreas Gohr */ 61e1d9dcc8SAndreas Gohr public function getPType() 62e1d9dcc8SAndreas Gohr { 63e1d9dcc8SAndreas Gohr return 'normal'; 64e1d9dcc8SAndreas Gohr } 65e1d9dcc8SAndreas Gohr 66e1d9dcc8SAndreas Gohr /** 67e1d9dcc8SAndreas Gohr * Handler to prepare matched data for the rendering process 68e1d9dcc8SAndreas Gohr * 69e1d9dcc8SAndreas Gohr * This function can only pass data to render() via its return value - render() 70e1d9dcc8SAndreas Gohr * may be not be run during the object's current life. 71e1d9dcc8SAndreas Gohr * 72e1d9dcc8SAndreas Gohr * Usually you should only need the $match param. 73e1d9dcc8SAndreas Gohr * 74e1d9dcc8SAndreas Gohr * @param string $match The text matched by the patterns 75e1d9dcc8SAndreas Gohr * @param int $state The lexer state for the match 76e1d9dcc8SAndreas Gohr * @param int $pos The character position of the matched text 77e1d9dcc8SAndreas Gohr * @param Doku_Handler $handler The Doku_Handler object 78e1d9dcc8SAndreas Gohr * @return bool|array Return an array with all data you want to use in render, false don't add an instruction 79e1d9dcc8SAndreas Gohr */ 80e1d9dcc8SAndreas Gohr abstract public function handle($match, $state, $pos, Doku_Handler $handler); 81e1d9dcc8SAndreas Gohr 82e1d9dcc8SAndreas Gohr /** 83e1d9dcc8SAndreas Gohr * Handles the actual output creation. 84e1d9dcc8SAndreas Gohr * 85e1d9dcc8SAndreas Gohr * The function must not assume any other of the classes methods have been run 86e1d9dcc8SAndreas Gohr * during the object's current life. The only reliable data it receives are its 87e1d9dcc8SAndreas Gohr * parameters. 88e1d9dcc8SAndreas Gohr * 89e1d9dcc8SAndreas Gohr * The function should always check for the given output format and return false 90e1d9dcc8SAndreas Gohr * when a format isn't supported. 91e1d9dcc8SAndreas Gohr * 92e1d9dcc8SAndreas Gohr * $renderer contains a reference to the renderer object which is 93e1d9dcc8SAndreas Gohr * currently handling the rendering. You need to use it for writing 94e1d9dcc8SAndreas Gohr * the output. How this is done depends on the renderer used (specified 95e1d9dcc8SAndreas Gohr * by $format 96e1d9dcc8SAndreas Gohr * 97e1d9dcc8SAndreas Gohr * The contents of the $data array depends on what the handler() function above 98e1d9dcc8SAndreas Gohr * created 99e1d9dcc8SAndreas Gohr * 100e1d9dcc8SAndreas Gohr * @param string $format output format being rendered 101e1d9dcc8SAndreas Gohr * @param Doku_Renderer $renderer the current renderer object 102e1d9dcc8SAndreas Gohr * @param array $data data created by handler() 103e1d9dcc8SAndreas Gohr * @return boolean rendered correctly? (however, returned value is not used at the moment) 104e1d9dcc8SAndreas Gohr */ 105e1d9dcc8SAndreas Gohr abstract public function render($format, Doku_Renderer $renderer, $data); 106e1d9dcc8SAndreas Gohr 107e1d9dcc8SAndreas Gohr /** 108e1d9dcc8SAndreas Gohr * There should be no need to override this function 109e1d9dcc8SAndreas Gohr * 110e1d9dcc8SAndreas Gohr * @param string $mode 111e1d9dcc8SAndreas Gohr * @return bool 112e1d9dcc8SAndreas Gohr */ 113e1d9dcc8SAndreas Gohr public function accepts($mode) 114e1d9dcc8SAndreas Gohr { 115e1d9dcc8SAndreas Gohr 116e1d9dcc8SAndreas Gohr if (!$this->allowedModesSetup) { 117e1d9dcc8SAndreas Gohr global $PARSER_MODES; 118e1d9dcc8SAndreas Gohr 119e1d9dcc8SAndreas Gohr $allowedModeTypes = $this->getAllowedTypes(); 120e1d9dcc8SAndreas Gohr foreach ($allowedModeTypes as $mt) { 121e1d9dcc8SAndreas Gohr $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]); 122e1d9dcc8SAndreas Gohr } 123e1d9dcc8SAndreas Gohr 124*1490c177SAndreas Gohr $idx = array_search(substr(get_class($this), 7), (array)$this->allowedModes, true); 125e1d9dcc8SAndreas Gohr if ($idx !== false) { 126e1d9dcc8SAndreas Gohr unset($this->allowedModes[$idx]); 127e1d9dcc8SAndreas Gohr } 128e1d9dcc8SAndreas Gohr $this->allowedModesSetup = true; 129e1d9dcc8SAndreas Gohr } 130e1d9dcc8SAndreas Gohr 131e1d9dcc8SAndreas Gohr return parent::accepts($mode); 132e1d9dcc8SAndreas Gohr } 133e1d9dcc8SAndreas Gohr} 134