| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TTextHighlighter class file
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo<weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TTextHighlighter.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
Prado::using('System.3rdParty.TextHighlighter.Text.Highlighter',false);
|
|
|
14 |
Prado::using('System.3rdParty.TextHighlighter.Text.Highlighter.Renderer.Html',false);
|
|
|
15 |
Prado::using('System.Web.UI.WebControls.TTextProcessor');
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TTextHighlighter class.
|
|
|
20 |
*
|
|
|
21 |
* TTextHighlighter does syntax highlighting its body content, including
|
|
|
22 |
* static text and rendering results of child controls.
|
|
|
23 |
* You can set {@link setLanguage Language} to specify what kind of syntax
|
|
|
24 |
* the body content is. Currently, TTextHighlighter supports the following
|
|
|
25 |
* languages: ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT, MYSQL, PERL,
|
|
|
26 |
* PHP, PYTHON, RUBY, SQL, XML and PRADO, where PRADO refers to PRADO template
|
|
|
27 |
* syntax. By setting {@link setShowLineNumbers ShowLineNumbers}
|
|
|
28 |
* to true, the highlighted result may be shown with line numbers.
|
|
|
29 |
*
|
|
|
30 |
* Note, TTextHighlighter requires {@link THead} to be placed on the page template
|
|
|
31 |
* because it needs to insert some CSS styles.
|
|
|
32 |
*
|
|
|
33 |
* @author Wei Zhuo<weizhuo[at]gmail[dot]com>
|
|
|
34 |
* @version $Id: TTextHighlighter.php 2541 2008-10-21 15:05:13Z qiang.xue $
|
|
|
35 |
* @package System.Web.UI.WebControls
|
|
|
36 |
* @since 3.0
|
|
|
37 |
*/
|
|
|
38 |
class TTextHighlighter extends TTextProcessor
|
|
|
39 |
{
|
|
|
40 |
private static $_lineNumberStyle=array(TTextHighlighterLineNumberStyle::Li => HL_NUMBERS_LI, TTextHighlighterLineNumberStyle::Table => HL_NUMBERS_TABLE);
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @return string tag name of the panel
|
|
|
44 |
*/
|
|
|
45 |
protected function getTagName()
|
|
|
46 |
{
|
|
|
47 |
return 'div';
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* @return string language whose syntax is to be used for highlighting. Defaults to 'php'.
|
|
|
52 |
*/
|
|
|
53 |
public function getLanguage()
|
|
|
54 |
{
|
|
|
55 |
return $this->getViewState('Language', 'php');
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* @param string language (case-insensitive) whose syntax is to be used for highlighting.
|
|
|
60 |
* Valid values are those file names (without suffix) that are contained
|
|
|
61 |
* in '3rdParty/TextHighlighter/Text/Highlighter'. Currently, the following languages are supported:
|
|
|
62 |
* ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT,
|
|
|
63 |
* MYSQL, PERL, PHP, PRADO, PYTHON, RUBY, SQL, XML
|
|
|
64 |
* If a language is not supported, it will be displayed as plain text.
|
|
|
65 |
*/
|
|
|
66 |
public function setLanguage($value)
|
|
|
67 |
{
|
|
|
68 |
$this->setViewState('Language', $value, 'php');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @return boolean whether to show line numbers in the highlighted result.
|
|
|
73 |
*/
|
|
|
74 |
public function getShowLineNumbers()
|
|
|
75 |
{
|
|
|
76 |
return $this->getViewState('ShowLineNumbers', false);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* @param boolean whether to show line numbers in the highlighted result.
|
|
|
81 |
*/
|
|
|
82 |
public function setShowLineNumbers($value)
|
|
|
83 |
{
|
|
|
84 |
$this->setViewState('ShowLineNumbers', TPropertyValue::ensureBoolean($value), false);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @return boolean true will show "Copy Code" link. Defaults to false.
|
|
|
89 |
*/
|
|
|
90 |
public function getEnableCopyCode()
|
|
|
91 |
{
|
|
|
92 |
return $this->getViewState('CopyCode', false);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* @param boolean true to show the "Copy Code" link.
|
|
|
97 |
*/
|
|
|
98 |
public function setEnableCopyCode($value)
|
|
|
99 |
{
|
|
|
100 |
$this->setViewState('CopyCode', TPropertyValue::ensureBoolean($value), false);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* @return TTextHighlighterLineNumberStyle style of row number, Table by default
|
|
|
105 |
*/
|
|
|
106 |
public function getLineNumberStyle()
|
|
|
107 |
{
|
|
|
108 |
return $this->getViewState('LineNumberStyle', TTextHighlighterLineNumberStyle::Table);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* @param TTextHighlighterLineNumberStyle style of row number
|
|
|
113 |
*/
|
|
|
114 |
public function setLineNumberStyle($value)
|
|
|
115 |
{
|
|
|
116 |
$this->setViewState('LineNumberStyle', TPropertyValue::ensureEnum($value,'TTextHighlighterLineNumberStyle'));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @return integer tab size. Defaults to 4.
|
|
|
121 |
*/
|
|
|
122 |
public function getTabSize()
|
|
|
123 |
{
|
|
|
124 |
return $this->getViewState('TabSize', 4);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* @param integer tab size
|
|
|
129 |
*/
|
|
|
130 |
public function setTabSize($value)
|
|
|
131 |
{
|
|
|
132 |
$this->setViewState('TabSize', TPropertyValue::ensureInteger($value));
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Registers css style for the highlighted result.
|
|
|
137 |
* This method overrides parent implementation.
|
|
|
138 |
* @param THtmlWriter writer
|
|
|
139 |
*/
|
|
|
140 |
public function onPreRender($writer)
|
|
|
141 |
{
|
|
|
142 |
parent::onPreRender($writer);
|
|
|
143 |
$this->registerStyleSheet();
|
|
|
144 |
$this->getPage()->getClientScript()->registerPradoScript('prado');
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Registers the stylesheet for presentation.
|
|
|
149 |
*/
|
|
|
150 |
protected function registerStyleSheet()
|
|
|
151 |
{
|
|
|
152 |
$cs=$this->getPage()->getClientScript();
|
|
|
153 |
$cssFile=Prado::getPathOfNamespace('System.3rdParty.TextHighlighter.highlight','.css');
|
|
|
154 |
$cssKey='prado:TTextHighlighter:'.$cssFile;
|
|
|
155 |
if(!$cs->isStyleSheetFileRegistered($cssKey))
|
|
|
156 |
$cs->registerStyleSheetFile($cssKey, $this->publishFilePath($cssFile));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Processes a text string.
|
|
|
161 |
* This method is required by the parent class.
|
|
|
162 |
* @param string text string to be processed
|
|
|
163 |
* @return string the processed text result
|
|
|
164 |
*/
|
|
|
165 |
public function processText($text)
|
|
|
166 |
{
|
|
|
167 |
try
|
|
|
168 |
{
|
|
|
169 |
$highlighter=Text_Highlighter::factory($this->getLanguage());
|
|
|
170 |
}
|
|
|
171 |
catch(Exception $e)
|
|
|
172 |
{
|
|
|
173 |
$highlighter=false;
|
|
|
174 |
}
|
|
|
175 |
if($highlighter===false)
|
|
|
176 |
return ('<pre>'.htmlentities(trim($text)).'</pre>');
|
|
|
177 |
|
|
|
178 |
$options["use_language"]=true;
|
|
|
179 |
$options["tabsize"] = $this->getTabSize();
|
|
|
180 |
if ($this->getShowLineNumbers())
|
|
|
181 |
$options["numbers"] = self::$_lineNumberStyle[$this->getLineNumberStyle()];
|
|
|
182 |
$highlighter->setRenderer(new Text_Highlighter_Renderer_Html($options));
|
|
|
183 |
return $highlighter->highlight(trim($text));
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
/**
|
|
|
187 |
* @return string header template with "Copy code" link.
|
|
|
188 |
*/
|
|
|
189 |
protected function getHeaderTemplate()
|
|
|
190 |
{
|
|
|
191 |
$id = $this->getClientID();
|
|
|
192 |
return TJavaScript::renderScriptBlock("new Prado.WebUI.TTextHighlighter('{$id}');");
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
class TTextHighlighterLineNumberStyle extends TEnumerable
|
|
|
197 |
{
|
|
|
198 |
const Li='Li';
|
|
|
199 |
const Table='Table';
|
|
|
200 |
}
|