Skip to content

Commit

Permalink
added bridge for Latte 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 17, 2022
1 parent 906ab00 commit f51e503
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.1
php-version: 8.0
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require-dev": {
"nette/tester": "^2.0",
"tracy/tracy": "^2.4",
"latte/latte": "^2.6",
"latte/latte": "^2.6 || ^3.0",
"phpstan/phpstan": "^0.12"
},
"replace": {
Expand Down
52 changes: 52 additions & 0 deletions src/Bridges/Latte/TexyExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* This file is part of the Texy! (https://texy.info)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Texy\Bridges\Latte;

use Latte;
use Latte\Compiler\Tag;
use Latte\Compiler\TemplateParser;
use Texy\Helpers;
use Texy\Texy;


/**
* Macro {texy} ... {/texy} for Latte v3
*/
class TexyExtension extends Latte\Extension
{
private $processor;


public function __construct(Texy|callable $texy)
{
$this->processor = $texy instanceof Texy
? function (string $text) use ($texy): string {
$text = Helpers::outdent(str_replace("\t", ' ', $text));
return $texy->process($text);
}
: $texy;
}


public function getTags(): array
{
return [
'texy' => fn(Tag $tag, TemplateParser $parser): \Generator => TexyNode::create($tag, $parser, $this->processor),
];
}


public function getProviders(): array
{
return [
'texy' => $this->processor,
];
}
}
2 changes: 1 addition & 1 deletion src/Bridges/Latte/TexyMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


/**
* Macro {texy} ... {/texy}
* Macro {texy} ... {/texy} for Latte v2
*/
class TexyMacro extends Latte\Macros\MacroSet
{
Expand Down
83 changes: 83 additions & 0 deletions src/Bridges/Latte/TexyNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* This file is part of the Texy! (https://texy.info)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Texy\Bridges\Latte;

use Latte;
use Latte\Compiler\NodeHelpers;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php\Expression\ArrayNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;
use Latte\Compiler\TemplateParser;


/**
* {texy} ... {/texy}
*/
class TexyNode extends StatementNode
{
public AreaNode $content;
public ArrayNode $args;


/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
public static function create(Tag $tag, TemplateParser $parser, callable $processor): \Generator
{
$node = new static;
$node->args = $tag->parser->parseArguments();

$saved = $parser->getContentType();
$parser->setContentType(Latte\ContentType::Text);
[$node->content] = yield;
$parser->setContentType($saved);

$text = NodeHelpers::toText($node->content);
if ($text !== null) {
try {
$text = $processor($text, ...NodeHelpers::toValue($node->args));
return new Latte\Compiler\Nodes\TextNode($text);
} catch (\Throwable) {
}
}

return $node;
}


public function print(PrintContext $context): string
{
$context->beginEscape()->enterContentType(Latte\ContentType::Text);
$res = $context->format(
<<<'XX'
ob_start(fn() => '') %line;
try {
%node
} finally {
$ʟ_tmp = ob_get_clean();
}
echo ($this->global->texy)($ʟ_tmp, ...%node);


XX,
$this->position,
$this->content,
$this->args,
);
$context->restoreEscape();
return $res;
}


public function &getIterator(): \Generator
{
yield $this->content;
}
}
44 changes: 44 additions & 0 deletions tests/Texy/latte2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '>')) {
Tester\Environment::skip('Test for Latte 2');
}


$texy = new Texy\Texy;

$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$macro = new Texy\Bridges\Latte\TexyMacro($latte, $texy);
$macro->install();

$template = <<<'XX'
{texy}
static
-----
{/texy}
XX;


Assert::match(
<<<'XX'
%A%
echo '<h1>static</h1>
';
%A%
XX
,
$latte->compile($template)
);


Assert::match(
'<h1>static</h1>',
$latte->renderToString($template)
);
56 changes: 56 additions & 0 deletions tests/Texy/latte3-custom-fn.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Test: TexyExtension
* @phpVersion 8.0
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}

$fn = function (string $text, int $heading = 1, string $locale = 'cs'): string {
$texy = new Texy\Texy;
$texy->headingModule->top = $heading;
$texy->typographyModule->locale = $locale;
return $texy->process($text);
};

$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new Texy\Bridges\Latte\TexyExtension($fn));

$template = <<<'XX'
{texy}
default
-----
{/texy}
{texy heading: 3}
"heading"
-----
{/texy}
{texy locale: en, heading: 3}
"both"
-----
{/texy}
XX;


Assert::match(
<<<'XX'
<h1>default</h1>
<h3>„heading“</h3>
<h3>“both”</h3>
XX,
$latte->renderToString($template),
);
80 changes: 80 additions & 0 deletions tests/Texy/latte3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* Test: TexyExtension
* @phpVersion 8.0
*/

declare(strict_types=1);

use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


$texy = new Texy\Texy;

$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new Texy\Bridges\Latte\TexyExtension($texy));

$template = <<<'XX'
{texy}
static
-----
<x n:attr>
{/texy}
{texy}
{='dynamic<>'}
-----
<x n:attr>
{/texy}
XX;


Assert::match(
<<<'XX'
%A%
echo '<h1>static</h1>
<p>&lt;x n:attr&gt;</p>
';
ob_start(fn() => '') /* line 8 */;
try {
echo 'dynamic<>' /* line 9 */;
echo '
-----
<x n:attr>
';
} finally {
$ʟ_tmp = ob_get_clean();
}
echo ($this->global->texy)($ʟ_tmp, ...[]);
%A%
XX,
$latte->compile($template),
);


Assert::match(
<<<'XX'
<h1>static</h1>
<p>&lt;x n:attr&gt;</p>
<h1>dynamic&lt;&gt;</h1>
<p>&lt;x n:attr&gt;</p>
XX,
$latte->renderToString($template),
);

0 comments on commit f51e503

Please sign in to comment.