xref: /plugin/today/syntax/today.php (revision c2de205a397cb4b27109538cf7248712fb4aa6e0)
18fb84481SMichael Große<?php
28fb84481SMichael Große
38fb84481SMichael Großedeclare(strict_types=1);
48fb84481SMichael Große
58fb84481SMichael Großeuse dokuwiki\Extension\SyntaxPlugin;
68fb84481SMichael Große
78fb84481SMichael Große/**
88fb84481SMichael Große * DokuWiki Plugin today (Syntax Component)
98fb84481SMichael Große *
108fb84481SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
118fb84481SMichael Große * @author  Michael Große <mic.grosse+dokuwiki@googlemail.com>
128fb84481SMichael Große */
138fb84481SMichael Großefinal class syntax_plugin_today_today extends SyntaxPlugin
148fb84481SMichael Große{
158fb84481SMichael Große    /**
168fb84481SMichael Große     * @return string Syntax mode type
178fb84481SMichael Große     */
188fb84481SMichael Große    public function getType(): string
198fb84481SMichael Große    {
208fb84481SMichael Große        return 'substition';
218fb84481SMichael Große    }
228fb84481SMichael Große
238fb84481SMichael Große    /**
248fb84481SMichael Große     * @return string Paragraph type
258fb84481SMichael Große     */
268fb84481SMichael Große    public function getPType(): string
278fb84481SMichael Große    {
288fb84481SMichael Große        return 'normal';
298fb84481SMichael Große    }
308fb84481SMichael Große
318fb84481SMichael Große    /**
328fb84481SMichael Große     * @return int Sort order - Low numbers go before high numbers
338fb84481SMichael Große     */
348fb84481SMichael Große    public function getSort(): int
358fb84481SMichael Große    {
368fb84481SMichael Große        return 1000;
378fb84481SMichael Große    }
388fb84481SMichael Große
398fb84481SMichael Große    /**
408fb84481SMichael Große     * Connect lookup pattern to lexer.
418fb84481SMichael Große     *
428fb84481SMichael Große     * @param string $mode Parser mode
438fb84481SMichael Große     */
448fb84481SMichael Große    public function connectTo($mode): void
458fb84481SMichael Große    {
468fb84481SMichael Große        $this->Lexer->addSpecialPattern('\{today.*?\}', $mode, 'plugin_today_today');
478fb84481SMichael Große    }
488fb84481SMichael Große
498fb84481SMichael Große    /**
508fb84481SMichael Große     * Handle matches of the today syntax
518fb84481SMichael Große     *
528fb84481SMichael Große     * @param string       $match   The match of the syntax
538fb84481SMichael Große     * @param int          $state   The state of the handler
548fb84481SMichael Große     * @param int          $pos     The position in the document
558fb84481SMichael Große     * @param Doku_Handler $handler The handler
568fb84481SMichael Große     *
578fb84481SMichael Große     * @return array Data for the renderer
588fb84481SMichael Große     */
598fb84481SMichael Große    public function handle($match, $state, $pos, Doku_Handler $handler): array
608fb84481SMichael Große    {
61*c2de205aSMichael Große        $data = trim(substr($match, strlen('{today'), -1));
62*c2de205aSMichael Große        $parts = explode(' ', $data);
638fb84481SMichael Große
64*c2de205aSMichael Große        return [
65*c2de205aSMichael Große            'namespace' => $parts[0] ?? null,
66*c2de205aSMichael Große            'format' => $parts[1] ?? null,
67*c2de205aSMichael Große        ];
688fb84481SMichael Große    }
698fb84481SMichael Große
708fb84481SMichael Große    /**
718fb84481SMichael Große     * Render xhtml output or metadata
728fb84481SMichael Große     *
738fb84481SMichael Große     * @param string        $mode     Renderer mode (supported modes: xhtml)
748fb84481SMichael Große     * @param Doku_Renderer $renderer The renderer
758fb84481SMichael Große     * @param array         $data     The data from the handler() function
768fb84481SMichael Große     *
778fb84481SMichael Große     * @return bool If rendering was successful.
788fb84481SMichael Große     */
798fb84481SMichael Große    public function render($mode, Doku_Renderer $renderer, $data): bool
808fb84481SMichael Große    {
818fb84481SMichael Große        if ($mode !== 'xhtml') {
828fb84481SMichael Große            return false;
838fb84481SMichael Große        }
848fb84481SMichael Große
85*c2de205aSMichael Große        $namespace = $data['namespace'] ?? '';
86*c2de205aSMichael Große        $format = $data['format'] ?? 'Y-m-d';
87*c2de205aSMichael Große        $today = date($format);
88*c2de205aSMichael Große        $pageId = $namespace . ':' . $today;
898fb84481SMichael Große        $renderer->internallink($pageId, 'today');
908fb84481SMichael Große
918fb84481SMichael Große        return true;
928fb84481SMichael Große    }
938fb84481SMichael Große}
94