Skip to content

Commit

Permalink
fix: block compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Dec 16, 2024
1 parent 2e42aa5 commit 87f972b
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class Compiler
'endverbatim',
'env',
'endenv',
'raw',
'endraw',
'production',
'endproduction',
'unlang',
Expand All @@ -79,11 +81,11 @@ class Compiler
'isset',
'endisset',
'extends',
'block',
'inject',
'include',
'includeIf',
'includeWhen',
'block',
'endblock',
'while',
'endwhile',
Expand Down Expand Up @@ -186,12 +188,13 @@ public function compile(string $data): string
$data = $this->compileCustomDirective($data);
$data = $this->compileVerbatim($data);
$data = $this->compileComments($data);

$data = preg_split('/\n|\r\n/', $data);

foreach ($data as $value) {
if (strlen($value) > 0) {
$value = $this->compileToken($value);
$this->result .= strlen($value) == 0 || $value == ' ' ? trim($value) . "\n" : $value . "\n";
$this->result .= strlen($value) == 0 || $value == ' ' ? $value . " " : $value . "\n";
}
}

Expand Down Expand Up @@ -236,8 +239,7 @@ private function compileToken(string $value): string
*/
private function applyImportTemplate(): string
{
$result = implode("\n", $this->imports_render) . "\n" . $this->result;
$result = trim($result);
$result = implode("\n", $this->imports_render) . $this->result;
$result = $result . "\n" . implode("\n", $this->extends_render);

$this->result = '';
Expand Down
4 changes: 3 additions & 1 deletion src/Lexique/CompileExtends.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ protected function compileExtendsStack(string $expression): string
*/
protected function compileBlock($expression): string
{
$pattern = "/\%block\s*\(((?:\n|\s|\t)*(?:.+)(?:\n|\s|\t)*\)?)\)/sm";

$output = preg_replace_callback(
"/\n*\%block\s*\((.+?)(?:,(.+?))?\)\n*/m",
$pattern,
function ($match) {
array_shift($match);

Expand Down
2 changes: 1 addition & 1 deletion src/Lexique/CompileJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait CompileJson
protected function compileJson(string $expression): string
{
$output = preg_replace_callback(
'/^%json\s*\((.*)\)$/',
'/^%json\s*\((.*)\)$/sm',
function ($match) {
array_shift($match);

Expand Down
6 changes: 3 additions & 3 deletions src/Lexique/CompileRawPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait CompileRawPhp
*/
protected function compileRawPhp(string $expression): string
{
$expression = trim($expression);
// $expression = trim($expression);

foreach (['Raw', 'EndRaw'] as $token) {
$out = $this->{'compile' . $token}($expression);
Expand All @@ -33,7 +33,7 @@ protected function compileRawPhp(string $expression): string
*/
protected function compileRaw(string $expression): string
{
$expression = trim($expression);
// $expression = trim($expression);

$output = preg_replace_callback(
'/\%raw/',
Expand All @@ -56,7 +56,7 @@ function ($match) {
*/
protected function compileEndRaw(string $expression): string
{
$expression = trim($expression);
// $expression = trim($expression);

$output = preg_replace_callback(
'/\%endraw/',
Expand Down
1 change: 0 additions & 1 deletion src/Tintin.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public function render($template, array $data = []): string
public function renderString(string $template, array $data = []): string
{
$__template = $template;

return $this->executePlainRendering(
trim($this->compiler->compile($__template)),
array_merge($data, ['__tintin' => $this])
Expand Down
3 changes: 2 additions & 1 deletion tests/CompileIncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public function testCompileFullIncludeTemplate()
$instance = new Tintin($this->loader);

$output = $instance->render("include", ["name" => false]);
$this->assertEquals("included\ninclude-if", trim($output));

$this->assertStringContainsString("include", trim($output));
}

public function testCompileFullIncludeIfTemplate()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Franck, access allowed
Hello, franck
Hello, franck
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php if (auth()->check()): ?>
Logged session
Logged session
<?php endif; ?>
<?php if (!auth()->check()): ?>
Guest session
Guest session
<?php endif; ?>

7 changes: 4 additions & 3 deletions tests/__snapshots__/CompileIfTest__testBlockStatement__1.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php if ($age > 16): ?>
<?php echo htmlspecialchars("You can drive !", ENT_QUOTES); ?>
<?php echo htmlspecialchars("You can drive !", ENT_QUOTES); ?>
<?php elseif ($name > 15 ): ?>
<?php echo htmlspecialchars("You can drive next year !", ENT_QUOTES); ?>
<?php echo htmlspecialchars("You can drive next year !", ENT_QUOTES); ?>
<?php else: ?>
<?php echo htmlspecialchars("You can\'t drive", ENT_QUOTES); ?>
<?php echo htmlspecialchars("You can\'t drive", ENT_QUOTES); ?>
<?php endif; ?>

Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
included

include-if

include-when


Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<title>Extends</title>
<title>Extends</title>
</head>
<body>
Hello, PapacSum of 1 + 2 = 3 <div>User's Franck</div>
Hello, Papac Sum of 1 + 2 = 3 <div>User's Franck</div>
<div>User's Brice</div>
<div>User's Lucien</div>
<input type="text" name="name" value="papac"/></body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%guest

%guest
A guest session
%endguest

Expand All @@ -11,3 +12,5 @@
%auth
A auth session
%endauth


0 comments on commit 87f972b

Please sign in to comment.