common.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @file divalign2/common.php
  4. * @brief Common functions for the divalign2 plugin.
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @version 5.0rc1
  7. * @date 2020-06-11
  8. * @author Luis Machuca Bezzaza <lambalicious [at] tuta [dot] io>
  9. *
  10. * This file and the files in syntax/ provide the syntax mode for the
  11. * divalign2 plugin.
  12. *
  13. * This work is a form from previous plugin (plugin:divalign)
  14. * by Jason Byrne. Check the wikipage for details.
  15. */
  16. // must be run within DokuWiki
  17. if(!defined('DOKU_INC')) die();
  18. if(!defined('DOKU_PLUGIN')) die();
  19. if(!defined('DW_LF')) define('DW_LF',"\n");
  20. if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  21. require_once(DOKU_PLUGIN.'syntax.php');
  22. /**
  23. * All DokuWiki plugins to extend the parser/rendering mechanism
  24. * need to inherit from this class
  25. */
  26. class syntax_plugin_divalign2_common extends DokuWiki_Syntax_Plugin {
  27. function getSort() {
  28. return 180;
  29. }
  30. function getType() {
  31. return 'formatting';
  32. }
  33. function getAllowedTypes() {
  34. return array(
  35. 'container', 'substition', 'protected',
  36. 'disabled', 'formatting', 'paragraphs'
  37. );
  38. }
  39. function getPType() {
  40. return 'block';
  41. }
  42. function connectTo($mode) {
  43. }
  44. function postConnect() {
  45. }
  46. function handle($match, $state, $pos, Doku_Handler $handler){
  47. // unpack and process
  48. $content= $match['content'];
  49. $align= $match['align'];
  50. switch ( $state ) {
  51. case DOKU_LEXER_ENTER: {
  52. break;
  53. }
  54. case DOKU_LEXER_UNMATCHED: {
  55. $handler->_addCall('cdata', array($content), $pos);
  56. break;
  57. }
  58. }
  59. return array($align,$state,$pos);
  60. }
  61. function render($mode, Doku_Renderer $renderer, $data) {
  62. list ($align, $state, $pos) = $data;
  63. if (false) {
  64. } else if (in_array($mode, ['xhtml', 's5', 'purplenumbers', 'rplus', 'html5'])) {
  65. switch ($state) {
  66. case DOKU_LEXER_ENTER: {
  67. if ($align) {
  68. $renderer->doc .= '<p class="divalign-'.$align.'">';
  69. }
  70. break;
  71. }
  72. case DOKU_LEXER_EXIT : {
  73. $renderer->doc .= '</p><!--divalign-->'. DW_LF;
  74. break;
  75. }
  76. } // end switch
  77. return true;
  78. } else if ($mode=='odt') {
  79. if (!method_exists ($renderer, 'getODTPropertiesFromElement')) {
  80. $this->render_odt_v1 ($renderer, $state, $align);
  81. } else {
  82. $this->render_odt_v2 ($renderer, $state, $align);
  83. }
  84. return true;
  85. }
  86. return false;
  87. }
  88. function render_odt_v1 (Doku_Renderer $renderer, $state, $align) {
  89. $Align = ucfirst ($align);
  90. //static $center_defined= false;
  91. $st = <<<EOF
  92. <style:style style:name="Text.Divalign.$Align" style:display-name="Text.Divalign.$Align" style:family="paragraph" style:parent-style-name="Text_20_body">
  93. <style:paragraph-properties fo:text-align="$align" style:justify-single-word="false" />
  94. </style:style>
  95. EOF;
  96. $renderer->autostyles["Text.Divalign.$Align"]= $st;
  97. $center_defined= true;
  98. switch ($state) {
  99. case DOKU_LEXER_ENTER: {
  100. $renderer->doc.= "<text:p text:style-name=\"Text.Divalign.$Align\">";
  101. break;
  102. }
  103. case DOKU_LEXER_EXIT: {
  104. $renderer->doc.= '</text:p>';
  105. //reduce_odt();
  106. break;
  107. }
  108. } // end switch
  109. }
  110. function render_odt_v2 (Doku_Renderer $renderer, $state, $align) {
  111. static $first = true;
  112. $alignments = array ('left', 'right', 'center', 'justify');
  113. if ($first) {
  114. // First entrance of the function. Create our ODT styles.
  115. // Group them under a parent called "Plugin DivAlign2"
  116. $first = false;
  117. // Create parent style to group the others beneath it
  118. if (!$renderer->styleExists('Plugin_DivAlign2')) {
  119. $parent_properties = array();
  120. $parent_properties ['style-parent'] = 'Text_20_body';
  121. $parent_properties ['style-class'] = 'Plugin_DivAlign2';
  122. $parent_properties ['style-name'] = 'Plugin_DivAlign2';
  123. $parent_properties ['style-display-name'] = 'Plugin DivAlign2';
  124. $renderer->createParagraphStyle($parent_properties);
  125. }
  126. $properties = array ();
  127. $properties ['justify-single-word'] = 'false';
  128. $properties ['style-class'] = NULL;
  129. $properties ['style-parent'] = 'Plugin_DivAlign2';
  130. foreach ($alignments as $alignment) {
  131. $Align = ucfirst ($alignment);
  132. $name = 'Plugin DivAlign2 '.$Align;
  133. $style_name = 'Plugin_DivAlign2_'.$Align;
  134. if (!$renderer->styleExists($style_name)) {
  135. $properties ['style-name'] = $style_name;
  136. $properties ['style-display-name'] = $name;
  137. $properties ['text-align'] = $alignment;
  138. $renderer->createParagraphStyle($properties);
  139. }
  140. }
  141. }
  142. $Align = ucfirst ($align);
  143. switch ($state) {
  144. case DOKU_LEXER_ENTER:
  145. $renderer->p_close();
  146. $renderer->p_open('Plugin_DivAlign2_'.$Align);
  147. break;
  148. case DOKU_LEXER_EXIT:
  149. $renderer->p_close();
  150. break;
  151. }
  152. }
  153. } // end class