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 Conflicts: markdown.php
This commit is contained in:
parent
b503c58756
commit
b2c76a3804
1 changed files with 25 additions and 12 deletions
37
markdown.php
37
markdown.php
|
|
@ -2010,7 +2010,8 @@ class MarkdownExtra_Parser extends Markdown_Parser {
|
|||
|
|
||||
# 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
|
||||
|
|
@ -2082,7 +2083,7 @@ class MarkdownExtra_Parser extends Markdown_Parser {
|
|||
#
|
||||
# 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
|
||||
|
|
@ -2912,6 +2913,10 @@ class MarkdownExtra_Parser extends Markdown_Parser {
|
|||
# ~~~
|
||||
# Code block
|
||||
# ~~~
|
||||
#
|
||||
# ```
|
||||
# Code block
|
||||
# ```
|
||||
#
|
||||
$less_than_tab = $this->tab_width;
|
||||
|
||||
|
|
@ -2919,17 +2924,21 @@ class MarkdownExtra_Parser extends Markdown_Parser {
|
|||
(?:\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.
|
||||
|
|
@ -2945,19 +2954,23 @@ class MarkdownExtra_Parser extends Markdown_Parser {
|
|||
return $text;
|
||||
}
|
||||
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;
|
||||
|
|
@ -3331,4 +3344,4 @@ negligence or otherwise) arising in any way out of the use of this
|
|||
software, even if advised of the possibility of such damage.
|
||||
|
||||
*/
|
||||
?>
|
||||
?>
|
||||
|
|
|
|||
Loading…
Reference in a new issue