xref: /plugin/today/syntax/today.php (revision 8fb844810886317568445185abdf37f8d307bc56)
1*8fb84481SMichael Große<?php
2*8fb84481SMichael Große
3*8fb84481SMichael Großedeclare(strict_types=1);
4*8fb84481SMichael Große
5*8fb84481SMichael Großeuse dokuwiki\Extension\SyntaxPlugin;
6*8fb84481SMichael Große
7*8fb84481SMichael Große/**
8*8fb84481SMichael Große * DokuWiki Plugin today (Syntax Component)
9*8fb84481SMichael Große *
10*8fb84481SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11*8fb84481SMichael Große * @author  Michael Große <mic.grosse+dokuwiki@googlemail.com>
12*8fb84481SMichael Große */
13*8fb84481SMichael Großefinal class syntax_plugin_today_today extends SyntaxPlugin
14*8fb84481SMichael Große{
15*8fb84481SMichael Große    /**
16*8fb84481SMichael Große     * @return string Syntax mode type
17*8fb84481SMichael Große     */
18*8fb84481SMichael Große    public function getType(): string
19*8fb84481SMichael Große    {
20*8fb84481SMichael Große        return 'substition';
21*8fb84481SMichael Große    }
22*8fb84481SMichael Große
23*8fb84481SMichael Große    /**
24*8fb84481SMichael Große     * @return string Paragraph type
25*8fb84481SMichael Große     */
26*8fb84481SMichael Große    public function getPType(): string
27*8fb84481SMichael Große    {
28*8fb84481SMichael Große        return 'normal';
29*8fb84481SMichael Große    }
30*8fb84481SMichael Große
31*8fb84481SMichael Große    /**
32*8fb84481SMichael Große     * @return int Sort order - Low numbers go before high numbers
33*8fb84481SMichael Große     */
34*8fb84481SMichael Große    public function getSort(): int
35*8fb84481SMichael Große    {
36*8fb84481SMichael Große        return 1000;
37*8fb84481SMichael Große    }
38*8fb84481SMichael Große
39*8fb84481SMichael Große    /**
40*8fb84481SMichael Große     * Connect lookup pattern to lexer.
41*8fb84481SMichael Große     *
42*8fb84481SMichael Große     * @param string $mode Parser mode
43*8fb84481SMichael Große     */
44*8fb84481SMichael Große    public function connectTo($mode): void
45*8fb84481SMichael Große    {
46*8fb84481SMichael Große        $this->Lexer->addSpecialPattern('\{today.*?\}', $mode, 'plugin_today_today');
47*8fb84481SMichael Große    }
48*8fb84481SMichael Große
49*8fb84481SMichael Große    /**
50*8fb84481SMichael Große     * Handle matches of the today syntax
51*8fb84481SMichael Große     *
52*8fb84481SMichael Große     * @param string       $match   The match of the syntax
53*8fb84481SMichael Große     * @param int          $state   The state of the handler
54*8fb84481SMichael Große     * @param int          $pos     The position in the document
55*8fb84481SMichael Große     * @param Doku_Handler $handler The handler
56*8fb84481SMichael Große     *
57*8fb84481SMichael Große     * @return array Data for the renderer
58*8fb84481SMichael Große     */
59*8fb84481SMichael Große    public function handle($match, $state, $pos, Doku_Handler $handler): array
60*8fb84481SMichael Große    {
61*8fb84481SMichael Große        $data = [];
62*8fb84481SMichael Große
63*8fb84481SMichael Große        $namespace = trim(substr($match, strlen('{today'), -1));
64*8fb84481SMichael Große        $data['namespace'] = $namespace;
65*8fb84481SMichael Große
66*8fb84481SMichael Große        return $data;
67*8fb84481SMichael Große    }
68*8fb84481SMichael Große
69*8fb84481SMichael Große    /**
70*8fb84481SMichael Große     * Render xhtml output or metadata
71*8fb84481SMichael Große     *
72*8fb84481SMichael Große     * @param string        $mode     Renderer mode (supported modes: xhtml)
73*8fb84481SMichael Große     * @param Doku_Renderer $renderer The renderer
74*8fb84481SMichael Große     * @param array         $data     The data from the handler() function
75*8fb84481SMichael Große     *
76*8fb84481SMichael Große     * @return bool If rendering was successful.
77*8fb84481SMichael Große     */
78*8fb84481SMichael Große    public function render($mode, Doku_Renderer $renderer, $data): bool
79*8fb84481SMichael Große    {
80*8fb84481SMichael Große        if ($mode !== 'xhtml') {
81*8fb84481SMichael Große            return false;
82*8fb84481SMichael Große        }
83*8fb84481SMichael Große
84*8fb84481SMichael Große        $today = date('Y-m-d');
85*8fb84481SMichael Große        $pageId = $data['namespace'] . ':' . $today;
86*8fb84481SMichael Große        $renderer->internallink($pageId, 'today');
87*8fb84481SMichael Große
88*8fb84481SMichael Große        return true;
89*8fb84481SMichael Große    }
90*8fb84481SMichael Große}
91