-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
321 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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><x n:attr></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><x n:attr></p> | ||
<h1>dynamic<></h1> | ||
<p><x n:attr></p> | ||
XX, | ||
$latte->renderToString($template), | ||
); |