From 966a8d20bf13258fb09fd19b2f890faca82217d4 Mon Sep 17 00:00:00 2001 From: James <78121730+itsnewtjam@users.noreply.github.com> Date: Tue, 30 Sep 2025 21:11:45 -0400 Subject: [PATCH] Fix/quark directive (#34) * better differentiate directive syntax * bump version --- composer.json | 2 +- src/Quark/QuarkCompiler.php | 4 ++-- tests/Unit/Quark/QuarkCompilerTest.php | 6 +++--- tests/Unit/Quark/QuarkEngineTest.php | 26 +++++++++++++------------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 26272e7..fa85f36 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "newtron/core", - "version": "0.1.4", + "version": "0.1.5", "type": "library", "description": "Core framework package for Newtron", "homepage": "https://github.com/newtron-framework/core", diff --git a/src/Quark/QuarkCompiler.php b/src/Quark/QuarkCompiler.php index df83be9..c229e90 100644 --- a/src/Quark/QuarkCompiler.php +++ b/src/Quark/QuarkCompiler.php @@ -126,7 +126,7 @@ private function registerBuiltinDirectives(): void { */ private function tokenize(string $source): array { $tokens = []; - $pattern = '/\{\{(.*?)\}\}|(~[a-zA-Z\-_]+\([^~]*\))/s'; + $pattern = '/\{\{(.*?)\}\}|\{~(.*?)~\}/s'; $lastPos = 0; preg_match_all($pattern, $source, $matches, PREG_OFFSET_CAPTURE); @@ -167,7 +167,7 @@ private function tokenize(string $source): array { * @throws \Exception If the directive syntax is invalid */ private function parseDirective(string $directive): array { - if (preg_match('/^~(\w+)\((.*)\)$/', $directive, $matches)) { + if (preg_match('/^(\w+)(?:\s+(.+))?$/', $directive, $matches)) { return [ 'type' => 'directive', 'name' => $matches[1], diff --git a/tests/Unit/Quark/QuarkCompilerTest.php b/tests/Unit/Quark/QuarkCompilerTest.php index 297cca7..5dbf5c3 100644 --- a/tests/Unit/Quark/QuarkCompilerTest.php +++ b/tests/Unit/Quark/QuarkCompilerTest.php @@ -52,7 +52,7 @@ public function testAddDirective(): void { $this->assertArrayHasKey('test', $directives); $this->assertEquals( "';\necho 'test_directive_ran';\necho '';\n", - $this->compiler->compile('
{{ test }}
'); + $this->createTestTemplate('layout', '{{ test }}
'); $this->assertEquals( 'Test Value
{{ test }}
'); $this->engine->setRootLayout('root'); @@ -66,7 +66,7 @@ public function testRenderTemplateWithRootLayout(): void { } public function testRenderTemplateWithSkipRootLayout(): void { - $this->createTestTemplate('root', '{{ test }}
'); $this->engine->setRootLayout('root'); $this->engine->skipRootLayout(); @@ -78,8 +78,8 @@ public function testRenderTemplateWithSkipRootLayout(): void { } public function testRenderTemplateWithSkipRootDirective(): void { - $this->createTestTemplate('root', '{{ test }}
'); + $this->createTestTemplate('root', '{{ test }}
'); $this->engine->setRootLayout('root'); $this->assertEquals( @@ -89,8 +89,8 @@ public function testRenderTemplateWithSkipRootDirective(): void { } public function testRenderTemplateWithCustomOutlet(): void { - $this->createTestTemplate('layout', '{{ test }}
~endslot()'); + $this->createTestTemplate('layout', '{{ test }}
{~ endslot ~}'); $this->assertEquals( 'Test Value
Included
'); $this->assertEquals( @@ -109,7 +109,7 @@ public function testRenderTemplateWithInclude(): void { } public function testRenderTemplateWithIf(): void { - $this->createTestTemplate('test', '{{ $item[\'name\'] }}
~endforeach(){{ $item[\'name\'] }}
{~ endforeach ~}