summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TTextHighlighter.php
blob: be1048961db8e773d9fa4b3ff34d1d7874b3dd37 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
 * TTextHighlighter class file
 *
 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005 Wei Zhuo
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 */

/**
 * Using GeSHi and TTextWriter classes
 */
Prado::using('System.3rdParty.geshi.geshi');

/**
 * TTextHighlighter class.
 *
 * TTextHighlighter does syntax highlighting its body content, including
 * static text and rendering results of child controls.
 * You can set {@link setLanguage Language} to specify what kind of syntax
 * the body content is. Currently, TTextHighlighter supports the following
 * languages: 'php','prado','css','html','javascript' and 'xml', where 'prado'
 * refers to PRADO template syntax. By setting {@link setShowLineNumbers ShowLineNumbers}
 * to true, the highlighted result may be shown with line numbers.
 *
 * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
 * @version $Revision: $  $Date: $
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TTextHighlighter extends TWebControl
{
	/**
	 * @return string tag name of the panel
	 */
	protected function getTagName()
	{
		return 'div';
	}

	/**
	 * @return string language whose syntax is to be used for highlighting. Defaults to 'php'.
	 */
	public function getLanguage()
	{
		return $this->getViewState('Language', 'php');
	}

	/**
	 * @param string language whose syntax is to be used for highlighting.
	 * Valid values are those file names (without suffix) that are contained
	 * in '3rdParty/geshi/geshi' (e.g. 'php','prado','css','html','javascript',
	 * 'xml'.)
	 */
	public function setLanguage($value)
	{
		$this->setViewState('Language', $value, 'php');
	}

	/**
	 * @return boolean whether to show line numbers in the highlighted result.
	 */
	public function getShowLineNumbers()
	{
		return $this->getViewState('ShowLineNumbers', false);
	}

	/**
	 * @param boolean whether to show line numbers in the highlighted result.
	 */
	public function setShowLineNumbers($value)
	{
		$this->setViewState('ShowLineNumbers', TPropertyValue::ensureBoolean($value), false);
	}

	/**
	 * Registers css style for the highlighted result.
	 * This method overrides parent implementation.
	 * @param THtmlWriter writer
	 */
	protected function onPreRender($writer)
	{
		parent::onPreRender($writer);
		$this->registerHighlightStyleSheet();
	}

	/**
	 * HTML-decodes static text.
	 * This method overrides parent implementation.
	 * @param mixed object to be added as body content
	 */
	public function addParsedObject($object)
	{
		if(is_string($object))
			$object=html_entity_decode($object);
		parent::addParsedObject($object);
	}

	/**
	 * Renders body content.
	 * This method overrides parent implementation by replacing
	 * the body content with syntax highlighted result.
	 * @param THtmlWriter writer
	 */
	protected function renderContents($writer)
	{
		$textWriter=new TTextWriter;
		parent::renderContents(new THtmlWriter($textWriter));
		$writer->write($this->highlightText($textWriter->flush()));
	}

	/**
	 * Register CSS style sheet file.
	 */
	protected function registerHighlightStyleSheet()
	{
		$cs = $this->getPage()->getClientScript();
		$cssKey='prado:TTextHighlighter';
		if(!$cs->isStyleSheetFileRegistered($cssKey))
		{
			$cssFile=Prado::getPathOfNamespace('System.3rdParty.geshi.highlight','.css');
			$styleSheet = $this->getService()->getAsset($cssFile);
			$cs->registerStyleSheetFile($cssKey, $styleSheet);
		}
	}

	/**
	 * Returns the highlighted text.
	 * @param string text to highlight.
	 * @return string highlighted text.
	 */
	protected function highlightText($text)
	{
		$geshi = new GeSHi(trim($text), $this->getLanguage());
		if($this->getShowLineNumbers())
			$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
		$geshi->enable_classes();
		return $geshi->parse_code();
	}
}
?>