nový tag do latte na českou typografii

lubos
Člen | 23
+
0
-

Zkoušel jsem udělat nový tag do latte, aby mi upravoval jednoznakoví předložky, protože zrovna tady na to jsem blbý jako tágo, používal jsem kombinace AI, ale nejsem si jistý co jsem to s nimi vyplodil. Helpers::typography() je jen hromada regulárů, které upravují vstupní text a vyflusávají upravený text. Je tu někdo kdo si troufne to projít ?

Koukám že texy má něco podobného: link

Rád bych to vlastně nějak namontovat do latte, aby to fungovalo “bez dohledu” (bez tagů) rovnou na všechny texty, které se tím proženou, ale to buch musel hodně máknout na Helpers::typography(), protože občas něco z html požere :D

<?php
declare(strict_types=1);

use Nette\Utils\Strings;
use Nette\Utils\Html;

use Latte\Compiler\NodeHelpers;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\NopNode;
use Latte\Compiler\Nodes\Php;
use Latte\Compiler\Nodes\Php\ModifierNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\Nodes\TextNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;
use Latte\Essential\TranslatorExtension;

final class LatteExtension extends Latte\Extension {

	public function getFilters(): array {
		return [
			'typo' =>  [Helpers::class, 'typography'],
		];
	}
	public function getTags(): array {
		return [
			'typo' => [TypographyNode::class, 'create'],
		];
	}

}

class TypographyNode extends StatementNode {

	public AreaNode $content;
	public ModifierNode $modifier;

	public static function create(Tag $tag): \Generator{
		$tag->outputMode = $tag::OutputKeepIndentation;

		$node = $tag->node = new static;
		$args = $tag->parser->parseArguments();
		$node->modifier = $tag->parser->parseModifier();
		$node->modifier->escape = true;
		if ($tag->void) {
			return new NopNode;
		}

		[$node->content] = yield;

		 // Rekurzivní funkce pro zpracování všech textových uzlů bez rozbití HTML značek
		$processNode = static function($n) use (&$processNode) {
			if ($n instanceof TextNode) {
				$n->content = Helpers::typography($n->content)->getHtml();
			} elseif ($n instanceof AreaNode && property_exists($n, 'children')) {
				foreach ($n->children as $child) {
					$processNode($child);
				}
			}
		};

		if ($node->content instanceof AreaNode) {
			$processNode($node->content);
		} elseif ($node->content instanceof TextNode) {
			$node->content->content = Helpers::typography($node->content->content)->getHtml();
		}

        return $node;
    }

	public function print(PrintContext $context): string {
		if ($this->content instanceof TextNode) {
			return $context->format(
				<<<'XX'
					$ʟ_fi = new LR\FilterInfo(%dump);
					echo %modifyContent(%dump) %line;
					XX,
				$context->getEscaper()->export(),
				$this->modifier,
				$this->content->content,
				$this->position,
			);

		} else {
			return $context->format(
				<<<'XX'
					ob_start(fn() => ''); try {
						%node
					} finally {
						$ʟ_tmp = ob_get_clean();
					}
					$ʟ_fi = new LR\FilterInfo(%dump);
					echo %modifyContent($ʟ_tmp) %line;
					XX,
				$this->content,
				$context->getEscaper()->export(),
				$this->modifier,
				$this->position,
			);
		}
	}



	public function &getIterator(): \Generator {
		yield $this->content;
		yield $this->modifier;
	}
}