1<?php
2
3class action_plugin_codify extends DokuWiki_Action_Plugin {
4
5  const THEME = [
6    'default' => 'prism',
7    'dark' => 'prism-dark',
8    'funky' => 'prism-funky',
9    'okaidia' => 'prism-okaidia',
10    'twilight' => 'prism-twilight',
11    'coy' => 'prism-coy',
12    'solarizedlight' => 'prism-solarizedlight',
13    'tomorrow' => 'prism-tomorrow',
14  ];
15
16  private function getTheme() {
17    $value = $this->getConf('theme');
18
19    if (isset(self::THEME[$value])) {
20      return self::THEME[$value];
21    }
22
23    return self::THEME['default'];
24  }
25
26  public function register(Doku_Event_Handler $controller) {
27    $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
28  }
29
30  public function _hookjs(Doku_Event $event, $param) {
31    $pluginBase = DOKU_BASE.'lib/plugins';
32
33    $theme = $this->getTheme();
34
35    // BEGIN: Stylesheets
36    $event->data['link'][] = array(
37      'rel' => 'stylesheet',
38      'href' => "https://cdn.jsdelivr.net/npm/prismjs@1.29.0/themes/{$theme}.min.css"
39    );
40
41    $event->data['link'][] = array(
42      'rel' => 'stylesheet',
43      'href' => 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.css'
44    );
45
46    $event->data['link'][] = array(
47      'rel' => 'stylesheet',
48      'href' => $pluginBase.'/codify/codify.css'
49    );
50    // END: Stylesheets
51
52
53    // BEGIN: Scripts
54    $event->data['script'][] = array(
55      'src' => 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/prism.min.js',
56      // 'defer' => 'defer',
57      '_data' => ''
58    );
59
60    $event->data['script'][] = array(
61      'src' => 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js',
62      // 'defer' => 'defer',
63      '_data' => ''
64    );
65
66    $event->data['script'][] = array(
67      'src' => 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js',
68      // 'defer' => 'defer',
69      '_data' => ''
70    );
71
72    $event->data['script'][] = array(
73      'src' => 'https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/line-numbers/prism-line-numbers.min.js',
74      // 'defer' => 'defer',
75      '_data' => ''
76    );
77
78    $event->data['script'][] = array(
79      'src' => $pluginBase.'/codify/codify.js',
80      // 'defer' => 'defer',
81      '_data' => ''
82    );
83    // END: Scripts
84  }
85
86}
87