-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighlightAsset.php
62 lines (54 loc) · 1.59 KB
/
HighlightAsset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace xinyeweb\highlight;
use yii\web\AssetBundle;
use yii\web\View;
use yii\helpers\Json;
/**
* This is just an example.
*/
class HighlightAsset extends AssetBundle
{
const DEFAULT_SELECTOR = 'pre code';
public $css = [
'styles/default.css',
];
public $js = [
'highlight.pack.js',
];
public $depends = [
'yii\web\JqueryAsset',
];
public function init() {
$this->sourcePath = dirname(__FILE__) .DIRECTORY_SEPARATOR . 'assets';
parent::init();
}
public $selector = self::DEFAULT_SELECTOR;
public $options = [];
public static function register($view)
{
$configOptions = [];
$configSelector = self::DEFAULT_SELECTOR;
try {
$thisBundle = \Yii::$app->getAssetManager()->getBundle(__CLASS__);
$configOptions = $thisBundle->options;
$configSelector = $thisBundle->selector;
} catch(\Exception $e) {
// do nothing...
}
$options = empty($configOptions) ? '' : Json::encode($configOptions);
if ($configSelector !== self::DEFAULT_SELECTOR) {
$view->registerJs('
hljs.configure(' . $options . ');
jQuery(\'' . $configSelector . '\').each(function(i, block) {
hljs.highlightBlock(block);
});');
} else {
$view->registerJs('
hljs.configure(' . $options . ');
hljs.initHighlightingOnLoad();',
View::POS_END
);
}
return parent::register($view);
}
}