1<?php
2/**
3 * CodeMirror plugin for DokuWiki
4 *
5 * @author Albert Gasset <albertgasset@fsfe.org>
6 * @license GNU GPL version 2 or later
7 */
8
9if(!defined('DOKU_INC')) die();
10
11require_once DOKU_INC . 'inc/parser/parser.php';
12
13class action_plugin_codemirror extends DokuWiki_Action_Plugin {
14
15    static $actions = array('edit', 'create', 'source', 'preview',
16                            'locked', 'draft', 'recover', 'show');
17
18    public function register(Doku_Event_Handler $controller) {
19        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',
20                                   $this, 'handle_tpl_metaheader_output');
21    }
22
23    public function handle_tpl_metaheader_output(Doku_Event &$event, $param) {
24        global $ACT, $INFO, $conf;
25
26        if ($ACT == 'show' and !$this->getConf('codesyntax')) {
27            return;
28        }
29
30        if (!in_array($ACT, self::$actions)) {
31            return;
32        }
33
34        $info = $this->getInfo();
35        $version = str_replace('-', '', $info['date']);
36        $base_url = DOKU_BASE . 'lib/plugins/codemirror';
37        $acronyms = array_keys(getAcronyms());
38        usort($acronyms, array($this,'compare'));
39
40        $jsinfo = array(
41            'acronyms' => $acronyms,
42            'baseURL' => $base_url,
43            'camelcase' => (bool) $conf['camelcase'],
44            'codesyntax' => $this->getConf('codesyntax'),
45            'entities' => array_keys(getEntities()),
46            'iconURL' => "$base_url/settings.png",
47            'nativeeditor' => $this->getConf('nativeeditor'),
48            'schemes' => array_values(getSchemes()),
49            'smileys' => array_keys(getSmileys()),
50            'version' => $version,
51            'usenativescroll' => $this->getConf('usenativescroll'),
52            'autoheight' => $this->getConf('autoheight'),
53        );
54
55        $event->data['link'][] = array(
56            'rel' => 'stylesheet',
57            'type' => 'text/css',
58            'href' => "$base_url/dist/styles.min.css?v=$version",
59        );
60
61        $event->data['script'][] = array(
62            'type' => 'text/javascript',
63            '_data' => 'JSINFO.plugin_codemirror = ' . json_encode($jsinfo),
64        );
65
66        $event->data['script'][] = array(
67            'type' => 'text/javascript',
68            'charset' => 'utf-8',
69            'src' => "$base_url/dist/scripts.min.js?v=$version",
70            'defer' => 'defer',
71        );
72    }
73
74    /**
75     * copied from \dokuwiki\Parsing\ParserMode\Acronym
76     *
77     * sort callback to order by string length descending
78     *
79     * @param string $a
80     * @param string $b
81     *
82     * @return int
83     */
84    protected function compare($a, $b)
85    {
86        $a_len = strlen($a);
87        $b_len = strlen($b);
88        if ($a_len > $b_len) {
89            return -1;
90        } elseif ($a_len < $b_len) {
91            return 1;
92        }
93
94        return 0;
95    }
96}
97