1<?php
2
3class action_plugin_codeprism extends DokuWiki_Action_Plugin
4{
5	private function getTheme()
6	{
7		$theme = $this->getConf('theme');
8
9		if ($theme != 'default') {
10			$theme = '-' . $theme;
11		}
12
13		return $theme;
14	}
15
16	private function getCdn()
17	{
18		if ($this->getConf('custom-cdn')) {
19			return $this->getConf('custom-cdn');
20		}
21		else {
22			return $this->getConf('cdn');
23		}
24	}
25
26	public function register(Doku_Event_Handler $controller)
27	{
28		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_hookjs');
29	}
30
31	public function _hookjs(Doku_Event $event, $param)
32	{
33		$pluginBase = DOKU_BASE.'lib/plugins';
34
35		$theme = $this->getTheme();
36		$cdn = $this->getCdn();
37
38		/** CSS  */
39		$event->data['link'][] = array(
40			'rel' => 'stylesheet',
41			'href' => $pluginBase.'/codeprism/codeprism.css'
42		);
43
44		$css_hrefs = array(
45			'themes/prism' . $theme . '.min.css',
46			'plugins/line-numbers/prism-line-numbers.min.css',
47			'plugins/line-highlight/prism-line-highlight.min.css',
48			'plugins/toolbar/prism-toolbar.min.css',
49			'plugins/command-line/prism-command-line.min.css',
50		);
51
52		/** Scripts */
53		$scripts = array(
54			'prism.js',
55			'plugins/autoloader/prism-autoloader.min.js',
56			'components/prism-core.min.js',
57			'plugins/line-numbers/prism-line-numbers.min.js',
58			'plugins/line-highlight/prism-line-highlight.min.js',
59			'plugins/toolbar/prism-toolbar.min.js',
60			'plugins/copy-to-clipboard/prism-copy-to-clipboard.js',
61			'plugins/show-language/prism-show-language.min.js',
62			'plugins/command-line/prism-command-line.min.js',
63			'plugins/file-highlight/prism-file-highlight.min.js'
64		);
65
66		if ($this->getConf('show-invis')) {
67			$css_hrefs[] = 'plugins/show-invisibles/prism-show-invisibles.min.css';
68			$scripts[] = 'plugins/show-invisibles/prism-show-invisibles.min.js';
69		}
70
71		if ($this->getConf('hl-brace')) {
72			$css_hrefs[] = 'plugins/match-braces/prism-match-braces.min.css';
73			$scripts[] = 'plugins/match-braces/prism-match-braces.min.js';
74		}
75
76		if ($this->getConf('previewer')) {
77			/* Need to load css-extras.min.js to preview CSS and need to load css.min.js
78			 * first. so it seems impossible to use autoloder.min.js to load CSS resources.
79			 */
80			$scripts[] = 'components/prism-css.min.js';
81			$scripts[] = 'components/prism-css-extras.min.js';
82
83			$css_hrefs[] = 'plugins/previewers/prism-previewers.min.css';
84			$css_hrefs[] = 'plugins/inline-color/prism-inline-color.min.css';
85			$scripts[] = 'plugins/previewers/prism-previewers.min.js';
86			$scripts[] = 'plugins/inline-color/prism-inline-color.min.js';
87		}
88
89		foreach($css_hrefs as $href) {
90			$event->data['link'][] = array(
91				'rel' => 'stylesheet',
92				'href' => $cdn . $href
93			);
94		}
95
96		foreach($scripts as $script) {
97			$event->data['script'][] = array(
98				'src' => $cdn . $script,
99				'_data' => ''
100			);
101		}
102	}
103}
104