xref: /plugin/dwtimeline/syntax/timeline.php (revision adfdabd7bf1d07e505f4aef1b04dc0290dd26249)
1<?php
2
3/**
4 * DokuWiki Plugin dwtimeline (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  saggi <saggi@gmx.de>
8 */
9
10class syntax_plugin_dwtimeline_timeline extends syntax_plugin_dwtimeline_dwtimeline
11{
12    /**
13     * @return array Things that may be inside the syntax
14     */
15    public function getAllowedTypes()
16    {
17        return ['plugin_dwtimeline_milestone'];
18    }
19
20    /**
21     * Set the EntryPattern
22     * @param string $mode
23     */
24    public function connectTo($mode)
25    {
26        $this->Lexer->addEntryPattern(
27            '<dwtimeline\b.*?>(?=.*?</dwtimeline\b.*?>)',
28            $mode,
29            'plugin_dwtimeline_timeline'
30        );
31    }
32
33    /**
34     * Set the ExitPattern
35     */
36    public function postConnect()
37    {
38        $this->Lexer->addExitPattern('</dwtimeline\b.*?>', 'plugin_dwtimeline_timeline');
39    }
40
41    /**
42     * Handle the match
43     * @param string       $match   The match of the syntax
44     * @param int          $state   The state of the handler
45     * @param int          $pos     The position in the document
46     * @param Doku_Handler $handler The handler
47     * @return array Data for the renderer
48     */
49    public function handle($match, $state, $pos, Doku_Handler $handler)
50    {
51        switch ($state) {
52            case DOKU_LEXER_ENTER:
53                parent::$align = $this->getConf('align');
54                $match         = trim(substr($match, 11, -1));// returns match between <dwtimeline(11) and >(-1)
55                $data          = $this->getTitleMatches($match);
56                parent::$align = $data['align'];
57                return [$state, $data];
58            case DOKU_LEXER_UNMATCHED:
59                return [$state, $match];
60            case DOKU_LEXER_EXIT:
61                $match = trim(substr($match, 12, -1));//returns match between </dwtimeline(12) and >(-1)
62                $data  = $this->getTitleMatches($match);
63                return [$state, $data];
64        }
65        return [];
66    }
67
68    /**
69     * Create output
70     *
71     * @param string        $mode     string     output format being rendered
72     * @param Doku_Renderer $renderer the current renderer object
73     * @param array         $data     data created by handler()
74     * @return  bool                 rendered correctly?
75     */
76    public function render($mode, Doku_Renderer $renderer, $data)
77    {
78        if ($mode == 'xhtml') {
79            if (!parent::$direction) {
80                parent::$direction = $this->getDirection();
81            }
82            [$state, $indata] = $data;
83            switch ($state) {
84                case DOKU_LEXER_ENTER:
85                    $renderer->doc .= '<div class="dwtimeline">' . DOKU_LF;
86                    if ($indata['align'] === 'horz') {
87                        $renderer->doc .= '<div class="timeline-' . $indata['align'] . '-line"></div>' . DOKU_LF;
88                    }
89                    $renderer->doc .= '<div class="timeline-' . $indata['align'] . '">' . DOKU_LF;
90                    if (isset($indata['title']) || isset($indata['description'])) {
91                        $renderer->doc .= '<div class="container-' . $indata['align'] . ' tl-top">' . DOKU_LF;
92                        $renderer->doc .= '<div class="tlcontent">' . DOKU_LF;
93                        if (isset($indata['title'])) {
94                            $renderer->doc .= '<div class="tltitle">' . $indata['title'] . '</div>' . DOKU_LF;
95                        }
96                        if (isset($indata['description'])) {
97                            $renderer->doc .= '<p>' . DOKU_LF . $indata['description'] . DOKU_LF . '</p>' . DOKU_LF;
98                        }
99                        $renderer->doc .= '</div>' . DOKU_LF;
100                        $renderer->doc .= '</div>' . DOKU_LF;
101                    }
102                    break;
103                case DOKU_LEXER_UNMATCHED:
104                    $renderer->cdata($indata);
105                    break;
106                case DOKU_LEXER_EXIT:
107                    if (isset($indata['title']) || isset($indata['description'])) {
108                        $renderer->doc .= '<div class="container-' . $indata['align'] . ' tl-bottom">' . DOKU_LF;
109                        $renderer->doc .= '<div class="tlcontent">' . DOKU_LF;
110                        if (isset($indata['title'])) {
111                            $renderer->doc .= '<div class="tltitle">' . $indata['title'] . '</div>' . DOKU_LF;
112                        }
113                        if (isset($indata['description'])) {
114                            $renderer->doc .= '<p>' . $indata['description'] . '</p>' . DOKU_LF;
115                        }
116                        $renderer->doc .= '</div>' . DOKU_LF;
117                        $renderer->doc .= '</div>' . DOKU_LF;
118                        $renderer->doc .= '</div>' . DOKU_LF;
119                    }
120                    $renderer->doc     .= '</div>' . DOKU_LF;
121                    parent::$direction = 'tl-' . $this->getConf('direction');//Reset direction
122                    break;
123            }
124            return true;
125        }
126        return false;
127    }
128}
129