1<?php
2
3use dokuwiki\Extension\SyntaxPlugin;
4
5/**
6 * DokuWiki Plugin iconify (Syntax Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author Tokano <contact@tokano.fr>
10 */
11class syntax_plugin_iconify extends SyntaxPlugin
12{
13    /** @inheritDoc */
14    public function getType()
15    {
16        return 'substition';
17    }
18
19    /** @inheritDoc */
20    public function getPType()
21    {
22        return 'block';
23    }
24
25    /** @inheritDoc */
26    public function getSort()
27    {
28        return 999; // Load late
29    }
30
31    /** @inheritDoc */
32    public function connectTo($mode)
33    {
34        $this->Lexer->addSpecialPattern('<iconify [^>]+>', $mode, 'plugin_iconify');
35    }
36
37    /** @inheritDoc */
38    public function handle($match, $state, $pos, Doku_Handler $handler)
39    {
40        $params = trim(substr($match, 8, -1));
41
42        // Parse parameters
43        $parts = preg_split('/\s+/', $params);
44        $icon = $parts[0]; // First part is the icon name
45        $color = null;
46        $size = null;
47
48        // Check for color and size parameters
49        foreach ($parts as $part) {
50            if (strpos($part, 'color=') === 0) {
51                $color = substr($part, 6);
52            } elseif (strpos($part, 'size=') === 0) {
53                $size = substr($part, 5);
54            }
55        }
56
57        return [
58            "icon" => $icon,
59            "color" => $color,
60            "size" => $size
61        ];
62    }
63
64    /** @inheritDoc */
65    public function render($mode, Doku_Renderer $renderer, $data)
66    {
67        if ($mode !== 'xhtml') {
68            return false;
69        }
70
71        $icon = htmlspecialchars($data['icon']);
72        $style = "";
73
74        // Add default size from configuration if size is not set
75        $defaultSize = $this->getConf('default_size') ?: '24px'; // Default size is 24px
76        $size = $data['size'] ?: $defaultSize;
77
78        // Generate style for size and color
79        if (!empty($size)) {
80            $style .= "font-size: $size;";
81        }
82        if (!empty($data['color'])) {
83            $color = htmlspecialchars($data['color']);
84            $style .= "color: $color;";
85        }
86
87        $renderer->doc .= "<span class='iconify' data-icon='$icon' style='$style'></span>";
88
89        return true;
90    }
91}
92