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->compiler->compile('
{~ test ~}
') ); } @@ -68,7 +68,7 @@ public function testTokenizeSource(): void { ['type' => 'text', 'content' => ''], ['type' => 'directive', 'name' => 'outlet', 'args' => ''], ], - $method->invoke($this->compiler, '
{{ test }}
~outlet()') + $method->invoke($this->compiler, '
{{ test }}
{~ outlet ~}') ); } @@ -79,7 +79,7 @@ public function testParseDirective(): void { $this->assertEquals( ['type' => 'directive', 'name' => 'outlet', 'args' => 'custom'], - $method->invoke($this->compiler, '~outlet(custom)') + $method->invoke($this->compiler, 'outlet custom') ); } diff --git a/tests/Unit/Quark/QuarkEngineTest.php b/tests/Unit/Quark/QuarkEngineTest.php index 948bbcc..c939818 100644 --- a/tests/Unit/Quark/QuarkEngineTest.php +++ b/tests/Unit/Quark/QuarkEngineTest.php @@ -45,8 +45,8 @@ public function testRenderTemplate(): void { } public function testRenderTemplateWithLayout(): void { - $this->createTestTemplate('layout', '
~outlet()
'); - $this->createTestTemplate('test', '~layout(layout)

{{ test }}

'); + $this->createTestTemplate('layout', '
{~ outlet ~}
'); + $this->createTestTemplate('test', '{~ layout layout ~}

{{ test }}

'); $this->assertEquals( '

Test Value

', @@ -55,7 +55,7 @@ public function testRenderTemplateWithLayout(): void { } public function testRenderTemplateWithRootLayout(): void { - $this->createTestTemplate('root', '
~outlet()
'); + $this->createTestTemplate('root', '
{~ outlet ~}
'); $this->createTestTemplate('test', '

{{ test }}

'); $this->engine->setRootLayout('root'); @@ -66,7 +66,7 @@ public function testRenderTemplateWithRootLayout(): void { } public function testRenderTemplateWithSkipRootLayout(): void { - $this->createTestTemplate('root', '
~outlet()
'); + $this->createTestTemplate('root', '
{~ outlet ~}
'); $this->createTestTemplate('test', '

{{ test }}

'); $this->engine->setRootLayout('root'); $this->engine->skipRootLayout(); @@ -78,8 +78,8 @@ public function testRenderTemplateWithSkipRootLayout(): void { } public function testRenderTemplateWithSkipRootDirective(): void { - $this->createTestTemplate('root', '
~outlet()
'); - $this->createTestTemplate('test', '~skip_root()

{{ test }}

'); + $this->createTestTemplate('root', '
{~ outlet ~}
'); + $this->createTestTemplate('test', '{~ skip_root ~}

{{ test }}

'); $this->engine->setRootLayout('root'); $this->assertEquals( @@ -89,8 +89,8 @@ public function testRenderTemplateWithSkipRootDirective(): void { } public function testRenderTemplateWithCustomOutlet(): void { - $this->createTestTemplate('layout', '
~outlet(custom)
'); - $this->createTestTemplate('test', '~layout(layout)~slot(custom)

{{ test }}

~endslot()'); + $this->createTestTemplate('layout', '
{~ outlet custom ~}
'); + $this->createTestTemplate('test', '{~ layout layout ~}{~ slot custom ~}

{{ test }}

{~ endslot ~}'); $this->assertEquals( '

Test Value

', @@ -99,7 +99,7 @@ public function testRenderTemplateWithCustomOutlet(): void { } public function testRenderTemplateWithInclude(): void { - $this->createTestTemplate('test', '
~include(included)
'); + $this->createTestTemplate('test', '
{~ include included ~}
'); $this->createTestTemplate('included', '

Included

'); $this->assertEquals( @@ -109,7 +109,7 @@ public function testRenderTemplateWithInclude(): void { } public function testRenderTemplateWithIf(): void { - $this->createTestTemplate('test', '
~if($test)True~endif()
'); + $this->createTestTemplate('test', '
{~ if $test ~}True{~ endif ~}
'); $this->assertEquals( '
', @@ -124,7 +124,7 @@ public function testRenderTemplateWithIf(): void { public function testRenderTemplateWithIfWithElses(): void { $this->createTestTemplate( 'test', - '
~if($test == 1)One~elseif($test == 2)Two~else()None~endif()
' + '
{~ if $test == 1 ~}One{~ elseif $test == 2 ~}Two{~ else ~}None{~ endif ~}
' ); $this->assertEquals( @@ -144,7 +144,7 @@ public function testRenderTemplateWithIfWithElses(): void { public function testRenderTemplateWithForeach(): void { $this->createTestTemplate( 'test', - '
~foreach($test as $item)

{{ $item[\'name\'] }}

~endforeach()
' + '
{~ foreach $test as $item ~}

{{ $item[\'name\'] }}

{~ endforeach ~}
' ); $this->assertEquals( @@ -160,7 +160,7 @@ public function testRenderTemplateWithForeach(): void { public function testRenderTemplateWithSet(): void { $this->createTestTemplate( 'test', - '
~if($test)~set($text = \'new_value\')~endif(){{ text }}
' + '
{~ if $test ~}{~ set $text = \'new_value\' ~}{~ endif ~}{{ text }}
' ); $this->assertEquals(