summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/controls/TTextHighlighter.php
diff options
context:
space:
mode:
Diffstat (limited to 'demos/quickstart/protected/controls/TTextHighlighter.php')
-rw-r--r--demos/quickstart/protected/controls/TTextHighlighter.php97
1 files changed, 97 insertions, 0 deletions
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 @@
+<?php
+
+require_once(dirname(__FILE__).'/Highlighter/geshi.php');
+
+/**
+ * ${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', '');
+ }
+
+ 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