summaryrefslogtreecommitdiff
path: root/framework/Web/UI/WebControls/TTextHighlighter.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Web/UI/WebControls/TTextHighlighter.php')
-rw-r--r--framework/Web/UI/WebControls/TTextHighlighter.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/framework/Web/UI/WebControls/TTextHighlighter.php b/framework/Web/UI/WebControls/TTextHighlighter.php
new file mode 100644
index 00000000..ec21b344
--- /dev/null
+++ b/framework/Web/UI/WebControls/TTextHighlighter.php
@@ -0,0 +1,92 @@
+<?php
+
+Prado::using('System.Web.UI.WebControls.Highlighter.geshi');
+Prado::using('System.IO.TTextWriter');
+
+/**
+ * ${classname}
+ *
+ * ${description}
+ *
+ * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
+ * @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', 'php');
+ }
+
+ public function setLanguage($value)
+ {
+ $this->setViewState('Language', $value, 'php');
+ }
+
+ public function setShowLineNumbers($value)
+ {
+ $this->setViewState('ShowLineNumbers', TPropertyValue::ensureBoolean($value), false);
+ }
+
+ public function getShowLineNumbers()
+ {
+ return $this->getViewState('ShowLineNumbers', false);
+ }
+
+ protected function onPreRender($writer)
+ {
+ parent::onPreRender($writer);
+ $this->registerTextHighlightStyleSheet();
+ }
+
+ public function addParsedObject($object)
+ {
+ if(is_string($object))
+ $object=html_entity_decode($object);
+ parent::addParsedObject($object);
+ }
+
+ 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 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 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();
+ }
+}
+?> \ No newline at end of file