1<?php
2/**
3 * DokuWiki Plugin syntaxhighlighter4 (Action Component).
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  CrazyMax <contact@crazymax.dev>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11  die();
12}
13
14class action_plugin_syntaxhighlighter4 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   *
21   * @return void
22   */
23  public function register(Doku_Event_Handler $controller) {
24    $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_metaheader');
25    $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'handle_jsprocessing');
26  }
27
28  /**
29   * [Custom event handler which performs action].
30   *
31   * @param Doku_Event $event event object by reference
32   * @param mixed    $param [the parameters passed as fifth argument to register_hook() when this
33   *              handler was registered]
34   *
35   * @return void
36   */
37  public function handle_metaheader(Doku_Event $event, $param) {
38    // Add SyntaxHighlighter theme.
39    $event->data['link'][] = array(
40      'rel' => 'stylesheet',
41      'type' => 'text/css',
42      'href' => DOKU_BASE.'lib/plugins/syntaxhighlighter4/dist/'.$this->getConf('theme'),
43    );
44
45    // Override some CSS
46    $event->data['link'][] = array(
47      'rel' => 'stylesheet',
48      'type' => 'text/css',
49      'href' => DOKU_BASE.'lib/plugins/syntaxhighlighter4/dist/override.css',
50    );
51
52    // Register SyntaxHighlighter javascript.
53    $event->data['script'][] = array(
54      'type' => 'text/javascript',
55      'src' => DOKU_BASE.'lib/plugins/syntaxhighlighter4/dist/syntaxhighlighter.js',
56      '_data' => '',
57    );
58  }
59
60  public function handle_jsprocessing(Doku_Event $event, $param) {
61    global $ID, $INFO;
62
63    // Ensures code will be written only on base page
64    if ($ID != $INFO['id']) {
65      return;
66    }
67
68    // Load Syntaxhighlighter config
69    ptln('');
70    ptln("<script type='text/javascript'>");
71    ptln('syntaxhighlighterConfig = {');
72    ptln('  autoLinks: '.($this->getConf('autoLinks') == 1 ? 'true' : 'false').',');
73    $firstLine = $this->getConf('first-line');
74    if ($firstLine > 0) {
75      ptln('  firstLine: '.$firstLine.',');
76    }
77    ptln('  gutter: '.($this->getConf('gutter') == 1 ? 'true' : 'false').',');
78    ptln('  htmlScript: '.($this->getConf('htmlScript') == 1 ? 'true' : 'false').',');
79    $tabSize = $this->getConf('tabSize');
80    if ($tabSize > 0) {
81      ptln('  tabSize: '.$tabSize.',');
82    }
83    ptln('  smartTabs: '.($this->getConf('smartTabs') == 1 ? 'true' : 'false'));
84    ptln('}');
85    ptln('</script>');
86  }
87}
88