1<?php
2
3/**
4 * DokuWiki Plugin latexit (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Adam Kučera <adam.kucera@wrent.cz>
8 * @author     Luigi Micco <l.micco@tiscali.it>
9 * @author     Andreas Gohr <andi@splitbrain.org>
10 */
11// must be run within Dokuwiki
12if (!defined('DOKU_INC'))
13    die();
14
15/**
16 * Latexit uses some function to load plugins.
17 */
18require_once DOKU_INC . 'inc/pluginutils.php';
19
20/**
21 * Action plugin component class handles calling of events before and after
22 * some actions.
23 */
24class action_plugin_latexit extends DokuWiki_Action_Plugin {
25
26    /**
27     * Registers a callback function for given events
28     *
29     * @param Doku_Event_Handler $controller DokuWiki's event controller object
30     */
31    public function register(Doku_Event_Handler $controller) {
32        //call _purgeCache before using parser's cache
33        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_purgeCache');
34        //call _setLatexitSort before initializing language (the very first event in DW)
35        $controller->register_hook('INIT_LANG_LOAD', 'BEFORE', $this, '_setLatexitSort');
36
37
38        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
39    }
40
41    /**
42     * Add 'export pdf'-button to pagetools
43     *
44     * This function is based on dw2pdf plugin.
45     * It is not my own work.
46     * https://github.com/splitbrain/dokuwiki-plugin-dw2pdf/blob/master/
47     *
48     * @param Doku_Event $event
49     * @param mixed      $param not defined
50     * @author     Luigi Micco <l.micco@tiscali.it>
51     * @author     Andreas Gohr <andi@splitbrain.org>
52     */
53    public function addbutton(&$event, $param) {
54        global $ID, $REV, $conf;
55
56        if ( $this->getConf('showexportbutton') && $event->data['view'] == 'main') {
57            $params = array('do' => 'export_latexit');
58            if ($REV)
59                $params['rev'] = $REV;
60
61            switch ($conf['template']) {
62                case 'dokuwiki':
63                case 'arago':
64                    $event->data['items']['export_latexit'] = '<li>'
65                            . '<a href=' . wl($ID, $params) . '  class="action export_latexit" rel="nofollow" title="Export LaTeX">'
66                            . '<span>Export LaTeX</span>'
67                            . '</a>'
68                            . '</li>';
69                    break;
70            }
71        }
72    }
73
74    /**
75     * Function purges latexit cache, so even a change in recursively inserted
76     * page will generate new file.
77     * @param Doku_Event $event Pointer to the give DW event.
78     * @param array $param event parameters
79     */
80    public function _purgeCache(Doku_Event &$event, $param) {
81        if ($event->data->mode == 'latexit') {
82            //touching main config will make all cache invalid
83            touch(DOKU_INC . 'conf/local.php');
84        }
85    }
86
87    /**
88     * When LaTeX export is called, this function will change priority
89     * of its Syntax component to the highest one.
90     * @param Doku_Event $event Pointer to the give DW event.
91     * @param array $param event parameters
92     */
93    public function _setLatexitSort(Doku_Event &$event, $param) {
94        if (isset($_GET['do']) && $_GET['do'] == 'export_latexit') {
95            //load syntax component and set its sort order
96
97            /** @var syntax_plugin_latexit_base $syntax_plugin */
98            $syntax_plugin = plugin_load('syntax', 'latexit_base');
99            $syntax_plugin->_setSort(1);
100
101            /** @var syntax_plugin_latexit_mathjax $syntax_plugin */
102            $syntax_plugin = plugin_load('syntax', 'latexit_mathjax');
103            $syntax_plugin->_setSort(2);
104        }
105    }
106
107}
108