- Added an 'urlize' method to the UrlExtension class
This commit is contained in:
parent
14cfb46c98
commit
7e80d4be6d
|
@ -6,6 +6,14 @@ use Picea\Compiler\Context;
|
||||||
|
|
||||||
class UrlExtension implements Extension {
|
class UrlExtension implements Extension {
|
||||||
|
|
||||||
|
public const URLIZE_PATTERN_URL = <<<PATTERN
|
||||||
|
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
|
||||||
|
PATTERN;
|
||||||
|
|
||||||
|
public const URLIZE_PATTERN_EMAIL = <<<PATTERN
|
||||||
|
/(?:[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+\\/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])/
|
||||||
|
PATTERN;
|
||||||
|
|
||||||
protected string $urlBase;
|
protected string $urlBase;
|
||||||
|
|
||||||
protected string $assetToken;
|
protected string $assetToken;
|
||||||
|
@ -54,6 +62,7 @@ class UrlExtension implements Extension {
|
||||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
||||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
||||||
$context->pushFunction("slug", [ $this, 'slug' ]);
|
$context->pushFunction("slug", [ $this, 'slug' ]);
|
||||||
|
$context->pushFunction("urlize", [ $this, 'urlize' ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRouteList(bool $full = false) : array
|
public function getRouteList(bool $full = false) : array
|
||||||
|
@ -66,6 +75,17 @@ class UrlExtension implements Extension {
|
||||||
return $url . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
return $url . ( $parameters ? "?" . http_build_query($parameters) : "" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function urlize(string $string) : string
|
||||||
|
{
|
||||||
|
# Normal URL patterns
|
||||||
|
$string = preg_replace(static::URLIZE_PATTERN_URL, '<a href="$0" target="_blank" title="$0">$0</a>', $string);
|
||||||
|
|
||||||
|
# Email patterns
|
||||||
|
$string = preg_replace(static::URLIZE_PATTERN_EMAIL, '<a href="mailto:$0" title="$0">$0</a>', $string);
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
public function currentUrl(array $parameters = []) : string
|
public function currentUrl(array $parameters = []) : string
|
||||||
{
|
{
|
||||||
return $this->buildUrl($this->uri(), $parameters);
|
return $this->buildUrl($this->uri(), $parameters);
|
||||||
|
|
Loading…
Reference in New Issue