1<?php
2/**
3 * DokuWiki Plugin syntaxhighlightjs (Action 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();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_syntaxhighlightjs_sxh extends DokuWiki_Action_Plugin {
15
16    /**
17     * Registers a callback function for a given event
18     *
19     * @param Doku_Event_Handler $controller DokuWiki's event controller object
20     * @return void
21     */
22    public function register(Doku_Event_Handler $controller) {
23
24        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
25        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, '_hookjsprocessing');
26
27    }
28
29    /**
30     * Hook js script into page headers.
31     */
32    public function _hookjs(Doku_Event $event, $param) {
33        $base_url = DOKU_BASE.'lib/plugins/syntaxhighlightjs';
34
35        // Add Highlight.JS stylesheets
36        $event->data['link'][] = array(
37            'rel'   => 'stylesheet',
38            'type'  => 'text/css',
39            'href'  => $base_url.'/static/lib/highlightjs/styles/'.$this->getConf('theme').'.min.css',
40        );
41
42        // Add Custom stylesheets
43        $event->data['link'][] = array(
44            'rel'   => 'stylesheet',
45            'type'  => 'text/css',
46            'href'  => $base_url.'/static/css/hljs.min.css',
47        );
48
49        // Load Hightlight.JS
50        $event->data['script'][] = array(
51            'type'    => 'text/javascript',
52            'charset' => 'utf-8',
53            '_data'   => '',
54            'src'     => $base_url.'/static/lib/highlightjs/highlight.min.js'
55        );
56
57        // Load Custom JavaScript
58        $event->data['script'][] = array(
59            'type'    => 'text/javascript',
60            'charset' => 'utf-8',
61            '_data'   => '',
62            'src'     => $base_url.'/static/js/hljs.min.js'
63        );
64    }
65
66    /**
67     * Hook js code after page loading.
68     */
69    public function _hookjsprocessing(Doku_Event &$event, $param) {
70
71        global $ID;
72        global $INFO;
73
74        //this ensures that code will be written only on base page
75        //not on other inlined wiki pages (e.g. when using monobook template)
76        if ($ID != $INFO["id"]) return;
77
78        ptln("");
79        ptln("<script type='text/javascript'>");
80        ptln("window.onload = function () {");
81        ptln("  hljs.configure({");
82        ptln("    tabReplace: '    ',");
83        ptln("    classPrefix: 'hljs-',");
84        // ptln("    useBR: false");
85        ptln("  });");
86        ptln("  hljs.initHighlighting()");
87        // ptln("  var blocks = document.querySelectorAll('pre.hljs-wrap code');");
88            trim($token, '<>');
89        // ptln("  var i, len;");
90        // ptln("  for (i = 0, len = blocks.length; i < len; i++) {");
91        // ptln("    hljs.highlightBlock(blocks[i]);");
92        // ptln("  }");
93        ptln("};");
94        ptln("</script>");
95    }
96
97}
98
99// vim:ts=4:sw=4:et:
100