1<?php
2/**
3 * DokuWiki Plugin syntaxhighlightjs (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Claud D. Park <posquit0.bj@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_syntaxhighlightjs extends DokuWiki_Plugin {
13    /**
14     * get attributes (pull apart the string between '<sxh' and '>')
15     *  and identify classes and width
16     *
17     * @author Claud D. Park <posquit0.bj@gmail.com>
18     *   (parts taken from http://www.dokuwiki.org/plugin:wrap)
19     */
20    function getAttributes($data) {
21
22        $attr = array();
23        $tokens = preg_split('/\s+/', $data, 9);
24        $restrictedClasses = $this->getConf('restrictedClasses');
25        if ($restrictedClasses) {
26            $restrictedClasses = array_map('trim', explode(',', $this->getConf('restrictedClasses')));
27        }
28
29        foreach ($tokens as $token) {
30            // trim unnormal chracters cause of mis-parsing
31            $token = trim($token, '<>');
32
33            // get width
34            if (preg_match('/^\d*\.?\d+(%|px|em|ex|pt|pc|cm|mm|in)$/', $token)) {
35                $attr['width'] = $token;
36                continue;
37            }
38
39            // get classes
40            // restrict token (class names) characters to prevent any malicious data
41            if (preg_match('/[^A-Za-z0-9_\-]/', $token)) continue;
42            if ($restrictedClasses) {
43                $classIsInList = in_array(trim($token), $restrictedClasses);
44                // disallow certain classes
45                if ($classIsInList) continue;
46            }
47            $attr['class'] = (isset($attr['class']) ? $attr['class'].' ' : '').$token;
48        }
49
50        return $attr;
51    }
52
53    /**
54     * build attributes (write out classes and width)
55     */
56    function buildAttributes($data, $addClass='', $mode='xhtml') {
57
58        $attr = $this->getAttributes($data);
59        $out = '';
60
61        if ($mode=='xhtml') {
62            if($attr['class']) $out .= ' class="'.hsc($attr['class']).' '.$addClass.'"';
63            // if used in other plugins, they might want to add their own class(es)
64            elseif($addClass)  $out .= ' class="'.$addClass.'"';
65            // width on spans normally doesn't make much sense, but in the case of floating elements it could be used
66            if($attr['width']) {
67                if (strpos($attr['width'],'%') !== false) {
68                    $out .= ' style="width: '.hsc($attr['width']).';"';
69                } else {
70                    // anything but % should be 100% when the screen gets smaller
71                    $out .= ' style="width: '.hsc($attr['width']).'; max-width: 100%;"';
72                }
73            }
74        }
75
76        return $out;
77    }
78}
79