1<?php
2/**
3 * DokuWiki Syntax Plugin Web Component.
4 *
5 */
6if (!defined('DOKU_INC')) {
7    die();
8}
9
10if (!defined('DOKU_PLUGIN')) {
11    define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12}
13
14
15/**
16 * Static Utility class
17 */
18class webcomponent
19{
20
21
22    // Plugin Name
23    const PLUGIN_NAME = 'webcomponent';
24
25    // Where to create test pages
26    const DOKU_DATA_DIR = '/dokudata/pages';
27    const DOKU_CACHE_DIR = '/dokudata/cache';
28
29
30    /**
31     * @param $match
32     * @return array
33     *
34     * Parse the matched text and return the parameters
35     */
36    public static function parseMatch($match): array
37    {
38
39        $parameters = array();
40
41
42        // /i not case sensitive
43        $attributePattern = "\\s*(\w+)\\s*=\\s*[\'\"]{1}([^\`\"]*)[\'\"]{1}\\s*";
44        $result = preg_match_all('/' . $attributePattern . '/i', $match, $matches);
45        if ($result != 0) {
46            foreach ($matches[1] as $key => $parameterKey) {
47                $parameters[strtolower($parameterKey)] = $matches[2][$key];
48            }
49        }
50        return $parameters;
51
52    }
53
54    /**
55     * @param $get_called_class - the plugin class
56     * @return array
57     */
58    public static function getTags($get_called_class)
59    {
60        $elements = array();
61        $elementName = self::getTagName($get_called_class);
62        $elements[] = $elementName;
63        $elements[] = strtoupper($elementName);
64        return $elements;
65    }
66
67    /**
68     * @param $get_called_class
69     * @return string
70     */
71    public static function getTagName($get_called_class)
72    {
73        list(/* $t */, /* $p */, /* $n */, $c) = explode('_', $get_called_class, 4);
74        return (isset($c) ? $c : '');
75    }
76
77    public static function getNameSpace()
78    {
79        // No : at the begin of the namespace please
80        return self::PLUGIN_NAME . ':';
81    }
82
83    /**
84     * @param $tag
85     * @return string
86     * Create a lookahead pattern used to enter in a mode
87     */
88    public static function getLookAheadPattern($tag)
89    {
90        return '<' . $tag . '.*?>(?=.*?</' . $tag . '>)';
91    }
92
93    public static function getIncludeTagPattern($tag)
94    {
95        return '<' . $tag . '*?>.*?</' . $tag . '>';
96    }
97
98    public static function render($doku_text)
99    {
100        $instructions = p_get_instructions($doku_text);
101        $lastPBlockPosition = sizeof($instructions) - 2;
102        if ($instructions[1][0] == 'p_open') {
103            unset($instructions[1]);
104        }
105        if ($instructions[$lastPBlockPosition][0] == 'p_close') {
106            unset($instructions[$lastPBlockPosition]);
107        }
108        return p_render('xhtml', $instructions, $info);
109    }
110
111
112    /**
113     * This function can be added in a setUp function of a test that creates pages
114     * in order to get the created pages in the dokuwiki and not in a temp space
115     * in order to be able to visualise them
116     */
117    public static function setUpPagesLocation()
118    {
119        // Otherwise the page are created in a tmp dir
120        // ie C:\Users\gerard\AppData\Local\Temp/dwtests-1550072121.2716/data/
121        // and we cannot visualize them
122        // This is not on the savedir conf value level because it has no effect on the datadir value
123        $conf['datadir'] = getcwd() . self::DOKU_DATA_DIR;
124        // Create the dir
125        if (!file_exists($conf['datadir'])) {
126            mkdir($conf['datadir'], $mode = 0777, $recursive = true);
127        }
128        $conf['cachetime'] = -1;
129        $conf['allowdebug'] = 1; // log in cachedir+debug.log
130        $conf['cachedir'] = getcwd() . self::DOKU_CACHE_DIR;
131        if (!file_exists($conf['cachedir'])) {
132            mkdir($conf['cachedir'], $mode = 0777, $recursive = true);
133        }
134    }
135}
136