-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathEBootstrapCode.php
104 lines (87 loc) · 2.6 KB
/
EBootstrapCode.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Render a syntaxhighlighted code
*
* Write the code to highlight between beginWidget and endWidget or pass it as 'content' parameter
* This widget includes the google code prettify library http://code.google.com/p/google-code-prettify/
* http://google-code-prettify.googlecode.com/svn/trunk/README.html
*
* @author Tim Helfensdörfer <tim@visualappeal.de>
* @version 0.3.0
* @package bootstrap.widgets
*/
class EBootstrapCode extends EBootstrapWidget {
/**
* Language for syntaxhighliting
*/
public $language = '';
/**
* If content is empty the code between beginWidget and endWidget is cached
*/
public $content = '';
/**
* Enable line numbers
*/
public $lineNumbers = false;
/**
* The css file for styling the code
*
* If its set to false, no file will be included
*/
public $cssFile = null;
/**
* The JS file for highlighting the code
*
* If its set to false, no file will be included
*/
public $jsFile = null;
/**
* Init the widget
*/
public function init() {
parent::init();
if (empty($this->content))
ob_start();
if (is_null($this->jsFile)) {
$jsFile = dirname(__FILE__).'/js/google-code-prettify/prettify.js';
$this->jsFile = Yii::app()->getAssetManager()->publish($jsFile);
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}
elseif ($this->jsFile !== false) {
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}
if (is_null($this->cssFile)) {
$cssFile = dirname(__FILE__).'/js/google-code-prettify/prettify.css';
$this->cssFile = Yii::app()->getAssetManager()->publish($cssFile);
Yii::app()->clientScript->registerCssFile($this->cssFile);
}
elseif ($this->cssFile !== false) {
Yii::app()->clientScript->registerCssFile($this->cssFile);
}
Yii::app()->clientScript->RegisterScript('ebootstrapcode-prettify.'.$this->getId(), 'prettyPrint();', CClientScript::POS_READY);
}
/**
* Render the widget
*/
public function run() {
parent::run();
if (empty($this->content)) {
$content = ob_get_contents();
$content = str_replace('>', '>', $content);
$content = str_replace('<', '<', $content);
ob_end_clean();
}
else {
$content = $this->content;
}
EBootstrap::mergeClass($this->htmlOptions, array('prettyprint', 'well'));
if (!empty($this->language))
EBootstrap::mergeClass($this->htmlOptions, array('lang-'.$this->language));
if ($this->lineNumbers)
EBootstrap::mergeClass($this->htmlOptions, array('linenums'));
echo CHtml::openTag('pre', $this->htmlOptions);
echo htmlspecialchars($content);
echo CHtml::closeTag('pre');
}
}
?>