Add uri parsing

This commit is contained in:
Dennis Eichhorn 2018-07-06 20:44:32 +02:00
parent c681d9697f
commit f6dd0571a0

View File

@ -15,6 +15,8 @@ declare(strict_types=1);
namespace phpOMS\Utils\Parser\Markdown; namespace phpOMS\Utils\Parser\Markdown;
use phpOMS\Uri\UriFactory;
/** /**
* Markdown parser class. * Markdown parser class.
* *
@ -769,7 +771,7 @@ class Markdown
} }
$data = [ $data = [
'url' => $matches[2], 'url' => UriFactory::build($matches[2]),
'title' => $matches[3] ?? null, 'title' => $matches[3] ?? null,
]; ];
@ -1049,7 +1051,7 @@ class Markdown
'name' => 'a', 'name' => 'a',
'text' => $matches[1], 'text' => $matches[1],
'attributes' => [ 'attributes' => [
'href' => $url, 'href' => UriFactory::build($url),
], ],
], ],
]; ];
@ -1140,7 +1142,7 @@ class Markdown
'element' => [ 'element' => [
'name' => 'img', 'name' => 'img',
'attributes' => [ 'attributes' => [
'src' => $link['element']['attributes']['href'], 'src' => UriFactory::build($link['element']['attributes']['href']),
'alt' => $link['element']['text'], 'alt' => $link['element']['text'],
], ],
], ],
@ -1186,7 +1188,7 @@ class Markdown
$remainder = \substr($remainder, $extent); $remainder = \substr($remainder, $extent);
if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches)) { if (\preg_match('/^[(]\s*+((?:[^ ()]++|[(][^ )]+[)])++)(?:[ ]+("[^"]*"|\'[^\']*\'))?\s*[)]/', $remainder, $matches)) {
$element['attributes']['href'] = $matches[1]; $element['attributes']['href'] = UriFactory::build($matches[1]);
if (isset($matches[2])) { if (isset($matches[2])) {
$element['attributes']['title'] = \substr($matches[2], 1, - 1); $element['attributes']['title'] = \substr($matches[2], 1, - 1);
@ -1209,7 +1211,7 @@ class Markdown
$def = self::$definitionData['Reference'][$definition]; $def = self::$definitionData['Reference'][$definition];
$element['attributes']['href'] = $def['url']; $element['attributes']['href'] = UriFactory::build($def['url']);
$element['attributes']['title'] = $def['title']; $element['attributes']['title'] = $def['title'];
} }
@ -1302,7 +1304,7 @@ class Markdown
'name' => 'a', 'name' => 'a',
'text' => $matches[0][0], 'text' => $matches[0][0],
'attributes' => [ 'attributes' => [
'href' => $matches[0][0], 'href' => UriFactory::build($matches[0][0]),
], ],
], ],
]; ];
@ -1329,7 +1331,7 @@ class Markdown
'name' => 'a', 'name' => 'a',
'text' => $matches[1], 'text' => $matches[1],
'attributes' => [ 'attributes' => [
'href' => $matches[1], 'href' => UriFactory::build($matches[1]),
], ],
], ],
]; ];
@ -1363,7 +1365,7 @@ class Markdown
*/ */
protected static function element(array $element) : string protected static function element(array $element) : string
{ {
$element = self::sanitizeElement($element); $element = self::sanitizeAndBuildElement($element);
$markup = '<' . $element['name']; $markup = '<' . $element['name'];
if (isset($element['attributes'])) { if (isset($element['attributes'])) {
@ -1442,7 +1444,7 @@ class Markdown
* *
* @since 1.0.0 * @since 1.0.0
*/ */
protected static function sanitizeElement(array $element) : array protected static function sanitizeAndBuildElement(array $element) : array
{ {
$safeUrlNameToAtt = [ $safeUrlNameToAtt = [
'a' => 'href', 'a' => 'href',