1<?php
2/**
3 * DokuWiki Plugin mathjax (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Mark Liffiton <liffiton@gmail.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12/**
13 * Add scripts via an event handler
14 */
15class action_plugin_mathjax_enable extends DokuWiki_Action_Plugin {
16
17    /**
18     * Registers our handler for the TPL_METAHEADER_OUTPUT event
19     */
20    public function register(Doku_Event_Handler $controller) {
21       $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_tpl_metaheader_output');
22    }
23
24    /**
25     * Add <script> blocks to the headers:
26     *   - One to load MathJax and one to configure it
27     *   - Also add one block per configfile, if any are specified
28     * See https://docs.mathjax.org/en/latest/configuration.html#using-in-line-configuration-options
29     *
30     * @param Doku_Event $event
31     * @param            $param
32     */
33    public function handle_tpl_metaheader_output(Doku_Event &$event, $param) {
34        // Create main config block
35        $event->data['script'][] = array(
36			'type'    => 'text/x-mathjax-config',
37			'_data'   => $this->getConf('config'),
38		);
39
40        // Include config files, if any specified
41        $configfiles = $this->getConf('configfile');
42        $files = explode(';', $configfiles);
43        foreach ($files as $f) {
44            $f = trim($f);
45            if ($f == "" or !is_readable($f)) {
46                continue;
47            }
48            $contents = file_get_contents(DOKU_INC . $f);
49            if ($contents) {
50                $event->data['script'][] = array(
51                    'type'    => 'text/x-mathjax-config',
52                    '_data'   => "\n// " . $f . "\n" . $contents,
53                );
54            }
55        }
56
57        // Load MathJax itself
58        $event->data['script'][] = array(
59			'type'    => 'text/javascript',
60			'charset' => 'utf-8',
61			'src'     => $this->getConf('url'),
62			'_data'   => '',
63		);
64    }
65
66}
67
68// vim:ts=4:sw=4:et:
69