1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

simplify parsing of escaped characters

This commit is contained in:
Emanuil Rusev 2014-01-19 22:49:43 +02:00
parent 0182812d6c
commit 2b73e94c6c

View File

@ -51,7 +51,6 @@ class Parsedown
# #
private $reference_map = array(); private $reference_map = array();
private $escape_sequence_map = array();
# #
# Public Methods # Public Methods
@ -66,25 +65,6 @@ class Parsedown
# replaces tabs with spaces # replaces tabs with spaces
$text = str_replace("\t", ' ', $text); $text = str_replace("\t", ' ', $text);
# encodes escape sequences
if (strpos($text, '\\') !== FALSE)
{
$escape_sequences = array('\\\\', '\`', '\*', '\_', '\{', '\}', '\[', '\]', '\(', '\)', '\>', '\#', '\+', '\-', '\.', '\!');
foreach ($escape_sequences as $index => $escape_sequence)
{
if (strpos($text, $escape_sequence) !== FALSE)
{
$code = "\x1A".'\\'.$index.';';
$text = str_replace($escape_sequence, $code, $text);
$this->escape_sequence_map[$code] = $escape_sequence;
}
}
}
# ~ # ~
$text = trim($text, "\n"); $text = trim($text, "\n");
@ -93,15 +73,6 @@ class Parsedown
$text = $this->parse_block_elements($lines); $text = $this->parse_block_elements($lines);
# decodes escape sequences
foreach ($this->escape_sequence_map as $code => $escape_sequence)
{
$text = str_replace($code, $escape_sequence[1], $text);
}
# ~
$text = rtrim($text, "\n"); $text = rtrim($text, "\n");
return $text; return $text;
@ -612,8 +583,6 @@ class Parsedown
$text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8'); $text = htmlspecialchars($element['text'], ENT_NOQUOTES, 'UTF-8');
strpos($text, "\x1A\\") !== FALSE and $text = strtr($text, $this->escape_sequence_map);
$markup .= isset($element['language']) $markup .= isset($element['language'])
? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>' ? '<pre><code class="language-'.$element['language'].'">'.$text.'</code></pre>'
: '<pre><code>'.$text.'</code></pre>'; : '<pre><code>'.$text.'</code></pre>';
@ -673,9 +642,9 @@ class Parsedown
return $markup; return $markup;
} }
private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '_', '`', 'http', '~~')) private function parse_span_elements($text, $markers = array('![', '&', '*', '<', '[', '\\', '_', '`', 'http', '~~'))
{ {
if (isset($text[2]) === false or $markers === array()) if (isset($text[1]) === false or $markers === array())
{ {
return $text; return $text;
} }
@ -711,7 +680,7 @@ class Parsedown
# ~ # ~
if ($closest_marker === null or isset($text[$closest_marker_position + 2]) === false) if ($closest_marker === null or isset($text[$closest_marker_position + 1]) === false)
{ {
$markup .= $text; $markup .= $text;
@ -921,6 +890,23 @@ class Parsedown
break; break;
case '\\':
if (in_array($text[1], $this->special_characters))
{
$markup .= $text[1];
$offset = 2;
}
else
{
$markup .= '\\';
$offset = 1;
}
break;
case '`': case '`':
if (preg_match('/^`(.+?)`/', $text, $matches)) if (preg_match('/^`(.+?)`/', $text, $matches))
@ -928,11 +914,6 @@ class Parsedown
$element_text = $matches[1]; $element_text = $matches[1];
$element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8'); $element_text = htmlspecialchars($element_text, ENT_NOQUOTES, 'UTF-8');
if ($this->escape_sequence_map and strpos($element_text, "\x1A") !== false)
{
$element_text = strtr($element_text, $this->escape_sequence_map);
}
$markup .= '<code>'.$element_text.'</code>'; $markup .= '<code>'.$element_text.'</code>';
$offset = strlen($matches[0]); $offset = strlen($matches[0]);
@ -1011,6 +992,8 @@ class Parsedown
'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var', 'span', 'strong', 'sub', 'sup', 'textarea', 'tt', 'var',
); );
private $special_characters = array('\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!');
# ~ # ~
private $strong_regex = array( private $strong_regex = array(