CMarkdownParser

CDbHttpSession

Yii Framework v1.1.10 类参考

CMarkdownParser

system.utils
继承 class CMarkdownParser » MarkdownExtra_Parser » Markdown_Parser
源自 1.0
版本 $Id: CMarkdownParser.php 3515 2011-12-28 12:29:24Z mdomba $
源码 framework/utils/CMarkdownParser.php
CMarkdownParser is a wrapper of 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:

公共属性

隐藏继承属性

属性类型描述定义在
abbr_desciptions MarkdownExtra_Parser
abbr_word_re MarkdownExtra_Parser
auto_close_tags_re MarkdownExtra_Parser
block_gamut Markdown_Parser
block_tags_re MarkdownExtra_Parser
clean_tags_re MarkdownExtra_Parser
contain_span_tags_re MarkdownExtra_Parser
context_block_tags_re MarkdownExtra_Parser
document_gamut Markdown_Parser
em_relist MarkdownExtra_Parser
em_strong_prepared_relist Markdown_Parser
em_strong_relist MarkdownExtra_Parser
empty_element_suffix Markdown_Parser
escape_chars Markdown_Parser
escape_chars_re Markdown_Parser
fn_id_prefix MarkdownExtra_Parser
footnote_counter MarkdownExtra_Parser
footnotes MarkdownExtra_Parser
footnotes_ordered MarkdownExtra_Parser
highlightCssClass string the css class for the div element containing the code block that is highlighted. CMarkdownParser
html_hashes Markdown_Parser
in_anchor Markdown_Parser
list_level Markdown_Parser
nested_brackets_depth Markdown_Parser
nested_brackets_re Markdown_Parser
nested_url_parenthesis_depth Markdown_Parser
nested_url_parenthesis_re Markdown_Parser
no_entities Markdown_Parser
no_markup Markdown_Parser
predef_abbr MarkdownExtra_Parser
predef_titles Markdown_Parser
predef_urls Markdown_Parser
purifierOptions mixed the options to be passed to {@link http://htmlpurifier. CMarkdownParser
span_gamut Markdown_Parser
strong_relist MarkdownExtra_Parser
tab_width Markdown_Parser
titles Markdown_Parser
urls Markdown_Parser
utf8_strlen Markdown_Parser

公共方法

隐藏继承方法

方法描述定义在
__construct() MarkdownExtra_Parser
_appendFootnotes_callback() MarkdownExtra_Parser
_detab_callback() Markdown_Parser
_doAbbreviations_callback() MarkdownExtra_Parser
_doAnchors_inline_callback() Markdown_Parser
_doAnchors_reference_callback() Markdown_Parser
_doBlockQuotes_callback() Markdown_Parser
_doBlockQuotes_callback2() Markdown_Parser
_doCodeBlocks_callback() Callback function when a code block is matched. CMarkdownParser
_doDefLists_callback() MarkdownExtra_Parser
_doFencedCodeBlocks_callback() Callback function when a fenced code block is matched. CMarkdownParser
_doFencedCodeBlocks_newlines() MarkdownExtra_Parser
_doHardBreaks_callback() Markdown_Parser
_doHeaders_attr() MarkdownExtra_Parser
_doHeaders_callback_atx() MarkdownExtra_Parser
_doHeaders_callback_setext() MarkdownExtra_Parser
_doImages_inline_callback() Markdown_Parser
_doImages_reference_callback() Markdown_Parser
_doLists_callback() Markdown_Parser
_doTable_callback() MarkdownExtra_Parser
_doTable_leadingPipe_callback() MarkdownExtra_Parser
_hashHTMLBlocks_callback() Markdown_Parser
_hashHTMLBlocks_inHTML() MarkdownExtra_Parser
_hashHTMLBlocks_inMarkdown() MarkdownExtra_Parser
_initDetab() Markdown_Parser
_processDefListItems_callback_dd() MarkdownExtra_Parser
_processDefListItems_callback_dt() MarkdownExtra_Parser
_processListItems_callback() Markdown_Parser
_stripAbbreviations_callback() MarkdownExtra_Parser
_stripFootnotes_callback() MarkdownExtra_Parser
_stripLinkDefinitions_callback() Markdown_Parser
_unhash_callback() Markdown_Parser
appendFootnotes() MarkdownExtra_Parser
detab() Markdown_Parser
doAbbreviations() MarkdownExtra_Parser
doAnchors() Markdown_Parser
doBlockQuotes() Markdown_Parser
doCodeBlocks() Markdown_Parser
doDefLists() MarkdownExtra_Parser
doFencedCodeBlocks() MarkdownExtra_Parser
doFootnotes() MarkdownExtra_Parser
doHardBreaks() Markdown_Parser
doHeaders() MarkdownExtra_Parser
doHorizontalRules() Markdown_Parser
doImages() Markdown_Parser
doItalicsAndBold() Markdown_Parser
doLists() Markdown_Parser
doTables() MarkdownExtra_Parser
encodeAmpsAndAngles() Markdown_Parser
encodeAttribute() Markdown_Parser
encodeEmailAddress() Markdown_Parser
formParagraphs() MarkdownExtra_Parser
getDefaultCssFile() 返回the default CSS file that is used to highlight code blocks. CMarkdownParser
getHiglightConfig() Generates the config for the highlighter. CMarkdownParser
handleSpanToken() Markdown_Parser
hashBlock() Markdown_Parser
hashClean() MarkdownExtra_Parser
hashHTMLBlocks() MarkdownExtra_Parser
hashPart() Markdown_Parser
makeCodeSpan() Markdown_Parser
outdent() Markdown_Parser
parseSpan() Markdown_Parser
prepareItalicsAndBold() Markdown_Parser
processDefListItems() MarkdownExtra_Parser
processListItems() Markdown_Parser
runBasicBlockGamut() Markdown_Parser
runBlockGamut() Markdown_Parser
runSpanGamut() Markdown_Parser
safeTransform() Transforms the content and purifies the result. CMarkdownParser
setup() MarkdownExtra_Parser
stripAbbreviations() MarkdownExtra_Parser
stripFootnotes() MarkdownExtra_Parser
stripLinkDefinitions() Markdown_Parser
teardown() MarkdownExtra_Parser
transform() Markdown_Parser
unhash() Markdown_Parser

受保护方法

隐藏继承方法

方法描述定义在
createHighLighter() Creates a highlighter instance. CMarkdownParser
getHighlightTag() Returns the user-entered highlighting options. CMarkdownParser
getInlineOption() Retrieves the specified configuration. CMarkdownParser
highlightCodeBlock() Highlights the code block. CMarkdownParser

属性详细

highlightCssClass 属性
public string $highlightCssClass;

the css class for the div element containing the code block that is highlighted. Defaults to 'hl-code'.

purifierOptions 属性 (可用自 v1.1.4)
public mixed $purifierOptions;

the options to be passed to HTML Purifier. This can be a HTMLPurifier_Config object, an array of directives (Namespace.Directive => Value) or the filename of an ini file. This property is used only when safeTransform is invoked.

方法详细

_doCodeBlocks_callback() 方法
public string _doCodeBlocks_callback(array $matches)
$matches array matches
{return} string the highlighted code block
源码: framework/utils/CMarkdownParser.php#98 (显示)
public function _doCodeBlocks_callback($matches)
{
    
$codeblock $this->outdent($matches[1]);
    if((
$codeblock $this->highlightCodeBlock($codeblock)) !== null)
        return 
"\n\n".$this->hashBlock($codeblock)."\n\n";
    else
        return 
parent::_doCodeBlocks_callback($matches);
}

Callback function when a code block is matched.

_doFencedCodeBlocks_callback() 方法
public string _doFencedCodeBlocks_callback(array $matches)
$matches array matches
{return} string the highlighted code block
源码: framework/utils/CMarkdownParser.php#112 (显示)
public function _doFencedCodeBlocks_callback($matches)
{
    return 
"\n\n".$this->hashBlock($this->highlightCodeBlock($matches[2]))."\n\n";
}

Callback function when a fenced code block is matched.

createHighLighter() 方法
protected Text_Highlighter createHighLighter(string $options)
$options string the user-entered options
{return} Text_Highlighter the highlighter instance
源码: framework/utils/CMarkdownParser.php#153 (显示)
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;
}

Creates a highlighter instance.

getDefaultCssFile() 方法
public string getDefaultCssFile()
{return} string the default CSS file that is used to highlight code blocks.
源码: framework/utils/CMarkdownParser.php#88 (显示)
public function getDefaultCssFile()
{
    return 
Yii::getPathOfAlias('system.vendors.TextHighlighter.highlight').'.css';
}

getHighlightTag() 方法
protected string getHighlightTag(string $codeblock)
$codeblock string code block with highlighting options.
{return} string the user-entered highlighting options. Null if no option is entered.
源码: framework/utils/CMarkdownParser.php#141 (显示)
protected function getHighlightTag($codeblock)
{
    
$str trim(current(preg_split("/\r|\n/"$codeblock,2)));
    if(
strlen($str) > && $str[0] === '[' && $str[strlen($str)-1] === ']')
        return 
$str;
}

Returns the user-entered highlighting options.

getHiglightConfig() 方法
public array getHiglightConfig(string $options)
$options string user-entered options
{return} array the highlighter config
源码: framework/utils/CMarkdownParser.php#172 (显示)
public function getHiglightConfig($options)
{
    
$config['use_language'] = true;
    if( 
$this->getInlineOption('showLineNumbers'$optionsfalse) )
        
$config['numbers'] = HL_NUMBERS_LI;
    
$config['tabsize'] = $this->getInlineOption('tabSize'$options4);
    return 
$config;
}

Generates the config for the highlighter.

getInlineOption() 方法
protected mixed getInlineOption(string $name, string $str, mixed $defaultValue)
$name string the configuration name
$str string the user-entered options
$defaultValue mixed default value if the configuration is not present
{return} mixed the configuration value
源码: framework/utils/CMarkdownParser.php#188 (显示)
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;
}

Retrieves the specified configuration.

highlightCodeBlock() 方法
protected string highlightCodeBlock(string $codeblock)
$codeblock string the code block
{return} string the highlighted code block. Null if the code block does not need to highlighted
源码: framework/utils/CMarkdownParser.php#122 (显示)
protected function highlightCodeBlock($codeblock)
{
    if((
$tag=$this->getHighlightTag($codeblock))!==null && ($highlighter=$this->createHighLighter($tag)))
    {
        
$codeblock preg_replace('/\A\n+|\n+\z/'''$codeblock);
        
$tagLen strpos($codeblock$tag)+strlen($tag);
        
$codeblock ltrim(substr($codeblock$tagLen));
        
$output=preg_replace('/<span\s+[^>]*>(\s*)<\/span>/''\1'$highlighter->highlight($codeblock));
        return 
"<div class=\"{$this->highlightCssClass}\">".$output."</div>";
    }
    else
        return 
"<pre>".CHtml::encode($codeblock)."</pre>";
}

Highlights the code block.

safeTransform() 方法
public string safeTransform(string $content)
$content string the markdown content
{return} string the purified HTML content
源码: framework/utils/CMarkdownParser.php#77 (显示)
public function safeTransform($content)
{
    
$content=$this->transform($content);
    
$purifier=new HTMLPurifier($this->purifierOptions);
    
$purifier->config->set('Cache.SerializerPath',Yii::app()->getRuntimePath());
    return 
$purifier->purify($content);
}

Transforms the content and purifies the result. This method calls the transform() method to convert markdown content into HTML content. It then uses CHtmlPurifier to purify the HTML content to avoid XSS attacks.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.