From d818c59428605262dd0aadef1063e5fb7677db02 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 29 Oct 2023 10:33:30 +0100 Subject: [PATCH] Refactor ColoredBlockquotes class internals --- .../Processing/ColoredBlockquotes.php | 73 ++++++++----------- .../Processing/ShortcodeProcessor.php | 12 ++- .../ColoredBlockquoteShortcodesTest.php | 16 +--- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php index 5d455e043af..898c588b2f6 100644 --- a/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php +++ b/packages/framework/src/Markdown/Processing/ColoredBlockquotes.php @@ -7,21 +7,31 @@ use Hyde\Markdown\Contracts\MarkdownShortcodeContract; use Hyde\Markdown\Models\Markdown; -use function str_replace; +use function ltrim; +use function explode; use function sprintf; use function str_starts_with; -use function strlen; -use function substr; use function trim; /** - * @internal This class may be refactored to work with a single class instead of five, thus extending this class is discouraged. - * - * @todo See if we can extract an arbitrary class name from the signature. + * @internal This class may be refactored further, thus extending this class is discouraged. */ -abstract class ColoredBlockquotes implements MarkdownShortcodeContract +class ColoredBlockquotes implements MarkdownShortcodeContract { - protected static string $signature = '>color'; + /** @var string The core signature. We combine this with an additional check for color later. */ + protected static string $signature = '>'; + + protected static array $signatures = ['>danger', '>info', '>success', '>warning']; + + /** + * @internal + * + * @return string[] + */ + public static function getSignatures(): array + { + return self::$signatures; + } public static function signature(): string { @@ -30,49 +40,30 @@ public static function signature(): string public static function resolve(string $input): string { - return str_starts_with($input, static::signature()) + return self::stringStartsWithSignature($input) ? static::expand($input) : $input; } protected static function expand(string $input): string { - return sprintf( - '
%s
', - static::getClassNameFromSignature(static::signature()), - trim(Markdown::render(trim(substr($input, strlen(static::signature())), ' '))) + $parts = explode(' ', $input, 2); + $class = ltrim($parts[0], '>'); + $contents = trim($parts[1] ?? '', ' '); + + return sprintf('
%s
', + $class, trim(Markdown::render($contents)) ); } - protected static function getClassNameFromSignature(string $signature): string + protected static function stringStartsWithSignature(string $input): bool { - return str_replace('>', '', $signature); - } + foreach (static::$signatures as $signature) { + if (str_starts_with($input, $signature)) { + return true; + } + } - /** @return ColoredBlockquotes[] */ - public static function get(): array - { - return [ - /** @internal */ - new class extends ColoredBlockquotes - { - protected static string $signature = '>danger'; - }, - /** @internal */ - new class extends ColoredBlockquotes - { - protected static string $signature = '>info'; - }, - /** @internal */ - new class extends ColoredBlockquotes - { - protected static string $signature = '>success'; - }, - /** @internal */ - new class extends ColoredBlockquotes - { - protected static string $signature = '>warning'; - }, - ]; + return false; } } diff --git a/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php b/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php index c9cf4d3f6fd..2f28c91efcb 100644 --- a/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php +++ b/packages/framework/src/Markdown/Processing/ShortcodeProcessor.php @@ -97,9 +97,15 @@ public function addShortcode(MarkdownShortcodeContract $shortcode): void protected function discoverShortcodes(): void { - $this->addShortcodesFromArray( - ColoredBlockquotes::get() - ); + // Add the built-in shortcodes. + + foreach (ColoredBlockquotes::getSignatures() as $signature) { + $this->shortcodes[$signature] = new ColoredBlockquotes(); + } + + $this->addShortcodesFromArray([ + // + ]); } protected function getOutput(): string diff --git a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php index 1c51a6247d9..8f25cbdd3a0 100644 --- a/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php +++ b/packages/framework/tests/Feature/ColoredBlockquoteShortcodesTest.php @@ -14,27 +14,19 @@ */ class ColoredBlockquoteShortcodesTest extends UnitTestCase { - public function testGetMethod() - { - $this->assertCount(4, ColoredBlockquotes::get()); - $this->assertContainsOnlyInstancesOf(ColoredBlockquotes::class, - ColoredBlockquotes::get() - ); - } - public function testResolveMethod() { $this->assertSame( - '

foo

', - ColoredBlockquotes::resolve('>color foo') + '

foo

', + ColoredBlockquotes::resolve('>info foo') ); } public function testCanUseMarkdownWithinBlockquote() { $this->assertSame( - '

foo bar

', - ColoredBlockquotes::resolve('>color foo **bar**') + '

foo bar

', + ColoredBlockquotes::resolve('>info foo **bar**') ); } }