GFM Enhancements (Backticks)

- Adding support for GFM backticks ``` (for fenced code blocks); in additional to ~~~ already supported in Markdown Extra.
- Adding support for GFM language marker; e.g. ```php
This commit is contained in:
Jason Caldwell 2013-09-23 03:59:54 -07:00
parent 9dbd8352de
commit 2383db7cd6

View file

@ -1846,7 +1846,8 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
|
# Fenced code block marker
(?<= ^ | \n )
[ ]{0,'.($indent+3).'}~{3,}
[ ]{0,'.($indent+3).'}[`~]{3,}
[A-Za-z0-9_\-]* # Language (optional).
[ ]*
(?:
\.?[-_:a-zA-Z0-9]+ # standalone class name
@ -1918,7 +1919,7 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
#
# Check for: Fenced code block marker.
#
else if (preg_match('{^\n?([ ]{0,'.($indent+3).'})(~+)}', $tag, $capture)) {
else if (preg_match('{^\n?([ ]{0,'.($indent+3).'})([`~]+)}', $tag, $capture)) {
# Fenced code block marker: find matching end marker.
$fence_indent = strlen($capture[1]); # use captured indent in re
$fence_re = $capture[2]; # use captured fence in re
@ -2760,6 +2761,10 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
# ~~~
# Code block
# ~~~
#
# ```
# Code block
# ```
#
$less_than_tab = $this->tab_width;
@ -2767,17 +2772,21 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
(?:\n|\A)
# 1: Opening marker
(
~{3,} # Marker: three tilde or more.
[`~]{3,} # 3 or more backticks/tildes.
)
# 2: Code language marker (optional).
(
[A-Za-z0-9_\-]* # Word chars and/or `-`.
)
[ ]*
(?:
\.?([-_:a-zA-Z0-9]+) # 2: standalone class name
\.?([-_:a-zA-Z0-9]+) # 3: Stand-alone class name.
|
'.$this->id_class_attr_catch_re.' # 3: Extra attributes
'.$this->id_class_attr_catch_re.' # 4: Extra attributes.
)?
[ ]* \n # Whitespace and newline following marker.
# 4: Content
# 5: Content
(
(?>
(?!\1 [ ]* \n) # Not a closing marker.
@ -2793,19 +2802,23 @@ class _MarkdownExtra_TmpImpl extends \Michelf\Markdown {
return $text;
}
protected function _doFencedCodeBlocks_callback($matches) {
$classname =& $matches[2];
$attrs =& $matches[3];
$codeblock = $matches[4];
$language = $matches[2];
$classname = $matches[3];
$attrs = $matches[4];
$codeblock = $matches[5];
$codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES);
$codeblock = preg_replace_callback('/^\n+/',
array(&$this, '_doFencedCodeBlocks_newlines'), $codeblock);
$attr_str = ''; # Initialize string of attributes.
if($language != '') $attr_str .= ' lang="'.$language.'"';
if ($classname != "") {
if ($classname{0} == '.')
$classname = substr($classname, 1);
$attr_str = ' class="'.$this->code_class_prefix.$classname.'"';
$attr_str .= ' class="'.$this->code_class_prefix.$classname.'"';
} else {
$attr_str = $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs);
$attr_str .= $this->doExtraAttributes($this->code_attr_on_pre ? "pre" : "code", $attrs);
}
$pre_attr_str = $this->code_attr_on_pre ? $attr_str : '';
$code_attr_str = $this->code_attr_on_pre ? '' : $attr_str;