diff --git a/Parsedown.php b/Parsedown.php index 4df515e..35f48b1 100755 --- a/Parsedown.php +++ b/Parsedown.php @@ -723,40 +723,30 @@ class Parsedown if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches)) { - $brackets_text = $matches[1]; + $element = array( + '!' => $text[0] === '!', + 'a' => $matches[1], + ); $offset = strlen($matches[0]); - $element_is_image = $text[0] === '!' and $offset++; + $element['!'] and $offset++; $remaining_text = substr($text, $offset); if ($remaining_text[0] === '(' and preg_match('/^\((.*?)\)/', $remaining_text, $matches)) { - $element_url = $matches[1]; - $element_url = str_replace('&', '&', $element_url); - $element_url = str_replace('<', '<', $element_url); - - if ($element_is_image) - { - $markup .= ''.$brackets_text.''; - } - else - { - $element_text = $this->parse_span_elements($brackets_text, $markers); - - $markup .= ''.$element_text.''; - } + $element['»'] = $matches[1]; $offset += strlen($matches[0]); } elseif ($this->reference_map) { - $reference = $brackets_text; + $reference = $element['a']; if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches)) { - $reference = $matches[1] ? $matches[1] : $brackets_text; + $reference = $matches[1] ? $matches[1] : $element['a']; $offset += strlen($matches[0]); } @@ -765,36 +755,37 @@ class Parsedown if (isset($this->reference_map[$reference])) { - $element_url = $this->reference_map[$reference]; - - $element_url = str_replace('&', '&', $element_url); - $element_url = str_replace('<', '<', $element_url); - - if ($element_is_image) - { - $markup .= ''.$brackets_text.''; - } - else - { - $element_text = $this->parse_span_elements($brackets_text, $markers); - - $markup .= ''.$element_text.''; - } + $element['»'] = $this->reference_map[$reference]; } else { - $markup .= $closest_marker; - - $offset = $element_is_image ? 2 : 1; + unset($element); } } else { - $markup .= $closest_marker; - - $offset = $closest_marker === '![' ? 2 : 1; + unset($element); } } + + if (isset($element)) + { + $element['»'] = str_replace('&', '&', $element['»']); + $element['»'] = str_replace('<', '<', $element['»']); + + if ($element['!']) + { + $markup .= ''.$element['a'].''; + } + else + { + $element['a'] = $this->parse_span_elements($element['a'], $markers); + + $markup .= ''.$element['a'].''; + } + + unset($element); + } else { $markup .= $closest_marker;