Package | system.utils |
---|---|
Inheritance | class CMarkdownParser » MarkdownExtra_Parser » Markdown_Parser |
Since | 1.0 |
Source Code | framework/utils/CMarkdownParser.php |
[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
[php showLineNumbers=1]which will show line numbers in each line of the code block.
Property | Type | Description | Defined By |
---|---|---|---|
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_backlink_class | MarkdownExtra_Parser | ||
fn_backlink_title | MarkdownExtra_Parser | ||
fn_id_prefix | MarkdownExtra_Parser | ||
fn_link_class | MarkdownExtra_Parser | ||
fn_link_title | 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 |
Method | Description | Defined By |
---|---|---|
__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 | |
_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() | Returns the default CSS file that is used to highlight code blocks. | CMarkdownParser |
getHighlightConfig() | Generates the config for the highlighter. | 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 |
Method | Description | Defined By |
---|---|---|
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 |
utf8_strlen() | Markdown_Parser |
the css class for the div element containing the code block that is highlighted. Defaults to 'hl-code'.
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.
public string _doCodeBlocks_callback(array $matches)
| ||
$matches | array | matches |
{return} | string | the highlighted code block |
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.
public string _doFencedCodeBlocks_callback(array $matches)
| ||
$matches | array | matches |
{return} | string | the highlighted code block |
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.
protected Text_Highlighter createHighLighter(string $options)
| ||
$options | 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->getHighlightConfig($options)));
return $highlighter;
}
Creates a highlighter instance.
public string getDefaultCssFile()
| ||
{return} | string | the default CSS file that is used to highlight code blocks. |
public function getDefaultCssFile()
{
return Yii::getPathOfAlias('system.vendors.TextHighlighter.highlight').'.css';
}
public array getHighlightConfig(string $options)
| ||
$options | string | 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.
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. |
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;
}
Returns the user-entered highlighting options.
public array getHiglightConfig(string $options)
| ||
$options | string | user-entered options |
{return} | array | the highlighter config |
public function getHiglightConfig($options)
{
return $this->getHighlightConfig($options);
}
Generates the config for the highlighter.
NOTE: This method is deprecated due to a mistake in the method name.
Use getHighlightConfig instead of this.
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 |
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.
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 |
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.
public string safeTransform(string $content)
| ||
$content | string | the markdown content |
{return} | string | the purified HTML content |
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.
CMarkdownParser usage examples
You can use this class directly in your code:
$parser = new CMarkdownParser; $contents = $parser->safeTransform($body);
Differences between CMarkdown and CMarkdownParser are quite simple. CMarkdown is a wrapper with an additional option (purifyOutput) and it can be used as widget.
Signup or Login in order to comment.