diff options
Diffstat (limited to 'framework/Web/UI/WebControls/TTextHighlighter.php')
-rw-r--r-- | framework/Web/UI/WebControls/TTextHighlighter.php | 111 |
1 files changed, 74 insertions, 37 deletions
diff --git a/framework/Web/UI/WebControls/TTextHighlighter.php b/framework/Web/UI/WebControls/TTextHighlighter.php index 80f6069b..0f221cc2 100644 --- a/framework/Web/UI/WebControls/TTextHighlighter.php +++ b/framework/Web/UI/WebControls/TTextHighlighter.php @@ -10,11 +10,10 @@ * @package System.Web.UI.WebControls
*/
-/**
- * Using GeSHi and TTextProcessor classes
- */
-Prado::using('System.3rdParty.geshi.geshi');
-Prado::using('System.Web.UI.WebControls.TTextProcessor');
+Prado::using('System.3rdParty.TextHighlighter.Text.Highlighter',false);
+Prado::using('System.3rdParty.TextHighlighter.Text.Highlighter.Renderer.Html',false); +Prado::using('System.Web.UI.WebControls.TTextProcessor'); + /**
* TTextHighlighter class.
@@ -34,6 +33,8 @@ Prado::using('System.Web.UI.WebControls.TTextProcessor'); */
class TTextHighlighter extends TTextProcessor
{
+ private static $_lineNumberStyle=array(TTextHighlighterLineNumberStyle::Li => HL_NUMBERS_LI, TTextHighlighterLineNumberStyle::Table => HL_NUMBERS_TABLE); + /**
* @return string tag name of the panel
*/
@@ -51,10 +52,12 @@ class TTextHighlighter extends TTextProcessor }
/**
- * @param string language whose syntax is to be used for highlighting.
+ * @param string language (case-insensitive) 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'.)
+ * in '3rdParty/TextHighlighter/Text/Highlighter'. Currently, the following languages are supported:
+ * ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT,
+ * MYSQL, PERL, PHP, PRADO, PYTHON, RUBY, SQL, XML
+ * If a language is not supported, it will be displayed as plain text.
*/
public function setLanguage($value)
{
@@ -92,7 +95,39 @@ class TTextHighlighter extends TTextProcessor {
$this->setViewState('CopyCode', TPropertyValue::ensureBoolean($value), false);
}
-
+ + /** + * @return TTextHighlighterLineNumberStyle style of row number, Table by default + */ + public function getLineNumberStyle() + { + return $this->getViewState('LineNumberStyle', TTextHighlighterLineNumberStyle::Table); + }
+ + /** + * @param TTextHighlighterLineNumberStyle style of row number + */ + public function setLineNumberStyle($value) + { + $this->setViewState('LineNumberStyle', TPropertyValue::ensureEnum($value,'TTextHighlighterLineNumberStyle')); + } + + /** + * @return integer tab size. Defaults to 4. + */ + public function getTabSize() + { + return $this->getViewState('TabSize', 4); + } + + /** + * @param integer tab size + */ + public function setTabSize($value) + { + $this->setViewState('TabSize', TPropertyValue::ensureInteger($value)); + } + /**
* Registers css style for the highlighted result.
* This method overrides parent implementation.
@@ -101,26 +136,21 @@ class TTextHighlighter extends TTextProcessor public function onPreRender($writer)
{
parent::onPreRender($writer);
- $this->registerHighlightScripts();
- }
-
- /**
- * Register CSS stylesheet file and javascript file.
- * @throws TConfigurationException if highlight.css file cannot be found
- */
- protected function registerHighlightScripts()
- {
- $cs=$this->getPage()->getClientScript();
- $cssKey='prado:TTextHighlighter';
- if(!$cs->isStyleSheetFileRegistered($cssKey))
- {
- if(($cssFile=Prado::getPathOfNamespace('System.3rdParty.geshi.highlight','.css'))===null)
- throw new TConfigurationException('texthighlighter_stylesheet_invalid');
- $styleSheet = $this->publishFilePath($cssFile);
- $cs->registerStyleSheetFile($cssKey, $styleSheet);
- }
- $cs->registerPradoScript('prado');
+ $this->registerStyleSheet();
+ $this->getPage()->getClientScript()->registerPradoScript('prado');
}
+ + /** + * Registers the stylesheet for presentation. + */ + protected function registerStyleSheet() + { + $cs=$this->getPage()->getClientScript(); + $cssFile=Prado::getPathOfNamespace('System.3rdParty.TextHighlighter.highlight','.css'); + $cssKey='prado:TTextHighlighter:'.$cssFile; + if(!$cs->isStyleSheetFileRegistered($cssKey)) + $cs->registerStyleSheetFile($cssKey, $this->publishFilePath($cssFile)); + } /**
* Processes a text string.
@@ -129,15 +159,16 @@ class TTextHighlighter extends TTextProcessor * @return string the processed text result
*/
public function processText($text)
- {
- $geshi = new GeSHi(trim($text), $this->getLanguage());
- if($this->getShowLineNumbers())
- $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
- $geshi->enable_classes();
- if($this->getEnableCopyCode())
- $geshi->set_header_content($this->getHeaderTemplate());
-
- return $geshi->parse_code();
+ { + if(($highlighter=Text_Highlighter::factory($this->getLanguage()))===false) + return ('<pre>'.htmlentities(trim($text)).'</pre>'); + + $options["use_language"]=true;
+ $options["tabsize"] = $this->getTabSize();
+ if ($this->getShowLineNumbers())
+ $options["numbers"] = self::$_lineNumberStyle[$this->getLineNumberStyle()];
+ $highlighter->setRenderer(new Text_Highlighter_Renderer_Html($options));
+ return $highlighter->highlight(trim($text));
}
/**
@@ -148,5 +179,11 @@ class TTextHighlighter extends TTextProcessor $id = $this->getClientID();
return TJavaScript::renderScriptBlock("new Prado.WebUI.TTextHighlighter('{$id}');");
}
+} + +class TTextHighlighterLineNumberStyle extends TEnumerable +{ + const Li='Li'; + const Table='Table'; }
?>
\ No newline at end of file |