You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

135 lines
5.3 KiB

  1. <?php
  2. /**
  3. * Div Syntax Component of the Wrap Plugin
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Anika Henke <anika@selfthinker.org>
  7. */
  8. class syntax_plugin_wrap_div extends DokuWiki_Syntax_Plugin {
  9. protected $special_pattern = '<div\b[^>\r\n]*?/>';
  10. protected $entry_pattern = '<div\b.*?>(?=.*?</div>)';
  11. protected $exit_pattern = '</div>';
  12. function getType(){ return 'formatting';}
  13. function getAllowedTypes() { return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); }
  14. function getPType(){ return 'stack';}
  15. function getSort(){ return 195; }
  16. // override default accepts() method to allow nesting - ie, to get the plugin accepts its own entry syntax
  17. function accepts($mode) {
  18. if ($mode == substr(get_class($this), 7)) return true;
  19. return parent::accepts($mode);
  20. }
  21. /**
  22. * Connect pattern to lexer
  23. */
  24. function connectTo($mode) {
  25. $this->Lexer->addSpecialPattern($this->special_pattern,$mode,'plugin_wrap_'.$this->getPluginComponent());
  26. $this->Lexer->addEntryPattern($this->entry_pattern,$mode,'plugin_wrap_'.$this->getPluginComponent());
  27. }
  28. function postConnect() {
  29. $this->Lexer->addExitPattern($this->exit_pattern, 'plugin_wrap_'.$this->getPluginComponent());
  30. $this->Lexer->addPattern('[ \t]*={2,}[^\n]+={2,}[ \t]*(?=\n)', 'plugin_wrap_'.$this->getPluginComponent());
  31. }
  32. /**
  33. * Handle the match
  34. */
  35. function handle($match, $state, $pos, Doku_Handler $handler){
  36. global $conf;
  37. switch ($state) {
  38. case DOKU_LEXER_ENTER:
  39. case DOKU_LEXER_SPECIAL:
  40. $data = strtolower(trim(substr($match,strpos($match,' '),-1)," \t\n/"));
  41. return array($state, $data);
  42. case DOKU_LEXER_UNMATCHED:
  43. $handler->addCall('cdata', array($match), $pos);
  44. break;
  45. case DOKU_LEXER_MATCHED:
  46. // we have a == header ==, use the core header() renderer
  47. // (copied from core header() in inc/parser/handler.php)
  48. $title = trim($match);
  49. $level = 7 - strspn($title,'=');
  50. if($level < 1) $level = 1;
  51. $title = trim($title,'=');
  52. $title = trim($title);
  53. $handler->addCall('header',array($title,$level,$pos), $pos);
  54. // close the section edit the header could open
  55. if ($title && $level <= $conf['maxseclevel']) {
  56. $handler->addPluginCall('wrap_closesection', array(), DOKU_LEXER_SPECIAL, $pos, '');
  57. }
  58. break;
  59. case DOKU_LEXER_EXIT:
  60. return array($state, '');
  61. }
  62. return false;
  63. }
  64. /**
  65. * Create output
  66. */
  67. function render($format, Doku_Renderer $renderer, $indata) {
  68. static $type_stack = array ();
  69. if (empty($indata)) return false;
  70. list($state, $data) = $indata;
  71. if($format == 'xhtml'){
  72. /** @var Doku_Renderer_xhtml $renderer */
  73. switch ($state) {
  74. case DOKU_LEXER_ENTER:
  75. $sectionEditStartData = ['target' => 'plugin_wrap_start', 'hid' => ''];
  76. $sectionEditEndData = ['target' =>'plugin_wrap_end', 'hid' => ''];
  77. if (!defined('SEC_EDIT_PATTERN')) {
  78. // backwards-compatibility for Frusterick Manners (2017-02-19)
  79. $sectionEditStartData = 'plugin_wrap_start';
  80. $sectionEditEndData = 'plugin_wrap_end';
  81. }
  82. // add a section edit right at the beginning of the wrap output
  83. $renderer->startSectionEdit(0, $sectionEditStartData);
  84. $renderer->finishSectionEdit();
  85. // add a section edit for the end of the wrap output. This prevents the renderer
  86. // from closing the last section edit so the next section button after the wrap syntax will
  87. // include the whole wrap syntax
  88. $renderer->startSectionEdit(0, $sectionEditEndData);
  89. case DOKU_LEXER_SPECIAL:
  90. $wrap = $this->loadHelper('wrap');
  91. $attr = $wrap->buildAttributes($data, 'plugin_wrap');
  92. $renderer->doc .= '<div'.$attr.'>';
  93. if ($state == DOKU_LEXER_SPECIAL) $renderer->doc .= '</div>';
  94. break;
  95. case DOKU_LEXER_EXIT:
  96. $renderer->doc .= '</div>';
  97. $renderer->finishSectionEdit();
  98. break;
  99. }
  100. return true;
  101. }
  102. if($format == 'odt'){
  103. switch ($state) {
  104. case DOKU_LEXER_ENTER:
  105. $wrap = plugin_load('helper', 'wrap');
  106. array_push ($type_stack, $wrap->renderODTElementOpen($renderer, 'div', $data));
  107. break;
  108. case DOKU_LEXER_EXIT:
  109. $element = array_pop ($type_stack);
  110. $wrap = plugin_load('helper', 'wrap');
  111. $wrap->renderODTElementClose ($renderer, $element);
  112. break;
  113. }
  114. return true;
  115. }
  116. return false;
  117. }
  118. }