mirror of
https://github.com/erusev/parsedown.git
synced 2025-01-17 12:48:28 +01:00
separate compiling of link / image
there are 2 types of input that produce link / image
This commit is contained in:
parent
4fecd91f7d
commit
68484504ca
@ -723,40 +723,30 @@ class Parsedown
|
|||||||
|
|
||||||
if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches))
|
if (strpos($text, ']') and preg_match('/\[((?:[^][]|(?R))*)\]/', $text, $matches))
|
||||||
{
|
{
|
||||||
$brackets_text = $matches[1];
|
$element = array(
|
||||||
|
'!' => $text[0] === '!',
|
||||||
|
'a' => $matches[1],
|
||||||
|
);
|
||||||
|
|
||||||
$offset = strlen($matches[0]);
|
$offset = strlen($matches[0]);
|
||||||
|
|
||||||
$element_is_image = $text[0] === '!' and $offset++;
|
$element['!'] and $offset++;
|
||||||
|
|
||||||
$remaining_text = substr($text, $offset);
|
$remaining_text = substr($text, $offset);
|
||||||
|
|
||||||
if ($remaining_text[0] === '(' and preg_match('/^\((.*?)\)/', $remaining_text, $matches))
|
if ($remaining_text[0] === '(' and preg_match('/^\((.*?)\)/', $remaining_text, $matches))
|
||||||
{
|
{
|
||||||
$element_url = $matches[1];
|
$element['»'] = $matches[1];
|
||||||
$element_url = str_replace('&', '&', $element_url);
|
|
||||||
$element_url = str_replace('<', '<', $element_url);
|
|
||||||
|
|
||||||
if ($element_is_image)
|
|
||||||
{
|
|
||||||
$markup .= '<img alt="'.$brackets_text.'" src="'.$element_url.'" />';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$element_text = $this->parse_span_elements($brackets_text, $markers);
|
|
||||||
|
|
||||||
$markup .= '<a href="'.$element_url.'">'.$element_text.'</a>';
|
|
||||||
}
|
|
||||||
|
|
||||||
$offset += strlen($matches[0]);
|
$offset += strlen($matches[0]);
|
||||||
}
|
}
|
||||||
elseif ($this->reference_map)
|
elseif ($this->reference_map)
|
||||||
{
|
{
|
||||||
$reference = $brackets_text;
|
$reference = $element['a'];
|
||||||
|
|
||||||
if (preg_match('/^\s*\[(.*?)\]/', $remaining_text, $matches))
|
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]);
|
$offset += strlen($matches[0]);
|
||||||
}
|
}
|
||||||
@ -765,36 +755,37 @@ class Parsedown
|
|||||||
|
|
||||||
if (isset($this->reference_map[$reference]))
|
if (isset($this->reference_map[$reference]))
|
||||||
{
|
{
|
||||||
$element_url = $this->reference_map[$reference];
|
$element['»'] = $this->reference_map[$reference];
|
||||||
|
|
||||||
$element_url = str_replace('&', '&', $element_url);
|
|
||||||
$element_url = str_replace('<', '<', $element_url);
|
|
||||||
|
|
||||||
if ($element_is_image)
|
|
||||||
{
|
|
||||||
$markup .= '<img alt="'.$brackets_text.'" src="'.$element_url.'" />';
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$element_text = $this->parse_span_elements($brackets_text, $markers);
|
|
||||||
|
|
||||||
$markup .= '<a href="'.$element_url.'">'.$element_text.'</a>';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$markup .= $closest_marker;
|
unset($element);
|
||||||
|
|
||||||
$offset = $element_is_image ? 2 : 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$markup .= $closest_marker;
|
unset($element);
|
||||||
|
|
||||||
$offset = $closest_marker === '![' ? 2 : 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($element))
|
||||||
|
{
|
||||||
|
$element['»'] = str_replace('&', '&', $element['»']);
|
||||||
|
$element['»'] = str_replace('<', '<', $element['»']);
|
||||||
|
|
||||||
|
if ($element['!'])
|
||||||
|
{
|
||||||
|
$markup .= '<img alt="'.$element['a'].'" src="'.$element['»'].'" />';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$element['a'] = $this->parse_span_elements($element['a'], $markers);
|
||||||
|
|
||||||
|
$markup .= '<a href="'.$element['»'].'">'.$element['a'].'</a>';
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($element);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$markup .= $closest_marker;
|
$markup .= $closest_marker;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user