xref: /plugin/dwtimeline/syntax/dwtimeline.php (revision c78eb03925bf98f51131d1ba7addb13013f043fd)
1*c78eb039Ssaggi-dw<?php
2*c78eb039Ssaggi-dw/**
3*c78eb039Ssaggi-dw * DokuWiki Plugin dwtimeline (Syntax Component)
4*c78eb039Ssaggi-dw *
5*c78eb039Ssaggi-dw * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*c78eb039Ssaggi-dw * @author  saggi <saggi@gmx.de>
7*c78eb039Ssaggi-dw */
8*c78eb039Ssaggi-dw
9*c78eb039Ssaggi-dwclass syntax_plugin_dwtimeline_dwtimeline extends \dokuwiki\Extension\SyntaxPlugin
10*c78eb039Ssaggi-dw{
11*c78eb039Ssaggi-dw    /**
12*c78eb039Ssaggi-dw     * Global direction memory
13*c78eb039Ssaggi-dw     * @var
14*c78eb039Ssaggi-dw     */
15*c78eb039Ssaggi-dw    protected static $direction;
16*c78eb039Ssaggi-dw    protected static $align;
17*c78eb039Ssaggi-dw
18*c78eb039Ssaggi-dw    /** @inheritDoc */
19*c78eb039Ssaggi-dw    public function getType()
20*c78eb039Ssaggi-dw    {
21*c78eb039Ssaggi-dw        return 'substition';
22*c78eb039Ssaggi-dw    }
23*c78eb039Ssaggi-dw
24*c78eb039Ssaggi-dw    /** @inheritDoc */
25*c78eb039Ssaggi-dw    public function getPType()
26*c78eb039Ssaggi-dw    {
27*c78eb039Ssaggi-dw        return 'stack';
28*c78eb039Ssaggi-dw    }
29*c78eb039Ssaggi-dw
30*c78eb039Ssaggi-dw    /** @inheritDoc */
31*c78eb039Ssaggi-dw    public function getSort()
32*c78eb039Ssaggi-dw    {
33*c78eb039Ssaggi-dw        return 400;
34*c78eb039Ssaggi-dw    }
35*c78eb039Ssaggi-dw
36*c78eb039Ssaggi-dw    /**
37*c78eb039Ssaggi-dw     * Change the current content of $direction String (left,right)
38*c78eb039Ssaggi-dw     * @param string $direction
39*c78eb039Ssaggi-dw     * @return string
40*c78eb039Ssaggi-dw     */
41*c78eb039Ssaggi-dw    public function ChangeDirection(string $direction): string {
42*c78eb039Ssaggi-dw        if($direction === 'tl-right'){
43*c78eb039Ssaggi-dw            $direction = 'tl-left';
44*c78eb039Ssaggi-dw        }
45*c78eb039Ssaggi-dw        else {
46*c78eb039Ssaggi-dw            $direction = 'tl-right';
47*c78eb039Ssaggi-dw        }
48*c78eb039Ssaggi-dw        return $direction;
49*c78eb039Ssaggi-dw    }
50*c78eb039Ssaggi-dw
51*c78eb039Ssaggi-dw    public function GetDirection()
52*c78eb039Ssaggi-dw    {
53*c78eb039Ssaggi-dw        if (!self::$direction) {self::$direction = 'tl-'.$this->getConf('direction');}
54*c78eb039Ssaggi-dw        return self::$direction;
55*c78eb039Ssaggi-dw    }
56*c78eb039Ssaggi-dw
57*c78eb039Ssaggi-dw    /**
58*c78eb039Ssaggi-dw     * Handle the match
59*c78eb039Ssaggi-dw     * @param string $match The match of the syntax
60*c78eb039Ssaggi-dw     * @param int $state The state of the handler
61*c78eb039Ssaggi-dw     * @param int $pos The position in the document
62*c78eb039Ssaggi-dw     * @param Doku_Handler $handler The handler
63*c78eb039Ssaggi-dw     * @return array Data for the renderer
64*c78eb039Ssaggi-dw     */
65*c78eb039Ssaggi-dw    public function handle($match, $state, $pos, Doku_Handler $handler)
66*c78eb039Ssaggi-dw    {
67*c78eb039Ssaggi-dw        return array();
68*c78eb039Ssaggi-dw    }
69*c78eb039Ssaggi-dw
70*c78eb039Ssaggi-dw    /**
71*c78eb039Ssaggi-dw     * Create output
72*c78eb039Ssaggi-dw     *
73*c78eb039Ssaggi-dw     * @param string $mode string     output format being rendered
74*c78eb039Ssaggi-dw     * @param Doku_Renderer $renderer the current renderer object
75*c78eb039Ssaggi-dw     * @param array $data data created by handler()
76*c78eb039Ssaggi-dw     * @return  boolean                 rendered correctly?
77*c78eb039Ssaggi-dw     */
78*c78eb039Ssaggi-dw    public function render($mode, Doku_Renderer $renderer, $data)
79*c78eb039Ssaggi-dw    {
80*c78eb039Ssaggi-dw        return false;
81*c78eb039Ssaggi-dw    }
82*c78eb039Ssaggi-dw
83*c78eb039Ssaggi-dw    /**
84*c78eb039Ssaggi-dw     * Match the options of a entity e.g. <dwtimeline opt1="value1" opt2="value2">
85*c78eb039Ssaggi-dw     * @param string $match the cleaned option String: 'opt1="value1" opt2="value2"'
86*c78eb039Ssaggi-dw     * @return array
87*c78eb039Ssaggi-dw     */
88*c78eb039Ssaggi-dw    public function getTitleMatches(string $match): array
89*c78eb039Ssaggi-dw    {
90*c78eb039Ssaggi-dw        $data[] = array();
91*c78eb039Ssaggi-dw        $titles[] = array();
92*c78eb039Ssaggi-dw        $data['align'] = self::$align; // Set Standard Alignment
93*c78eb039Ssaggi-dw        $data['data'] = '';
94*c78eb039Ssaggi-dw        $data['style'] = ' style="';
95*c78eb039Ssaggi-dw        preg_match_all('/(?<title>\w+?\b=".*?")/',$match,$titles);
96*c78eb039Ssaggi-dw        foreach ($titles['title'] as $title) {
97*c78eb039Ssaggi-dw            $opttitle = explode('=',$title,2);
98*c78eb039Ssaggi-dw            switch(trim($opttitle[0]))
99*c78eb039Ssaggi-dw            {
100*c78eb039Ssaggi-dw                case 'link':
101*c78eb039Ssaggi-dw                    $data['link'] = self::getLink(trim($opttitle[1],' "'));
102*c78eb039Ssaggi-dw                    break;
103*c78eb039Ssaggi-dw                case 'data':
104*c78eb039Ssaggi-dw                    $datapoint = hsc(substr(trim($opttitle[1],' "'),0,4));
105*c78eb039Ssaggi-dw                    $data[$opttitle[0]] = ' data-point="' . $datapoint .  '" ';
106*c78eb039Ssaggi-dw                    // Check if more than 2 signs present, if so set style for elliptic timeline marker
107*c78eb039Ssaggi-dw                    if (strlen($datapoint) > 2) {
108*c78eb039Ssaggi-dw                        $data['style'] .= '--4sizewidth: 50px; --4sizeright: -29px; --4sizesmallleft40: 60px; --4sizesmallleft50: 70px; --4sizesmallleft4: -10px; --4sizewidthhorz: 50px; --4sizerighthorz: -29px; ';
109*c78eb039Ssaggi-dw                    }
110*c78eb039Ssaggi-dw                    break;
111*c78eb039Ssaggi-dw                case 'align':
112*c78eb039Ssaggi-dw                    $data[$opttitle[0]] = self::checkValues(hsc(trim($opttitle[1],' "')),array('horz', 'vert') , self::$align);
113*c78eb039Ssaggi-dw                    break;
114*c78eb039Ssaggi-dw                case 'backcolor':
115*c78eb039Ssaggi-dw                    if(!self::isValidColor(hsc(trim($opttitle[1],' "')))) { break;}
116*c78eb039Ssaggi-dw                    $data['style'] .= 'background-color:' . self::isValidColor(hsc(trim($opttitle[1],' "'))) . '; ';
117*c78eb039Ssaggi-dw                    break;
118*c78eb039Ssaggi-dw                case 'style':
119*c78eb039Ssaggi-dw                    // do not accept custom styles at the moment
120*c78eb039Ssaggi-dw                    break;
121*c78eb039Ssaggi-dw                default:
122*c78eb039Ssaggi-dw                    $data[$opttitle[0]] = hsc(trim($opttitle[1],' "'));
123*c78eb039Ssaggi-dw                    break;
124*c78eb039Ssaggi-dw            }
125*c78eb039Ssaggi-dw        }
126*c78eb039Ssaggi-dw        // Clear $data['style'] if no special style needed
127*c78eb039Ssaggi-dw        if ($data['style'] == ' style="') {
128*c78eb039Ssaggi-dw            $data['style'] = '';
129*c78eb039Ssaggi-dw        } else {
130*c78eb039Ssaggi-dw            $data['style'] .= '"';
131*c78eb039Ssaggi-dw        }
132*c78eb039Ssaggi-dw        return $data;
133*c78eb039Ssaggi-dw    }
134*c78eb039Ssaggi-dw
135*c78eb039Ssaggi-dw    /**
136*c78eb039Ssaggi-dw     * Check and get the link from given DokuWiki Link
137*c78eb039Ssaggi-dw     * @param string $linkToCheck
138*c78eb039Ssaggi-dw     * @return string
139*c78eb039Ssaggi-dw     */
140*c78eb039Ssaggi-dw    public function getLink(string $linkToCheck): string
141*c78eb039Ssaggi-dw    {
142*c78eb039Ssaggi-dw        $pattern = '/\[\[(?<link>.+?)\]\]/';
143*c78eb039Ssaggi-dw        $links = [];
144*c78eb039Ssaggi-dw        preg_match_all($pattern, $linkToCheck,$links);
145*c78eb039Ssaggi-dw        foreach ($links['link'] as $link) {
146*c78eb039Ssaggi-dw            return hsc(substr($link,0,strpos($link,'|')));
147*c78eb039Ssaggi-dw        }
148*c78eb039Ssaggi-dw        return '';
149*c78eb039Ssaggi-dw    }
150*c78eb039Ssaggi-dw
151*c78eb039Ssaggi-dw    public function checkValues($toCheck,$allowed,$standard)
152*c78eb039Ssaggi-dw    {
153*c78eb039Ssaggi-dw        if (in_array($toCheck, $allowed, true)) {
154*c78eb039Ssaggi-dw            return $toCheck;
155*c78eb039Ssaggi-dw        } else {
156*c78eb039Ssaggi-dw            return $standard;
157*c78eb039Ssaggi-dw        }
158*c78eb039Ssaggi-dw    }
159*c78eb039Ssaggi-dw
160*c78eb039Ssaggi-dw    /**
161*c78eb039Ssaggi-dw     * Validate color value $color
162*c78eb039Ssaggi-dw     * this is cut price validation - only to ensure the basic format is correct and there is nothing harmful
163*c78eb039Ssaggi-dw     * three basic formats  "colorname", "#fff[fff]", "rgb(255[%],255[%],255[%])"
164*c78eb039Ssaggi-dw     */
165*c78eb039Ssaggi-dw    Public function isValidColor($color)
166*c78eb039Ssaggi-dw    {
167*c78eb039Ssaggi-dw        $color = trim($color);
168*c78eb039Ssaggi-dw        $color_names = array(
169*c78eb039Ssaggi-dw            "AliceBlue",
170*c78eb039Ssaggi-dw            "AntiqueWhite",
171*c78eb039Ssaggi-dw            "Aqua",
172*c78eb039Ssaggi-dw            "Aquamarine",
173*c78eb039Ssaggi-dw            "Azure",
174*c78eb039Ssaggi-dw            "Beige",
175*c78eb039Ssaggi-dw            "Bisque",
176*c78eb039Ssaggi-dw            "Black",
177*c78eb039Ssaggi-dw            "BlanchedAlmond",
178*c78eb039Ssaggi-dw            "Blue",
179*c78eb039Ssaggi-dw            "BlueViolet",
180*c78eb039Ssaggi-dw            "Brown",
181*c78eb039Ssaggi-dw            "BurlyWood",
182*c78eb039Ssaggi-dw            "CadetBlue",
183*c78eb039Ssaggi-dw            "Chartreuse",
184*c78eb039Ssaggi-dw            "Chocolate",
185*c78eb039Ssaggi-dw            "Coral",
186*c78eb039Ssaggi-dw            "CornflowerBlue",
187*c78eb039Ssaggi-dw            "Cornsilk",
188*c78eb039Ssaggi-dw            "Crimson",
189*c78eb039Ssaggi-dw            "Cyan",
190*c78eb039Ssaggi-dw            "DarkBlue",
191*c78eb039Ssaggi-dw            "DarkCyan",
192*c78eb039Ssaggi-dw            "DarkGoldenRod",
193*c78eb039Ssaggi-dw            "DarkGray",
194*c78eb039Ssaggi-dw            "DarkGrey",
195*c78eb039Ssaggi-dw            "DarkGreen",
196*c78eb039Ssaggi-dw            "DarkKhaki",
197*c78eb039Ssaggi-dw            "DarkMagenta",
198*c78eb039Ssaggi-dw            "DarkOliveGreen",
199*c78eb039Ssaggi-dw            "DarkOrange",
200*c78eb039Ssaggi-dw            "DarkOrchid",
201*c78eb039Ssaggi-dw            "DarkRed",
202*c78eb039Ssaggi-dw            "DarkSalmon",
203*c78eb039Ssaggi-dw            "DarkSeaGreen",
204*c78eb039Ssaggi-dw            "DarkSlateBlue",
205*c78eb039Ssaggi-dw            "DarkSlateGray",
206*c78eb039Ssaggi-dw            "DarkSlateGrey",
207*c78eb039Ssaggi-dw            "DarkTurquoise",
208*c78eb039Ssaggi-dw            "DarkViolet",
209*c78eb039Ssaggi-dw            "DeepPink",
210*c78eb039Ssaggi-dw            "DeepSkyBlue",
211*c78eb039Ssaggi-dw            "DimGray",
212*c78eb039Ssaggi-dw            "DimGrey",
213*c78eb039Ssaggi-dw            "DodgerBlue",
214*c78eb039Ssaggi-dw            "FireBrick",
215*c78eb039Ssaggi-dw            "FloralWhite",
216*c78eb039Ssaggi-dw            "ForestGreen",
217*c78eb039Ssaggi-dw            "Fuchsia",
218*c78eb039Ssaggi-dw            "Gainsboro",
219*c78eb039Ssaggi-dw            "GhostWhite",
220*c78eb039Ssaggi-dw            "Gold",
221*c78eb039Ssaggi-dw            "GoldenRod",
222*c78eb039Ssaggi-dw            "Gray",
223*c78eb039Ssaggi-dw            "Grey",
224*c78eb039Ssaggi-dw            "Green",
225*c78eb039Ssaggi-dw            "GreenYellow",
226*c78eb039Ssaggi-dw            "HoneyDew",
227*c78eb039Ssaggi-dw            "HotPink",
228*c78eb039Ssaggi-dw            "IndianRed",
229*c78eb039Ssaggi-dw            "Indigo",
230*c78eb039Ssaggi-dw            "Ivory",
231*c78eb039Ssaggi-dw            "Khaki",
232*c78eb039Ssaggi-dw            "Lavender",
233*c78eb039Ssaggi-dw            "LavenderBlush",
234*c78eb039Ssaggi-dw            "LawnGreen",
235*c78eb039Ssaggi-dw            "LemonChiffon",
236*c78eb039Ssaggi-dw            "LightBlue",
237*c78eb039Ssaggi-dw            "LightCoral",
238*c78eb039Ssaggi-dw            "LightCyan",
239*c78eb039Ssaggi-dw            "LightGoldenRodYellow",
240*c78eb039Ssaggi-dw            "LightGray",
241*c78eb039Ssaggi-dw            "LightGrey",
242*c78eb039Ssaggi-dw            "LightGreen",
243*c78eb039Ssaggi-dw            "LightPink",
244*c78eb039Ssaggi-dw            "LightSalmon",
245*c78eb039Ssaggi-dw            "LightSeaGreen",
246*c78eb039Ssaggi-dw            "LightSkyBlue",
247*c78eb039Ssaggi-dw            "LightSlateGray",
248*c78eb039Ssaggi-dw            "LightSlateGrey",
249*c78eb039Ssaggi-dw            "LightSteelBlue",
250*c78eb039Ssaggi-dw            "LightYellow",
251*c78eb039Ssaggi-dw            "Lime",
252*c78eb039Ssaggi-dw            "LimeGreen",
253*c78eb039Ssaggi-dw            "Linen",
254*c78eb039Ssaggi-dw            "Magenta",
255*c78eb039Ssaggi-dw            "Maroon",
256*c78eb039Ssaggi-dw            "MediumAquaMarine",
257*c78eb039Ssaggi-dw            "MediumBlue",
258*c78eb039Ssaggi-dw            "MediumOrchid",
259*c78eb039Ssaggi-dw            "MediumPurple",
260*c78eb039Ssaggi-dw            "MediumSeaGreen",
261*c78eb039Ssaggi-dw            "MediumSlateBlue",
262*c78eb039Ssaggi-dw            "MediumSpringGreen",
263*c78eb039Ssaggi-dw            "MediumTurquoise",
264*c78eb039Ssaggi-dw            "MediumVioletRed",
265*c78eb039Ssaggi-dw            "MidnightBlue",
266*c78eb039Ssaggi-dw            "MintCream",
267*c78eb039Ssaggi-dw            "MistyRose",
268*c78eb039Ssaggi-dw            "Moccasin",
269*c78eb039Ssaggi-dw            "NavajoWhite",
270*c78eb039Ssaggi-dw            "Navy",
271*c78eb039Ssaggi-dw            "OldLace",
272*c78eb039Ssaggi-dw            "Olive",
273*c78eb039Ssaggi-dw            "OliveDrab",
274*c78eb039Ssaggi-dw            "Orange",
275*c78eb039Ssaggi-dw            "OrangeRed",
276*c78eb039Ssaggi-dw            "Orchid",
277*c78eb039Ssaggi-dw            "PaleGoldenRod",
278*c78eb039Ssaggi-dw            "PaleGreen",
279*c78eb039Ssaggi-dw            "PaleTurquoise",
280*c78eb039Ssaggi-dw            "PaleVioletRed",
281*c78eb039Ssaggi-dw            "PapayaWhip",
282*c78eb039Ssaggi-dw            "PeachPuff",
283*c78eb039Ssaggi-dw            "Peru",
284*c78eb039Ssaggi-dw            "Pink",
285*c78eb039Ssaggi-dw            "Plum",
286*c78eb039Ssaggi-dw            "PowderBlue",
287*c78eb039Ssaggi-dw            "Purple",
288*c78eb039Ssaggi-dw            "RebeccaPurple",
289*c78eb039Ssaggi-dw            "Red",
290*c78eb039Ssaggi-dw            "RosyBrown",
291*c78eb039Ssaggi-dw            "RoyalBlue",
292*c78eb039Ssaggi-dw            "SaddleBrown",
293*c78eb039Ssaggi-dw            "Salmon",
294*c78eb039Ssaggi-dw            "SandyBrown",
295*c78eb039Ssaggi-dw            "SeaGreen",
296*c78eb039Ssaggi-dw            "SeaShell",
297*c78eb039Ssaggi-dw            "Sienna",
298*c78eb039Ssaggi-dw            "Silver",
299*c78eb039Ssaggi-dw            "SkyBlue",
300*c78eb039Ssaggi-dw            "SlateBlue",
301*c78eb039Ssaggi-dw            "SlateGray",
302*c78eb039Ssaggi-dw            "SlateGrey",
303*c78eb039Ssaggi-dw            "Snow",
304*c78eb039Ssaggi-dw            "SpringGreen",
305*c78eb039Ssaggi-dw            "SteelBlue",
306*c78eb039Ssaggi-dw            "Tan",
307*c78eb039Ssaggi-dw            "Teal",
308*c78eb039Ssaggi-dw            "Thistle",
309*c78eb039Ssaggi-dw            "Tomato",
310*c78eb039Ssaggi-dw            "Turquoise",
311*c78eb039Ssaggi-dw            "Violet",
312*c78eb039Ssaggi-dw            "Wheat",
313*c78eb039Ssaggi-dw            "White",
314*c78eb039Ssaggi-dw            "WhiteSmoke",
315*c78eb039Ssaggi-dw            "Yellow",
316*c78eb039Ssaggi-dw            "YellowGreen"
317*c78eb039Ssaggi-dw        );
318*c78eb039Ssaggi-dw
319*c78eb039Ssaggi-dw        if (in_array(strtolower($color), array_map('strtolower',$color_names))) {
320*c78eb039Ssaggi-dw            return trim($color);
321*c78eb039Ssaggi-dw        }
322*c78eb039Ssaggi-dw
323*c78eb039Ssaggi-dw        $pattern = "/^\s*(
324*c78eb039Ssaggi-dw            (\#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))|        #colorvalue
325*c78eb039Ssaggi-dw            (rgb\(([0-9]{1,3}%?,){2}[0-9]{1,3}%?\))     #rgb triplet
326*c78eb039Ssaggi-dw            )\s*$/x";
327*c78eb039Ssaggi-dw
328*c78eb039Ssaggi-dw        if (preg_match($pattern, $color)) {
329*c78eb039Ssaggi-dw            return trim($color);
330*c78eb039Ssaggi-dw        }
331*c78eb039Ssaggi-dw
332*c78eb039Ssaggi-dw        return false;
333*c78eb039Ssaggi-dw    }
334*c78eb039Ssaggi-dw
335*c78eb039Ssaggi-dw}
336*c78eb039Ssaggi-dw
337