CMarkdownParser

优质
小牛编辑
124浏览
2023-12-01
所有包 | 属性 | 方法
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:
  • official markdown syntax
  • markdown extra syntax

公共属性

隐藏继承属性

属性类型描述定义在
abbr_desciptionsMarkdownExtra_Parser
abbr_word_reMarkdownExtra_Parser
auto_close_tags_reMarkdownExtra_Parser
block_gamutMarkdown_Parser
block_tags_reMarkdownExtra_Parser
clean_tags_reMarkdownExtra_Parser
contain_span_tags_reMarkdownExtra_Parser
context_block_tags_reMarkdownExtra_Parser
document_gamutMarkdown_Parser
em_relistMarkdownExtra_Parser
em_strong_prepared_relistMarkdown_Parser
em_strong_relistMarkdownExtra_Parser
empty_element_suffixMarkdown_Parser
escape_charsMarkdown_Parser
escape_chars_reMarkdown_Parser
fn_backlink_classMarkdownExtra_Parser
fn_backlink_titleMarkdownExtra_Parser
fn_id_prefixMarkdownExtra_Parser
fn_link_classMarkdownExtra_Parser
fn_link_titleMarkdownExtra_Parser
footnote_counterMarkdownExtra_Parser
footnotesMarkdownExtra_Parser
footnotes_orderedMarkdownExtra_Parser
highlightCssClassstringthe css class for the div element containing the code block that is highlighted.CMarkdownParser
html_hashesMarkdown_Parser
in_anchorMarkdown_Parser
list_levelMarkdown_Parser
nested_brackets_depthMarkdown_Parser
nested_brackets_reMarkdown_Parser
nested_url_parenthesis_depthMarkdown_Parser
nested_url_parenthesis_reMarkdown_Parser
no_entitiesMarkdown_Parser
no_markupMarkdown_Parser
predef_abbrMarkdownExtra_Parser
predef_titlesMarkdown_Parser
predef_urlsMarkdown_Parser
purifierOptionsmixedthe options to be passed to {@link http://htmlpurifier.CMarkdownParser
span_gamutMarkdown_Parser
strong_relistMarkdownExtra_Parser
tab_widthMarkdown_Parser
titlesMarkdown_Parser
urlsMarkdown_Parser
utf8_strlenMarkdown_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
_doAutoLinks_email_callback()Markdown_Parser
_doAutoLinks_url_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
doAutoLinks()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.

参见

  • http://htmlpurifier.org/live/configdoc/plain.html

方法详细

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

Callback function when a code block is matched.

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

Callback function when a fenced code block is matched.

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

Creates a highlighter instance.

getDefaultCssFile() 方法
public string getDefaultCssFile()
{return}stringthe default CSS file that is used to highlight code blocks.
源码: framework/utils/CMarkdownParser.php#88 (显示) publicfunctiongetDefaultCssFile()
{
returnYii::getPathOfAlias('system.vendors.TextHighlighter.highlight').'.css';
}
getHighlightTag() 方法
protected string getHighlightTag(string $codeblock)
$codeblockstringcode block with highlighting options.
{return}stringthe user-entered highlighting options. Null if no option is entered.
源码: framework/utils/CMarkdownParser.php#141 (显示) protectedfunctiongetHighlightTag($codeblock)
{
$str=trim(current(preg_split("/r|n/",$codeblock,2)));
if(strlen($str)>2&&$str[0]==='['&&$str[strlen($str)-1]===']')
return$str;
}

Returns the user-entered highlighting options.

getHiglightConfig() 方法
public array getHiglightConfig(string $options)
$optionsstringuser-entered options
{return}arraythe highlighter config
源码: framework/utils/CMarkdownParser.php#172 (显示) publicfunctiongetHiglightConfig($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;
}

Generates the config for the highlighter.

getInlineOption() 方法
protected mixed getInlineOption(string $name, string $str, mixed $defaultValue)
$namestringthe configuration name
$strstringthe user-entered options
$defaultValuemixeddefault value if the configuration is not present
{return}mixedthe configuration value
源码: framework/utils/CMarkdownParser.php#188 (显示) protectedfunctiongetInlineOption($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)
$codeblockstringthe code block
{return}stringthe highlighted code block. Null if the code block does not need to highlighted
源码: framework/utils/CMarkdownParser.php#122 (显示) protectedfunctionhighlightCodeBlock($codeblock)
{
if(($tag=$this->getHighlightTag($codeblock))!==null&&($highlighter=$this->createHighLighter($tag)))
{
$codeblock=preg_replace('/An+|n+z/','',$codeblock);
$tagLen=strpos($codeblock,$tag)+strlen($tag);
$codeblock=ltrim(substr($codeblock,$tagLen));
$output=preg_replace('/<spans+[^>]*>(s*)</span>/','1',$highlighter->highlight($codeblock));
return"<divclass="{$this->highlightCssClass}">".$output."</div>";
}
else
return"<pre>".CHtml::encode($codeblock)."</pre>";
}

Highlights the code block.

safeTransform() 方法
public string safeTransform(string $content)
$contentstringthe markdown content
{return}stringthe purified HTML content
源码: framework/utils/CMarkdownParser.php#77 (显示) publicfunctionsafeTransform($content)
{
$content=$this->transform($content);
$purifier=newHTMLPurifier($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.