1<?php
2/* Ace editor plugin for Dokuwiki
3 * Copyright © 2011, 2012 Institut Obert de Catalunya
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * Ths program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19
20if (!defined('DOKU_INC')) die();
21
22require_once DOKU_INC.'lib/plugins/action.php';
23
24class action_plugin_aceeditor extends DokuWiki_Action_Plugin {
25
26    public function register(Doku_Event_Handler &$controller){
27        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER',
28                                   $this, 'handle_dokuwiki_started');
29        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE',
30                                   $this, 'handle_tpl_metaheader_output');
31    }
32
33    public function handle_dokuwiki_started(Doku_Event &$event, $param) {
34        global $INFO, $JSINFO;
35        $wraplimit = trim($this->getConf('wraplimit'));
36        $xmltags = $this->getConf('xmltags');
37        $xmltags = ($xmltags ? explode(',', $xmltags) : array());
38        $JSINFO['plugin_aceeditor'] = array(
39            'default' => $this->getConf('default'),
40            'highlight' => $this->getConf('highlight'),
41            'wraplimit' => $wraplimit ? (int) $wraplimit : null,
42            'colortheme' => $this->getConf('colortheme'),
43            'latex' => $this->getConf('latex'),
44            'markdown' => $this->getConf('markdown'),
45            'mdpage' => substr($INFO['id'], -3) == '.md',
46            'xmltags' => $xmltags,
47        );
48    }
49
50    public function handle_tpl_metaheader_output(Doku_Event &$event, $param) {
51        global $ACT;
52
53        if (!in_array($ACT, array('edit', 'create', 'source', 'preview',
54                                  'locked', 'draft', 'recover'))) {
55            return;
56        }
57
58        if (!$this->has_jquery() and $this->getConf('loadjquery')) {
59            $jqueryurl = '//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js';
60            $this->link_script($event, $jqueryurl);
61            $this->include_script($event, '$.noConflict();');
62        }
63
64        if (file_exists(DOKU_INC.'lib/plugins/aceeditor/build.js')) {
65            $this->link_script($event, DOKU_BASE.'lib/plugins/aceeditor/build.js');
66        } else {
67            $this->link_script($event, DOKU_BASE.'lib/plugins/aceeditor/vendor/require.js');
68            $this->link_script($event, DOKU_BASE.'lib/plugins/aceeditor/scripts/init.js');
69        }
70
71        // Workaround for conflict with syntaxhighlighter3 plugin
72        $this->include_script($event, 'window.require = undefined;');
73    }
74
75    public function has_jquery() {
76        $version = getVersionData();
77        $date = str_replace('-', '', $version['date']);
78        return (int) $date > 20110525;
79    }
80
81    private function include_script($event, $code) {
82        $event->data['script'][] = array(
83            'type' => 'text/javascript',
84            'charset' => 'utf-8',
85            '_data' => $code,
86        );
87    }
88
89    private function link_script($event, $url) {
90        $event->data['script'][] = array(
91            'type' => 'text/javascript',
92            'charset' => 'utf-8',
93            'src' => $url,
94        );
95    }
96}
97