SyntaxPlugin.php 4.1 KB

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