Skip to content

Commit

Permalink
Merge 82d697a into 8860c5b
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk committed Jul 31, 2022
2 parents 8860c5b + 82d697a commit d541527
Show file tree
Hide file tree
Showing 33 changed files with 445 additions and 474 deletions.
1 change: 0 additions & 1 deletion .github/dependabot.yml
Expand Up @@ -16,6 +16,5 @@ updates:
- dependency-name: "nette/di"
- dependency-name: "nette/schema"
- dependency-name: "latte/latte"
- dependency-name: "nepada/template-factory"
- dependency-name: "nette/bootstrap"
- dependency-name: "nette/application"
22 changes: 15 additions & 7 deletions README.md
Expand Up @@ -23,15 +23,13 @@ extensions:
texy: Nepada\Bridges\TexyDI\TexyExtension
```

This extension relies on [nepada/template-factory](https://github.com/nepada/template-factory) - make sure you've enabled it as well.


Usage
-----

### Configuration

This extension contains simple Texy factory that only creates new instance of `Texy\Texy` and sets output mode to `HTML5`.
This extension contains simple Texy factory that only creates new instance of `Texy\Texy`.

Usually you will want to define your own factory by implementing `Nepada\Texy\TexyFactory`, often more than one.

Expand All @@ -48,15 +46,25 @@ This example adds two custom factories. Note the names `foo` and `bar` - we call

### In templates

Latte templates come with a couple of filters that you can use:
There are 2 new tags for processing blocks and single lines, both with possibility to specify a custom mode:

```late
{texy fooMode}
- one
- two
{/texy}
<p>
{texyLine barMode}Whatever...{/texyLine}
</p>
```

Alternatively, you can use one of 3 filters to achieve similar result:

- `|texy:customMode` calls `$texyMultiplier->processBlock()`
- `|texyLine:customMode` calls `$texyMultiplier->processLine()`
- `|texyTypo:customMode` calls `$texyMultiplier->processTypo()`

You can pass the name of custom mode as an argument to each of the filters.

**Deprecated:** Alternatively, you can wrap your code into macro `{texyMode modeName}...{/texyMode}`, which changes the default mode for all calls inside.

### In presenters and other controls

Expand Down
14 changes: 6 additions & 8 deletions composer.json
Expand Up @@ -6,7 +6,7 @@
"keywords": ["nette", "latte", "texy"],
"support": {
"email": "petr@pada.cz",
"issues": "https://github.com/nepada/template-factory/issues"
"issues": "https://github.com/nepada/texy-nette/issues"
},
"authors": [
{
Expand All @@ -17,7 +17,7 @@
"require": {
"php": ">=8.0.0 <8.2",
"nette/utils": "^3.2@dev",
"texy/texy": "^3.1.5@dev"
"texy/texy": "^3.1.6@dev"
},
"require-dev": {
"nette/tester": "2.4.2",
Expand All @@ -31,18 +31,16 @@
"nepada/coding-standard": "7.5.1",
"nette/di": "^3.0.6@dev",
"nette/schema": "^1.0.3@dev",
"latte/latte": "^2.7@dev",
"nepada/template-factory": "^2.2.0@dev",
"latte/latte": "^3.0@dev",
"nette/bootstrap": ">=3.0@dev",
"nette/application": ">=3.1.4"
},
"conflict": {
"latte/latte": ">=3.0.0"
"latte/latte": "<3.0.0"
},
"suggest": {
"nette/di": "for integration with Nette DI container",
"latte/latte": "to use Latte with custom Texy! macros in templates",
"nepada/template-factory": "to automatically inject required services into Latte templates"
"latte/latte": "to use Latte with custom Texy! macros in templates"
},
"autoload": {
"psr-4": {
Expand All @@ -63,7 +61,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.3-dev"
"dev-master": "5.0-dev"
}
}
}
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Expand Up @@ -24,3 +24,9 @@ parameters:
- LogicException
- Nette\InvalidStateException
- Nette\MemberAccessException

ignoreErrors:
-
message: "#^Parameter \\#1 \\$value of method Generator\\<int,array\\|null,array\\<int, Latte\\\\Compiler\\\\Nodes\\\\AreaNode\\|Latte\\\\Compiler\\\\Tag\\|null\\>,Nepada\\\\Bridges\\\\TexyLatte\\\\TexyNode\\>\\:\\:send\\(\\) expects array\\{Latte\\\\Compiler\\\\Nodes\\\\AreaNode, Latte\\\\Compiler\\\\Tag\\|null\\}, mixed given\\.$#"
count: 1
path: src/Bridges/TexyLatte/TexyNode.php
46 changes: 16 additions & 30 deletions src/Bridges/TexyDI/TexyExtension.php
Expand Up @@ -3,13 +3,17 @@

namespace Nepada\Bridges\TexyDI;

use Latte;
use Nepada;
use Nepada\Texy;
use Nette;
use Nette\Bridges\ApplicationDI\LatteExtension;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;

class TexyExtension extends Nette\DI\CompilerExtension
/**
* @property \stdClass $config
*/
final class TexyExtension extends Nette\DI\CompilerExtension
{

private const DEFAULT_MODE = 'default';
Expand All @@ -27,52 +31,34 @@ public function getConfigSchema(): Nette\Schema\Schema
public function loadConfiguration(): void
{
$container = $this->getContainerBuilder();
$config = $this->getConfig();
assert($config instanceof \stdClass);

$container->addDefinition($this->prefix('texyFactory'), new ServiceDefinition())
->setType(Texy\DefaultTexyFactory::class);

$container->addDefinition($this->prefix('multiplier'), new ServiceDefinition())
->setType(Texy\TexyMultiplier::class)
->setFactory(Texy\TexyMultiplier::class, [$config->defaultMode]);

$container->addDefinition($this->prefix('latteFilters'), new ServiceDefinition())
->setType(Nepada\Bridges\TexyLatte\TexyFilters::class);
->setFactory(Texy\TexyMultiplier::class, [$this->config->defaultMode]);
}

public function beforeCompile(): void
{
$container = $this->getContainerBuilder();
$config = $this->getConfig();
assert($config instanceof \stdClass);

$multiplier = $container->getDefinition($this->prefix('multiplier'));
assert($multiplier instanceof ServiceDefinition);
foreach ($config->factories as $name => $factory) {
foreach ($this->config->factories as $name => $factory) {
$multiplier->addSetup('addFactory', [$name, $factory]);
}

if (! class_exists(Latte\Engine::class)) {
return;
}
/** @var Nette\Bridges\ApplicationDI\LatteExtension[] $latteExtension */
$latteExtension = $this->compiler->getExtensions(Nette\Bridges\ApplicationDI\LatteExtension::class);
if ($latteExtension === []) {
throw new \LogicException('LatteExtension not found, did you register it in your configuration?');
}
reset($latteExtension)->addMacro(Nepada\Bridges\TexyLatte\TexyModeMacro::class . '::install');

if (! class_exists(Nepada\TemplateFactory\TemplateConfigurator::class)) {
return;
/** @var LatteExtension $latteExtension */
foreach ($this->compiler->getExtensions(LatteExtension::class) as $latteExtension) {
$latteExtension->addExtension(new Statement(
Nepada\Bridges\TexyLatte\TexyLatteExtension::class,
[
'texyMultiplier' => $multiplier,
],
));
}
$templateConfigurator = $container->getDefinitionByType(Nepada\TemplateFactory\TemplateConfigurator::class);
assert($templateConfigurator instanceof ServiceDefinition);
$templateConfigurator->addSetup('addFilter', ['texy', [$this->prefix('@latteFilters'), 'process']])
->addSetup('addFilter', ['texyLine', [$this->prefix('@latteFilters'), 'processLine']])
->addSetup('addFilter', ['texyTypo', [$this->prefix('@latteFilters'), 'processTypo']])
->addSetup('addProvider', ['texy', $this->prefix('@multiplier')])
->addSetup('addParameter', ['texy', $this->prefix('@multiplier')]);
}

}
66 changes: 0 additions & 66 deletions src/Bridges/TexyLatte/TexyFilters.php

This file was deleted.

91 changes: 91 additions & 0 deletions src/Bridges/TexyLatte/TexyLatteExtension.php
@@ -0,0 +1,91 @@
<?php
declare(strict_types = 1);

namespace Nepada\Bridges\TexyLatte;

use Latte\Compiler\Tag;
use Latte\Compiler\TemplateParser;
use Latte\ContentType;
use Latte\Extension;
use Latte\Runtime\FilterInfo;
use Nepada\Texy\TexyMultiplier;
use Texy\Helpers;

final class TexyLatteExtension extends Extension
{

private TexyMultiplier $texyMultiplier;

public function __construct(TexyMultiplier $texyMultiplier)
{
$this->texyMultiplier = $texyMultiplier;
}

/**
* @return array<string, callable(Tag, TemplateParser): (\Generator|void)|\stdClass>
*/
public function getTags(): array
{
return [
'texy' => fn (Tag $tag, TemplateParser $parser) => yield from TexyNode::create($tag, $parser, [$this, 'processBlock']),
'texyLine' => fn (Tag $tag, TemplateParser $parser) => yield from TexyNode::create($tag, $parser, [$this->texyMultiplier, 'processLine']),
];
}

/**
* @return array<string, callable>
*/
public function getFilters(): array
{
return [
'texy' => [$this, 'texyFilter'],
'texyLine' => [$this, 'texyLineFilter'],
'texyTypo' => [$this, 'texyTypoFilter'],
];
}

/**
* @return array<string, mixed>
*/
public function getProviders(): array
{
return [
'texy' => fn (string $text, string $tag, ?string $mode = null): string => match ($tag) {
'texy' => $this->processBlock($text, $mode),
'texyLine' => $this->texyMultiplier->processLine($text, $mode),
default => throw new \InvalidArgumentException("Unsupported texy tag '{$tag}'"),
},
];
}

public function texyFilter(FilterInfo $filterInfo, string $text, ?string $mode = null): string
{
if (! in_array($filterInfo->contentType, [null, ContentType::Text, ContentType::Html], true)) {
trigger_error('Filter |texy used with incompatible type ' . strtoupper($filterInfo->contentType), E_USER_WARNING);
}
$filterInfo->contentType = ContentType::Html;
return $this->processBlock($text, $mode);
}

public function texyLineFilter(FilterInfo $filterInfo, string $text, ?string $mode = null): string
{
if (! in_array($filterInfo->contentType, [null, ContentType::Text, ContentType::Html], true)) {
trigger_error('Filter |texyLine used with incompatible type ' . strtoupper($filterInfo->contentType), E_USER_WARNING);
}
$filterInfo->contentType = ContentType::Html;
return $this->texyMultiplier->processLine($text, $mode);
}

public function texyTypoFilter(FilterInfo $filterInfo, string $text, ?string $mode = null): string
{
$filterInfo->validate([null, ContentType::Text], 'texyTypo');
return $this->texyMultiplier->processTypo($text, $mode);
}

public function processBlock(string $text, ?string $mode = null): string
{
$text = Helpers::outdent(str_replace("\t", ' ', $text));
return $this->texyMultiplier->processBlock($text, $mode);
}

}

0 comments on commit d541527

Please sign in to comment.