From ccdb91a35602377fe389c6c5b0945478929e0eca Mon Sep 17 00:00:00 2001 From: wei <> Date: Wed, 28 Dec 2005 22:41:20 +0000 Subject: Adding TTextHighlighter for quickstart docs. --- .../protected/controls/TTextHighlighter.php | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 demos/quickstart/protected/controls/TTextHighlighter.php (limited to 'demos/quickstart/protected/controls/TTextHighlighter.php') diff --git a/demos/quickstart/protected/controls/TTextHighlighter.php b/demos/quickstart/protected/controls/TTextHighlighter.php new file mode 100644 index 00000000..d8bb6bba --- /dev/null +++ b/demos/quickstart/protected/controls/TTextHighlighter.php @@ -0,0 +1,97 @@ + + * @version $Revision: 1.66 $ $Date: ${DATE} ${TIME} $ + * @package ${package} + */ +class TTextHighlighter extends TWebControl +{ + /** + * @return string tag name of the panel + */ + protected function getTagName() + { + return 'div'; + } + + public function getLanguage() + { + return $this->getViewState('Language', ''); + } + + public function setLanguage($value) + { + $this->setViewState('Language', $value, ''); + } + + public function setEnableLineNumbers($value) + { + $this->setViewState('LineNumbers', TPropertyValue::ensureBoolean($value), false); + } + + public function getEnableLineNumbers() + { + return $this->getViewState('LineNumbers', false); + } + + public function getEnableEntities() + { + return $this->getViewState('Entities', false); + } + + public function setEnableEntities($value) + { + $this->setViewState('Entities', TPropertyValue::ensureBoolean($value), false); + } + + /** + * Parse the body string using GeSHi to highlight the contents. + */ + public function addParsedObject($object) + { + if(is_string($object)) + { + $this->registerTextHighlightStyleSheet(); + $this->getControls()->add($this->getTextHighlight($object)); + } + else + $this->getControls()->add($object); + } + + /** + * Register CSS style sheet file. + */ + protected function registerTextHighlightStyleSheet() + { + $cs = $this->getPage()->getClientScript(); + if(!$cs->isStyleSheetFileRegistered(get_class($this))) + { + $styleSheet = $this->getAsset('Highlighter/code_highlight.css'); + $cs->registerStyleSheetFile(get_class($this), $styleSheet); + } + } + + /** + * Returns the highlighted text. + * @param string text to highlight. + * @return string highlighted text. + */ + protected function getTextHighlight($text) + { + if(!$this->getEnableEntities()) + $text = html_entity_decode($text); + $geshi = new GeSHi(trim($text), $this->getLanguage()); + if($this->getEnableLineNumbers()) + $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); + $geshi->enable_classes(); + return $geshi->parse_code(); + } +} +?> \ No newline at end of file -- cgit v1.2.3