CTextHighlighter.php 4.0 KB

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