Completed
Push — master ( d71f5d...704662 )
by Fabio
21:55
created

TTextHighlighter::setEncodeHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TTextHighlighter class file
4
 *
5
 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/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
 * By default the contents are encoded using {@link THttpUtility::htmlEncode} before being rendered, and
33
 * any leading end-of-line character is removed to avoid an empty line being rendered.
34
 * Setting {@link setEncodeHtml EncodeHtml} to false the original content will be rendered.
35
 *
36
 * Note, TTextHighlighter requires {@link THead} to be placed on the page template
37
 * because it needs to insert some CSS styles.
38
 *
39
 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
40
 * @package Prado\Web\UI\WebControls
41
 * @since 3.0
42
 */
43
class TTextHighlighter extends TWebControl
44
{
45
	/**
46
	 * @return string tag name of the panel
47
	 */
48
	protected function getTagName()
49
	{
50
		return 'pre';
51
	}
52
53
	/**
54
	 * @return string language whose syntax is to be used for highlighting. Defaults to 'php'.
55
	 */
56
	public function getLanguage()
57
	{
58
		return $this->getViewState('Language', 'php');
59
	}
60
61
	/**
62
	 * @param string language (case-insensitive) whose syntax is to be used for highlighting.
63
	 * If a language is not supported, it will be displayed as plain text.
64
	 */
65
	public function setLanguage($value)
66
	{
67
		$this->setViewState('Language', strtolower($value), 'php');
68
	}
69
70
	/**
71
	 * @return boolean whether to show line numbers in the highlighted result.
72
	 */
73
	public function getShowLineNumbers()
74
	{
75
		return $this->getViewState('ShowLineNumbers', false);
76
	}
77
78
	/**
79
	 * @param boolean whether to show line numbers in the highlighted result.
80
	 */
81
	public function setShowLineNumbers($value)
82
	{
83
		$this->setViewState('ShowLineNumbers', TPropertyValue::ensureBoolean($value), false);
84
	}
85
86
	/**
87
	 * @return boolean true will show "Copy Code" link. Defaults to false.
88
	 */
89
	public function getEnableCopyCode()
90
	{
91
		return $this->getViewState('CopyCode', false);
92
	}
93
94
	/**
95
	 * @param boolean true to show the "Copy Code" link.
96
	 */
97
	public function setEnableCopyCode($value)
98
	{
99
		$this->setViewState('CopyCode', TPropertyValue::ensureBoolean($value), false);
100
	}
101
102
	/**
103
	 * @return style of syntax highlightning
104
	 */
105
	public function getSyntaxStyle()
106
	{
107
		return $this->getViewState('SyntaxStyle', 'default');
108
	}
109
110
	/**
111
	 * @param style of syntax highlightning
112
	 */
113
	public function setSyntaxStyle($value)
114
	{
115
		$this->setViewState('SyntaxStyle', TPropertyValue::ensureString($value), 'default');
116
	}
117
118
	/**
119
	 * @return integer tab size. Defaults to 4.
120
	 */
121
	public function getTabSize()
122
	{
123
		return $this->getViewState('TabSize', 4);
124
	}
125
126
	/**
127
	 * @param integer tab size
128
	 */
129
	public function setTabSize($value)
130
	{
131
		$this->setViewState('TabSize', TPropertyValue::ensureInteger($value));
132
	}
133
134
	/**
135
	 * @return boolean wether the contents are html encoded. Defaults to true.
136
	 */
137
	public function getEncodeHtml()
138
	{
139
		return $this->getViewState('EncodeHtml', true);
140
	}
141
142
	/**
143
	 * @param boolean wether to html-encode the contents using {@link THttpUtility::htmlEncode}.
144
	 */
145
	public function setEncodeHtml($value)
146
	{
147
		$this->setViewState('EncodeHtml', TPropertyValue::ensureBoolean($value), true);
148
	}
149
150
	/**
151
	 * Registers css style for the highlighted result.
152
	 * This method overrides parent implementation.
153
	 * @param THtmlWriter writer
154
	 */
155
	public function onPreRender($writer)
156
	{
157
		parent::onPreRender($writer);
158
		$this->registerStyleSheet();
159
	}
160
161
	/**
162
	 * Registers the stylesheet for presentation.
163
	 */
164
	protected function registerStyleSheet()
165
	{
166
		$cs=$this->getPage()->getClientScript();
167
		$cssFile=Prado::getPathOfNamespace('Vendor.bower-asset.highlightjs.styles.'.$this->getSyntaxStyle(),'.css');
168
		$cssKey='prado:TTextHighlighter:'.$cssFile;
169
		if(!$cs->isStyleSheetFileRegistered($cssKey))
170
			$cs->registerStyleSheetFile($cssKey, $this->publishFilePath($cssFile));
171
	}
172
173
	/**
174
	 * Get javascript text highlighter options.
175
	 * @return array text highlighter client-side options
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|boolean>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
176
	 */
177
	protected function getTextHighlightOptions()
178
	{
179
		$options = array();
180
		$options['ID'] = $this->getClientID();
181
		$options['tabsize'] = str_repeat(' ', $this->getTabSize());
182
		$options['copycode'] = $this->getEnableCopyCode();
183
		$options['linenum'] = $this->getShowLineNumbers();
184
185
		return $options;
186
	}
187
188
	/**
189
	 * Renders the openning tag for the control (including attributes)
190
	 * @param THtmlWriter the writer used for the rendering purpose
191
	 */
192
	public function renderBeginTag($writer)
193
	{
194
		$this->renderClientControlScript($writer);
195
		$writer->addAttribute('id', $this->getClientID());
196
		parent::renderBeginTag($writer);
197
198
		$writer->addAttribute('id', $this->getClientID().'_code');
199
		$writer->addAttribute('class', $this->getLanguage());
200
		$writer->renderBeginTag('code');
201
	}
202
203
	/**
204
	 * Renders the body content enclosed between the control tag.
205
	 * By default, child controls and text strings will be rendered.
206
	 * You can override this method to provide customized content rendering.
207
	 * @param THtmlWriter the writer used for the rendering purpose
208
	 */
209
	public function renderContents($writer)
210
	{
211
		if($this->getEncodeHtml())
212
		{
213
			$escapedWriter = new TTextHighlighterWriter($writer);
214
			parent::renderChildren($escapedWriter);
215
		} else {
216
			parent::renderChildren($writer);
217
		}
218
	}
219
220
	/**
221
	 * Renders the closing tag for the control
222
	 * @param THtmlWriter the writer used for the rendering purpose
223
	 */
224
	public function renderEndTag($writer)
225
	{
226
		$writer->renderEndTag();
227
		parent::renderEndTag($writer);
228
	}
229
230
	protected function renderClientControlScript($writer)
231
	{
232
		$cs = $this->getPage()->getClientScript();
233
		$cs->registerPradoScript('texthighlight');
234
235
		$options = TJavaScript::encode($this->getTextHighlightOptions());
236
		$code = "new Prado.WebUI.TTextHighlighter($options);";
237
		$cs->registerEndScript("prado:".$this->getClientID(), $code);
238
239
	}
240
}
241