<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Nette Framework forum : Uživatelé Nette Framework : Latte : nový tag do latte na českou typografii</title>
<link>https://forum.nette.org/cs</link>
<description>Nette Framework forum</description>
<language>cs</language>
<docs>http://backend.userland.com/rss</docs>
<item>
	<title>nový tag do latte na českou typografii (Latte)</title>
	<dc:creator>lubos</dc:creator>
	<link>https://forum.nette.org/cs/36821#p228508</link>
	<guid isPermaLink="false">228508@http://forum.nette.org/cs</guid>
	<description>&lt;p&gt;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 ?&lt;/p&gt;

&lt;p&gt;Koukám že texy má něco podobného: &lt;a
href=&quot;https://github.com/JanTvrdik/StaticWeb/blob/0369501f22497b76aadbcdb4925a2491c2ca84d1/StaticWebEngine/libs/Texy/modules/TexyTypographyModule.php&quot;&gt;link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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&lt;/p&gt;

&lt;pre
class=&quot;language-php&quot;&gt;&lt;code&gt;&amp;lt;?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 [
			&apos;typo&apos; =&amp;gt;  [Helpers::class, &apos;typography&apos;],
		];
	}
	public function getTags(): array {
		return [
			&apos;typo&apos; =&amp;gt; [TypographyNode::class, &apos;create&apos;],
		];
	}

}

class TypographyNode extends StatementNode {

	public AreaNode $content;
	public ModifierNode $modifier;

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

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

		[$node-&amp;gt;content] = yield;

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

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

        return $node;
    }

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

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



	public function &amp;amp;getIterator(): \Generator {
		yield $this-&amp;gt;content;
		yield $this-&amp;gt;modifier;
	}
}&lt;/code&gt;&lt;/pre&gt;
</description>
	<pubDate>Sat, 08 Mar 2025 21:04:33 +0100</pubDate>
</item>
</channel>
</rss>
