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
17// must be run within DokuWiki
18if(!defined('DOKU_INC')) die();
19if(!defined('DOKU_PLUGIN')) die();
20if(!defined('DW_LF')) define('DW_LF',"\n");
21if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
22require_once(DOKU_PLUGIN.'syntax.php');
23
24/**
25 * All DokuWiki plugins to extend the parser/rendering mechanism
26 * need to inherit from this class
27 */
28class syntax_plugin_divalign2_common extends DokuWiki_Syntax_Plugin {
29
30    function getSort() {
31        return 180;
32    }
33
34    function getType() {
35        return 'formatting';
36    }
37
38    function getAllowedTypes() {
39        return array(
40         'container', 'substition', 'protected',
41         'disabled', 'formatting', 'paragraphs'
42         );
43    }
44
45    function getPType() {
46        return 'block';
47    }
48
49    function connectTo($mode) {
50    }
51
52    function postConnect() {
53    }
54
55    function handle($match, $state, $pos, Doku_Handler $handler){
56        // unpack and process
57        $content= $match['content'];
58        $align= $match['align'];
59        switch ( $state ) {
60          case DOKU_LEXER_ENTER: {
61            break;
62            }
63          case DOKU_LEXER_UNMATCHED: {
64            $handler->_addCall('cdata', array($content), $pos);
65            break;
66            }
67        }
68        return array($align,$state,$pos);
69    }
70
71    function render($mode, Doku_Renderer $renderer, $data) {
72        list ($align, $state, $pos) = $data;
73        if (false) {
74
75        } else if (in_array($mode, ['xhtml', 's5', 'purplenumbers', 'rplus', 'html5'])) {
76
77            switch ($state) {
78            case DOKU_LEXER_ENTER: {
79                if ($align) {
80                    $renderer->doc .= '<p class="divalign-'.$align.'">';
81                    }
82                break;
83            }
84            case DOKU_LEXER_EXIT : {
85                $renderer->doc .= '</p><!--divalign-->'. DW_LF;
86                break;
87            }
88            } // end switch
89            return true;
90
91        } else if ($mode=='odt') {
92            if (!method_exists ($renderer, 'getODTPropertiesFromElement')) {
93                $this->render_odt_v1 ($renderer, $state, $align);
94            } else {
95                $this->render_odt_v2 ($renderer, $state, $align);
96            }
97            return true;
98        }
99
100        return false;
101    }
102
103    function render_odt_v1 (Doku_Renderer $renderer, $state, $align) {
104        $Align = ucfirst ($align);
105        //static $center_defined= false;
106        $st = <<<EOF
107<style:style style:name="Text.Divalign.$Align" style:display-name="Text.Divalign.$Align" style:family="paragraph" style:parent-style-name="Text_20_body">
108<style:paragraph-properties fo:text-align="$align" style:justify-single-word="false" />
109</style:style>
110
111EOF;
112
113        $renderer->autostyles["Text.Divalign.$Align"]= $st;
114        $center_defined= true;
115        switch ($state) {
116        case DOKU_LEXER_ENTER: {
117            $renderer->doc.= "<text:p text:style-name=\"Text.Divalign.$Align\">";
118            break;
119        }
120        case DOKU_LEXER_EXIT: {
121            $renderer->doc.= '</text:p>';
122            //reduce_odt();
123            break;
124        }
125        } // end switch
126    }
127
128    function render_odt_v2 (Doku_Renderer $renderer, $state, $align) {
129        static $first = true;
130        $alignments = array ('left', 'right', 'center', 'justify');
131
132        if ($first) {
133            // First entrance of the function. Create our ODT styles.
134            // Group them under a parent called "Plugin DivAlign2"
135            $first = false;
136
137            // Create parent style to group the others beneath it
138            if (!$renderer->styleExists('Plugin_DivAlign2')) {
139                $parent_properties = array();
140                $parent_properties ['style-parent'] = 'Text_20_body';
141                $parent_properties ['style-class'] = 'Plugin_DivAlign2';
142                $parent_properties ['style-name'] = 'Plugin_DivAlign2';
143                $parent_properties ['style-display-name'] = 'Plugin DivAlign2';
144                $renderer->createParagraphStyle($parent_properties);
145            }
146
147            $properties = array ();
148            $properties ['justify-single-word'] = 'false';
149            $properties ['style-class'] = NULL;
150            $properties ['style-parent'] = 'Plugin_DivAlign2';
151            foreach ($alignments as $alignment) {
152                $Align = ucfirst ($alignment);
153                $name = 'Plugin DivAlign2 '.$Align;
154                $style_name = 'Plugin_DivAlign2_'.$Align;
155                if (!$renderer->styleExists($style_name)) {
156                    $properties ['style-name'] = $style_name;
157                    $properties ['style-display-name'] = $name;
158                    $properties ['text-align'] = $alignment;
159                    $renderer->createParagraphStyle($properties);
160                }
161            }
162        }
163
164        $Align = ucfirst ($align);
165        switch ($state) {
166            case DOKU_LEXER_ENTER:
167                $renderer->p_close();
168                $renderer->p_open('Plugin_DivAlign2_'.$Align);
169                break;
170            case DOKU_LEXER_EXIT:
171                $renderer->p_close();
172                break;
173        }
174    }
175
176} // end class
177
178
179