This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -84,7 +84,14 @@ class KirbyTag
public static function parse(string $string, array $data = [], array $options = [])
{
// remove the brackets, extract the first attribute (the tag type)
$tag = trim(rtrim(ltrim($string, '('), ')'));
$tag = trim(ltrim($string, '('));
// use substr instead of rtrim to keep non-tagged brackets
// (link: file.pdf text: Download (PDF))
if (substr($tag, -1) === ')') {
$tag = substr($tag, 0, -1);
}
$type = trim(substr($tag, 0, strpos($tag, ':')));
$type = strtolower($type);
$attr = static::$types[$type]['attr'] ?? [];

View File

@@ -3,6 +3,8 @@
namespace Kirby\Text;
use Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Str;
/**
* Parses and converts custom kirbytags in any
@@ -22,10 +24,31 @@ class KirbyTags
public static function parse(string $text = null, array $data = [], array $options = []): string
{
return preg_replace_callback('!(?=[^\]])\([a-z0-9_-]+:.*?\)!is', function ($match) use ($data, $options) {
$regex = '!
(?=[^\]]) # positive lookahead that matches a group after the main expression without including ] in the result
(?=\([a-z0-9_-]+:) # positive lookahead that requires starts with ( and lowercase ASCII letters, digits, underscores or hyphens followed with : immediately to the right of the current location
(\( # capturing group 1
(?:[^()]+|(?1))*+ # repetitions of any chars other than ( and ) or the whole group 1 pattern (recursed)
\)) # end of capturing group 1
!isx';
return preg_replace_callback($regex, function ($match) use ($data, $options) {
$debug = $options['debug'] ?? false;
try {
return static::$tagClass::parse($match[0], $data, $options)->render();
} catch (InvalidArgumentException $e) {
// stay silent in production and ignore non-existing tags
if ($debug !== true || Str::startsWith($e->getMessage(), 'Undefined tag type:') === true) {
return $match[0];
}
throw $e;
} catch (Exception $e) {
if ($debug === true) {
throw $e;
}
return $match[0];
}
}, $text);

View File

@@ -56,11 +56,11 @@ class Markdown
/**
* Parses the given text and returns the HTML
*
* @param string $text
* @param string|null $text
* @param bool $inline
* @return string
*/
public function parse(string $text, bool $inline = false): string
public function parse(string $text = null, bool $inline = false): string
{
if ($this->options['extra'] === true) {
$parser = new ParsedownExtra();

View File

@@ -115,10 +115,10 @@ class SmartyPants
/**
* Parses the given text
*
* @param string $text
* @param string|null $text
* @return string
*/
public function parse(string $text): string
public function parse(string $text = null): string
{
// prepare the text
$text = str_replace('"', '"', $text);