mirror of
https://github.com/erusev/parsedown.git
synced 2024-09-20 03:51:29 +02:00
syntax highlight() method added
This commit is contained in:
parent
bc30ad8e30
commit
011ae05fdc
@ -86,6 +86,44 @@ class Parsedown
|
|||||||
return $text;
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function highlight($text, $language = '')
|
||||||
|
{
|
||||||
|
if (is_array($text) and isset($text['text']))
|
||||||
|
{
|
||||||
|
if (isset($text['language']))
|
||||||
|
$language = $text['language'];
|
||||||
|
$text = $text['text'];
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($language)
|
||||||
|
{
|
||||||
|
case 'php':
|
||||||
|
if (strpos($text, '<'.'?') === false)
|
||||||
|
{
|
||||||
|
$text = '<'.'?php'."\n".$text;
|
||||||
|
$tag_added = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$markup = '<pre>'.highlight_string($text, true).'</pre>'."\n";
|
||||||
|
|
||||||
|
if (isset($tag_added))
|
||||||
|
{
|
||||||
|
$markup = preg_replace('@<\?php<br[ ]/?'.'>@', '', $markup);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
|
$markup = '<pre><code';
|
||||||
|
$markup .= ' class="language-'.$language.'"';
|
||||||
|
$markup .= '>'.$text.'</code></pre>'."\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $markup;
|
||||||
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Private
|
# Private
|
||||||
|
|
||||||
@ -670,25 +708,18 @@ class Parsedown
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'code':
|
case 'code':
|
||||||
|
|
||||||
$text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
|
|
||||||
|
|
||||||
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'fenced':
|
case 'fenced':
|
||||||
|
|
||||||
$text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
|
|
||||||
|
|
||||||
$markup .= '<pre><code';
|
|
||||||
|
|
||||||
if (isset($block['language']))
|
if (isset($block['language']))
|
||||||
{
|
{
|
||||||
$markup .= ' class="language-'.$block['language'].'"';
|
$markup .= $this->highlight($block);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$text = htmlspecialchars($block['text'], ENT_NOQUOTES, 'UTF-8');
|
||||||
|
|
||||||
$markup .= '>'.$text.'</code></pre>'."\n";
|
$markup .= '<pre><code>'.$text.'</code></pre>'."\n";
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user