1: <?php
2: /**
3: * CTextHighlighter class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: require_once(Yii::getPathOfAlias('system.vendors.TextHighlighter.Text.Highlighter').'.php');
12: require_once(Yii::getPathOfAlias('system.vendors.TextHighlighter.Text.Highlighter.Renderer.Html').'.php');
13:
14: /**
15: * CTextHighlighter does syntax highlighting for its body content.
16: *
17: * The language of the syntax to be applied is specified via {@link language} property.
18: * Currently, CTextHighlighter supports the following languages:
19: * ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT, MYSQL, PERL,
20: * PHP, PYTHON, RUBY, SQL, XML. By setting {@link showLineNumbers}
21: * to true, the highlighted result may be shown with line numbers.
22: *
23: * @author Qiang Xue <qiang.xue@gmail.com>
24: * @package system.web.widgets
25: * @since 1.0
26: */
27: class CTextHighlighter extends COutputProcessor
28: {
29: /**
30: * @var string the language whose syntax is to be used for highlighting.
31: * Valid values are those file names (without suffix) that are contained
32: * in 'vendors/TextHighlighter/Text/Highlighter'. Currently, the following
33: * languages are supported:
34: * ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT,
35: * MYSQL, PERL, PHP, PYTHON, RUBY, SQL, XML
36: * If a language is not supported, it will be displayed as plain text.
37: * Language names are case-insensitive.
38: */
39: public $language;
40: /**
41: * @var boolean whether to show line numbers in the highlighted result. Defaults to false.
42: * @see lineNumberStyle
43: */
44: public $showLineNumbers=false;
45: /**
46: * @var string the style of line number display. It can be either 'list' or 'table'. Defaults to 'list'.
47: * @see showLineNumbers
48: */
49: public $lineNumberStyle='list';
50: /**
51: * @var integer tab size. Defaults to 4.
52: */
53: public $tabSize=4;
54: /**
55: * @var mixed the CSS file used for the widget. Defaults to null, meaning
56: * using the default CSS file included together with the widget.
57: * If false, no CSS file will be used. Otherwise, the specified CSS file
58: * will be included when using this widget.
59: */
60: public $cssFile;
61: /**
62: * @var array the HTML attributes to be applied to the container element.
63: * The highlighted content is contained in a DIV element.
64: */
65: public $containerOptions=array();
66:
67:
68: /**
69: * Processes the captured output.
70: * This method highlights the output according to the syntax of the specified {@link language}.
71: * @param string $output the captured output to be processed
72: */
73: public function processOutput($output)
74: {
75: $output=$this->highlight($output);
76: parent::processOutput($output);
77: }
78:
79: /**
80: * Highlights the content by the syntax of the specified language.
81: * @param string $content the content to be highlighted.
82: * @return string the highlighted content
83: */
84: public function highlight($content)
85: {
86: $this->registerClientScript();
87:
88: $options['use_language']=true;
89: $options['tabsize']=$this->tabSize;
90: if($this->showLineNumbers)
91: $options['numbers']=($this->lineNumberStyle==='list')?HL_NUMBERS_LI:HL_NUMBERS_TABLE;
92:
93: $highlighter=empty($this->language)?false:Text_Highlighter::factory($this->language);
94: if($highlighter===false)
95: $o='<pre>'.CHtml::encode($content).'</pre>';
96: else
97: {
98: $highlighter->setRenderer(new Text_Highlighter_Renderer_Html($options));
99: $o=preg_replace('/<span\s+[^>]*>(\s*)<\/span>/','\1',$highlighter->highlight($content));
100: }
101:
102: return CHtml::tag('div',$this->containerOptions,$o);
103: }
104:
105: /**
106: * Registers the needed CSS and JavaScript.
107: */
108: public function registerClientScript()
109: {
110: if($this->cssFile!==false)
111: self::registerCssFile($this->cssFile);
112: }
113:
114: /**
115: * Registers the needed CSS file.
116: * @param string $url the CSS URL. If null, a default CSS URL will be used.
117: */
118: public static function registerCssFile($url=null)
119: {
120: if($url===null)
121: $url=CHtml::asset(Yii::getPathOfAlias('system.vendors.TextHighlighter.highlight').'.css');
122: Yii::app()->getClientScript()->registerCssFile($url);
123: }
124: }
125: