* @link http://www.yiiframework.com/ * @copyright Copyright © 2008-2009 Yii Software LLC * @license http://www.yiiframework.com/license/ */ require_once(Yii::getPathOfAlias('system.vendors.markdown.markdown').'.php'); /** * 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: *
".$codeblock.""; } /** * Returns the user-entered highlighting options. * @param string 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 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->getHiglightConfig($options))); return $highlighter; } /** * Generates the config for the highlighter. * @param string user-entered options * @return array the highlighter config */ public function getHiglightConfig($options) { $config['use_language'] = true; if( $this->getInlineOption('showLineNumbers', $options, false) ) $config['numbers'] = HL_NUMBERS_LI; $config['tabsize'] = $this->getInlineOption('tabSize', $options, 4); return $config; } /** * Retrieves the specified configuration. * @param string the configuration name * @param string the user-entered options * @param mixed 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; } }