xref: /plugin/dwtimeline/syntax/milestone.php (revision adfdabd7bf1d07e505f4aef1b04dc0290dd26249)
185220f43Ssaggi-dw<?php
2545c554bSsaggi-dw
385220f43Ssaggi-dw/**
485220f43Ssaggi-dw * DokuWiki Plugin dwtimeline (Syntax Component)
585220f43Ssaggi-dw *
685220f43Ssaggi-dw * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
785220f43Ssaggi-dw * @author  saggi <saggi@gmx.de>
885220f43Ssaggi-dw */
94ff971beSsaggi-dw
10c78eb039Ssaggi-dwclass syntax_plugin_dwtimeline_milestone extends syntax_plugin_dwtimeline_dwtimeline
1185220f43Ssaggi-dw{
1285220f43Ssaggi-dw    /** @inheritDoc */
1385220f43Ssaggi-dw    public function getType()
1485220f43Ssaggi-dw    {
152ddb0813SSteffen Sagert        return 'plugin_dwtimeline_milestone';
1685220f43Ssaggi-dw    }
1785220f43Ssaggi-dw
18545c554bSsaggi-dw    public function accepts($mode)
19545c554bSsaggi-dw    {
20545c554bSsaggi-dw        if ($mode == 'plugin_dwtimeline_timeline') {
21545c554bSsaggi-dw            return true;
22545c554bSsaggi-dw        }
232ddb0813SSteffen Sagert        return parent::accepts($mode);
2485220f43Ssaggi-dw    }
2585220f43Ssaggi-dw
2685220f43Ssaggi-dw    /**
2785220f43Ssaggi-dw     * @return array Things that may be inside the syntax
2885220f43Ssaggi-dw     */
29545c554bSsaggi-dw    public function getAllowedTypes()
30545c554bSsaggi-dw    {
31*adfdabd7Ssaggi-dw        return ['container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'];
3285220f43Ssaggi-dw    }
3385220f43Ssaggi-dw
3485220f43Ssaggi-dw    /**
3585220f43Ssaggi-dw     * Set the EntryPattern
364fa647deSsaggi-dw     * @param string $mode
3785220f43Ssaggi-dw     */
3885220f43Ssaggi-dw    public function connectTo($mode)
3985220f43Ssaggi-dw    {
40545c554bSsaggi-dw        $pattern = '<milestone\b(?:[^>"\']+|"(?:[^"\\\\]|\\\\.)*"|\'(?:[^\'\\\\]|\\\\.)*\')*>(?=.*?\</milestone>)';
41545c554bSsaggi-dw        $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_dwtimeline_milestone');
4208c5c745SSteffen Sagert    }
4385220f43Ssaggi-dw
4485220f43Ssaggi-dw    /**
4585220f43Ssaggi-dw     * Set the ExitPattern
4685220f43Ssaggi-dw     */
4785220f43Ssaggi-dw    public function postConnect()
4885220f43Ssaggi-dw    {
49545c554bSsaggi-dw        $this->Lexer->addExitPattern('</milestone\>', 'plugin_dwtimeline_milestone');
5085220f43Ssaggi-dw    }
5185220f43Ssaggi-dw
5285220f43Ssaggi-dw    /**
5385220f43Ssaggi-dw     * Handle the match
544fa647deSsaggi-dw     * @param string       $match   The match of the syntax
554fa647deSsaggi-dw     * @param int          $state   The state of the handler
564fa647deSsaggi-dw     * @param int          $pos     The position in the document
574fa647deSsaggi-dw     * @param Doku_Handler $handler The handler
584fa647deSsaggi-dw     * @return array Data for the renderer
5985220f43Ssaggi-dw     */
6085220f43Ssaggi-dw    public function handle($match, $state, $pos, Doku_Handler $handler)
6185220f43Ssaggi-dw    {
6285220f43Ssaggi-dw        switch ($state) {
6385220f43Ssaggi-dw            case DOKU_LEXER_ENTER:
6485220f43Ssaggi-dw                $match         = trim(substr($match, 10, -1));// returns match between <milestone(10) and >(-1)
65c78eb039Ssaggi-dw                $data          = $this->getTitleMatches($match);
66c78eb039Ssaggi-dw                $data['align'] = parent::$align;
67545c554bSsaggi-dw                return [$state, $data];
6885220f43Ssaggi-dw            case DOKU_LEXER_UNMATCHED:
69545c554bSsaggi-dw                return [$state, $match];
7085220f43Ssaggi-dw            case DOKU_LEXER_EXIT:
71545c554bSsaggi-dw                return [$state, ''];
7285220f43Ssaggi-dw        }
73545c554bSsaggi-dw        return [];
7485220f43Ssaggi-dw    }
7585220f43Ssaggi-dw
7685220f43Ssaggi-dw    /**
774fa647deSsaggi-dw     * Create output
784fa647deSsaggi-dw     *
794fa647deSsaggi-dw     * @param string        $mode     string     output format being rendered
804fa647deSsaggi-dw     * @param Doku_Renderer $renderer the current renderer object
814fa647deSsaggi-dw     * @param array         $data     data created by handler()
82545c554bSsaggi-dw     * @return  bool                 rendered correctly?
8385220f43Ssaggi-dw     */
8485220f43Ssaggi-dw    public function render($mode, Doku_Renderer $renderer, $data)
8585220f43Ssaggi-dw    {
8685220f43Ssaggi-dw        if ($mode == 'xhtml') {
87545c554bSsaggi-dw            if (!parent::$direction) {
88545c554bSsaggi-dw                parent::$direction = $this->getDirection();
89545c554bSsaggi-dw            }
90*adfdabd7Ssaggi-dw            [$state, $indata] = $data;
9185220f43Ssaggi-dw            switch ($state) {
9285220f43Ssaggi-dw                case DOKU_LEXER_ENTER:
93545c554bSsaggi-dw                    $renderer->doc .= '<div class="container-' . $indata['align'] . ' ';
94545c554bSsaggi-dw                    $renderer->doc .= parent::$direction . '"' . $indata['data'] . $indata['style'] . '>' . DOKU_LF;
9508c5c745SSteffen Sagert                    $renderer->doc .= '<div class="tlcontent">' . DOKU_LF;
964ff971beSsaggi-dw                    if (isset($indata['title'])) {
97545c554bSsaggi-dw                        if (!empty($indata['link'])) {
98545c554bSsaggi-dw                            // get back raw title-value
99545c554bSsaggi-dw                            $label = htmlspecialchars_decode($indata['title'], ENT_QUOTES);
100545c554bSsaggi-dw
101545c554bSsaggi-dw                            // create link with label
102545c554bSsaggi-dw                            $wikilink = '[[' . $indata['link'] . '|' . $label . ']]';
103545c554bSsaggi-dw
104545c554bSsaggi-dw                            // render link
105545c554bSsaggi-dw                            $info          = [];
106545c554bSsaggi-dw                            $renderer->doc .= '<div class="mstitle">'
107545c554bSsaggi-dw                                . p_render('xhtml', p_get_instructions($wikilink), $info)
108545c554bSsaggi-dw                                . '</div>' . DOKU_LF;
10985220f43Ssaggi-dw                        } else {
1104ff971beSsaggi-dw                            $renderer->doc .= '<div class="mstitle">' . $indata['title'] . '</div>' . DOKU_LF;
11185220f43Ssaggi-dw                        }
11285220f43Ssaggi-dw                    }
113545c554bSsaggi-dw                    if (isset($indata['description'])) {
114545c554bSsaggi-dw                        $renderer->doc .= '<div class="msdesc">' . $indata['description'] . '</div>' . DOKU_LF;
115545c554bSsaggi-dw                    }
11685220f43Ssaggi-dw                    break;
11785220f43Ssaggi-dw                case DOKU_LEXER_UNMATCHED:
118545c554bSsaggi-dw                    $renderer->cdata($indata);
11985220f43Ssaggi-dw                    break;
12085220f43Ssaggi-dw                case DOKU_LEXER_EXIT:
12185220f43Ssaggi-dw                    $renderer->doc     .= '</div>' . DOKU_LF;
12285220f43Ssaggi-dw                    $renderer->doc     .= '</div>' . DOKU_LF;
123545c554bSsaggi-dw                    parent::$direction = $this->changeDirection(parent::$direction);
12485220f43Ssaggi-dw                    break;
12585220f43Ssaggi-dw            }
12685220f43Ssaggi-dw            return true;
12785220f43Ssaggi-dw        }
12885220f43Ssaggi-dw        return false;
12985220f43Ssaggi-dw    }
13085220f43Ssaggi-dw}
131