* @link http://www.yiiframework.com/ * @copyright 2008-2013 Yii Software LLC * @license http://www.yiiframework.com/license/ */ require_once(Yii::getPathOfAlias('system.vendors.markdown.markdown').'.php'); if(!class_exists('HTMLPurifier_Bootstrap',false)) { require_once(Yii::getPathOfAlias('system.vendors.htmlpurifier').DIRECTORY_SEPARATOR.'HTMLPurifier.standalone.php'); HTMLPurifier_Bootstrap::registerAutoload(); } /** * CMarkdownParser is a wrapper of {@link http://michelf.com/projects/php-markdown/extra/ MarkdownExtra_Parser}. * * CMarkdownParser extends MarkdownExtra_Parser by using Text_Highlighter * to highlight code blocks with specific language syntax. * In particular, if a code block starts with the following: *
* [language] ** The syntax for the specified language will be used to highlight * code block. The languages supported include (case-insensitive): * ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT, * MYSQL, PERL, PHP, PYTHON, RUBY, SQL, XML * * You can also specify options to be passed to the syntax highlighter. For example: *
* [php showLineNumbers=1] ** which will show line numbers in each line of the code block. * * For details about the standard markdown syntax, please check the following: *
".CHtml::encode($codeblock).""; } /** * Returns the user-entered highlighting options. * @param string $codeblock code block with highlighting options. * @return string the user-entered highlighting options. Null if no option is entered. */ protected function getHighlightTag($codeblock) { $str = trim(current(preg_split("/\r|\n/", $codeblock,2))); if(strlen($str) > 2 && $str[0] === '[' && $str[strlen($str)-1] === ']') return $str; } /** * Creates a highlighter instance. * @param string $options the user-entered options * @return Text_Highlighter the highlighter instance */ protected function createHighLighter($options) { if(!class_exists('Text_Highlighter', false)) { require_once(Yii::getPathOfAlias('system.vendors.TextHighlighter.Text.Highlighter').'.php'); require_once(Yii::getPathOfAlias('system.vendors.TextHighlighter.Text.Highlighter.Renderer.Html').'.php'); } $lang = current(preg_split('/\s+/', substr(substr($options,1), 0,-1),2)); $highlighter = Text_Highlighter::factory($lang); if($highlighter) $highlighter->setRenderer(new Text_Highlighter_Renderer_Html($this->getHighlightConfig($options))); return $highlighter; } /** * Generates the config for the highlighter. * @param string $options user-entered options * @return array the highlighter config */ public function getHighlightConfig($options) { $config = array('use_language'=>true); if( $this->getInlineOption('showLineNumbers', $options, false) ) $config['numbers'] = HL_NUMBERS_LI; $config['tabsize'] = $this->getInlineOption('tabSize', $options, 4); return $config; } /** * Generates the config for the highlighter. * * NOTE: This method is deprecated due to a mistake in the method name. * Use {@link getHighlightConfig} instead of this. * * @param string $options user-entered options * @return array the highlighter config */ public function getHiglightConfig($options) { return $this->getHighlightConfig($options); } /** * Retrieves the specified configuration. * @param string $name the configuration name * @param string $str the user-entered options * @param mixed $defaultValue default value if the configuration is not present * @return mixed the configuration value */ protected function getInlineOption($name, $str, $defaultValue) { if(preg_match('/'.$name.'(\s*=\s*(\d+))?/i', $str, $v) && count($v) > 2) return $v[2]; else return $defaultValue; } }