|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TTextHighlighter class file |
|
4
|
|
|
* |
|
5
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado4 |
|
7
|
|
|
* @copyright Copyright © 2005-2016 The PRADO Group |
|
8
|
|
|
* @license https://github.com/pradosoft/prado4/blob/master/LICENSE |
|
9
|
|
|
* @package Prado\Web\UI\WebControls |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Prado\Web\UI\WebControls; |
|
13
|
|
|
use Prado\TPropertyValue; |
|
14
|
|
|
use Prado\Prado; |
|
15
|
|
|
use Prado\Web\Javascripts\TJavaScript; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TTextHighlighter class. |
|
19
|
|
|
* |
|
20
|
|
|
* TTextHighlighter does syntax highlighting its body content, including |
|
21
|
|
|
* static text and rendering results of child controls. |
|
22
|
|
|
* You can set {@link setLanguage Language} to specify what kind of syntax |
|
23
|
|
|
* the body content is and {@link setSyntaxStyle SyntaxStyle} to specify the |
|
24
|
|
|
* style used to highlight the content. |
|
25
|
|
|
* |
|
26
|
|
|
* The list of supported syntaxes is available at https://github.com/isagalaev/highlight.js/tree/master/src/languages |
|
27
|
|
|
* The list of supported styles is available at https://github.com/isagalaev/highlight.js/tree/master/src/styles |
|
28
|
|
|
* |
|
29
|
|
|
* By setting {@link setShowLineNumbers ShowLineNumbers} to true, the highlighted |
|
30
|
|
|
* result may be shown with line numbers. To style lin numbers, use the css class "hljs-line-numbers". |
|
31
|
|
|
* |
|
32
|
|
|
* Note, TTextHighlighter requires {@link THead} to be placed on the page template |
|
33
|
|
|
* because it needs to insert some CSS styles. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Wei Zhuo<weizhuo[at]gmail[dot]com> |
|
36
|
|
|
* @package Prado\Web\UI\WebControls |
|
37
|
|
|
* @since 3.0 |
|
38
|
|
|
*/ |
|
39
|
|
|
class TTextHighlighter extends TWebControl |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* @return string tag name of the panel |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function getTagName() |
|
45
|
|
|
{ |
|
46
|
|
|
return 'pre'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string language whose syntax is to be used for highlighting. Defaults to 'php'. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getLanguage() |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->getViewState('Language', 'php'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string language (case-insensitive) whose syntax is to be used for highlighting. |
|
59
|
|
|
* If a language is not supported, it will be displayed as plain text. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setLanguage($value) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->setViewState('Language', strtolower($value), 'php'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return boolean whether to show line numbers in the highlighted result. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getShowLineNumbers() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->getViewState('ShowLineNumbers', false); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param boolean whether to show line numbers in the highlighted result. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setShowLineNumbers($value) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->setViewState('ShowLineNumbers', TPropertyValue::ensureBoolean($value), false); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return boolean true will show "Copy Code" link. Defaults to false. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getEnableCopyCode() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getViewState('CopyCode', false); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param boolean true to show the "Copy Code" link. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setEnableCopyCode($value) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->setViewState('CopyCode', TPropertyValue::ensureBoolean($value), false); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return style of syntax highlightning |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getSyntaxStyle() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->getViewState('SyntaxStyle', 'default'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param style of syntax highlightning |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setSyntaxStyle($value) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->setViewState('SyntaxStyle', TPropertyValue::ensureString($value), 'default'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return integer tab size. Defaults to 4. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getTabSize() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getViewState('TabSize', 4); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param integer tab size |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setTabSize($value) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->setViewState('TabSize', TPropertyValue::ensureInteger($value)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Registers css style for the highlighted result. |
|
132
|
|
|
* This method overrides parent implementation. |
|
133
|
|
|
* @param THtmlWriter writer |
|
134
|
|
|
*/ |
|
135
|
|
|
public function onPreRender($writer) |
|
136
|
|
|
{ |
|
137
|
|
|
parent::onPreRender($writer); |
|
138
|
|
|
$this->registerStyleSheet(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Registers the stylesheet for presentation. |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function registerStyleSheet() |
|
145
|
|
|
{ |
|
146
|
|
|
$cs=$this->getPage()->getClientScript(); |
|
147
|
|
|
$cssFile=Prado::getPathOfNamespace('Vendor.bower-asset.highlightjs.styles.'.$this->getSyntaxStyle(),'.css'); |
|
148
|
|
|
$cssKey='prado:TTextHighlighter:'.$cssFile; |
|
149
|
|
|
if(!$cs->isStyleSheetFileRegistered($cssKey)) |
|
150
|
|
|
$cs->registerStyleSheetFile($cssKey, $this->publishFilePath($cssFile)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get javascript text highlighter options. |
|
155
|
|
|
* @return array text highlighter client-side options |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getTextHighlightOptions() |
|
158
|
|
|
{ |
|
159
|
|
|
$options = array(); |
|
160
|
|
|
$options['ID'] = $this->getClientID(); |
|
161
|
|
|
$options['tabsize'] = str_repeat(' ', $this->getTabSize()); |
|
162
|
|
|
$options['copycode'] = $this->getEnableCopyCode(); |
|
163
|
|
|
$options['linenum'] = $this->getShowLineNumbers(); |
|
164
|
|
|
|
|
165
|
|
|
return $options; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Renders the openning tag for the control (including attributes) |
|
170
|
|
|
* @param THtmlWriter the writer used for the rendering purpose |
|
171
|
|
|
*/ |
|
172
|
|
|
public function renderBeginTag($writer) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->renderClientControlScript($writer); |
|
175
|
|
|
$writer->addAttribute('id', $this->getClientID()); |
|
176
|
|
|
parent::renderBeginTag($writer); |
|
177
|
|
|
|
|
178
|
|
|
$writer->addAttribute('id', $this->getClientID().'_code'); |
|
179
|
|
|
$writer->addAttribute('class', $this->getLanguage()); |
|
180
|
|
|
$writer->renderBeginTag('code'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Renders the closing tag for the control |
|
185
|
|
|
* @param THtmlWriter the writer used for the rendering purpose |
|
186
|
|
|
*/ |
|
187
|
|
|
public function renderEndTag($writer) |
|
188
|
|
|
{ |
|
189
|
|
|
$writer->renderEndTag(); |
|
190
|
|
|
parent::renderEndTag($writer); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
protected function renderClientControlScript($writer) |
|
194
|
|
|
{ |
|
195
|
|
|
$cs = $this->getPage()->getClientScript(); |
|
196
|
|
|
$cs->registerPradoScript('texthighlight'); |
|
197
|
|
|
|
|
198
|
|
|
$options = TJavaScript::encode($this->getTextHighlightOptions()); |
|
199
|
|
|
$code = "new Prado.WebUI.TTextHighlighter($options);"; |
|
200
|
|
|
$cs->registerEndScript("prado:".$this->getClientID(), $code); |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.