From 35b816c00269f195f7865a34c924c4bbc1110817 Mon Sep 17 00:00:00 2001 From: Fidel Remedios Rosado Date: Sun, 8 Feb 2026 14:36:05 -0500 Subject: [PATCH] Adding support to new patterns --- README.md | 4 ++++ src/index.php | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 158a2b6..ecf58b0 100644 --- a/README.md +++ b/README.md @@ -188,12 +188,16 @@ Create an `app.css` file with your Tailwind imports and `@source` directive: ```css @import "tailwindcss"; @source "./templates"; /* Directory to scan for classes */ + +@patterns "/->class\s*\(\s*[\"']([^\"']+)[\"']\s*\)/";/* New pattern declared */ + ``` The `@source` directive tells TailwindPHP where to find your template files. It supports: - Directories: `@source "./templates";` - Glob patterns: `@source "./src/**/*.php";` - Multiple sources: Add multiple `@source` directives +- Extended patterns: Add multiple `@patterns` directives with "new regex"; ### Examples diff --git a/src/index.php b/src/index.php index 1da8e72..bd270c5 100644 --- a/src/index.php +++ b/src/index.php @@ -60,6 +60,9 @@ /** @var Theme|null Cached default theme instance */ $_defaultThemeCache = null; +/* variable to store new patterns */ +$_customsPatterns = []; + /** * Virtual modules that should not be resolved from the filesystem. */ @@ -892,6 +895,39 @@ function parseCss(array &$ast, array $options = []): array return WalkAction::ReplaceSkip([]); } + // Handle @patterns + if ($node['name'] === '@patterns') { + if (!empty($node['nodes'])) { + throw new \Exception('`@patterns` cannot have a body.'); + } + + $patternsParam = $node['params']; + + // Permitir sin comillas envolventes + if ( + ($patternsParam[0] === '"' && $patternsParam[strlen($patternsParam) - 1] === '"') || + ($patternsParam[0] === "'" && $patternsParam[strlen($patternsParam) - 1] === "'") + ) { + $patternsParam = substr($patternsParam, 1, -1); + } + + // Dividir por comas y limpiar + $newPatterns = array_map('trim', explode(',', $patternsParam)); + $newPatterns = array_filter($newPatterns, 'strlen'); + + if (empty($newPatterns)) { + throw new \Exception('At least one pattern is required in @patterns directive.'); + } + + global $_customsPatterns; + $_customsPatterns = array_merge($_customsPatterns ?? [], $newPatterns); + + // Log para debugging + // fwrite(STDERR, 'customPatterns encontrados y guardados: ' . json_encode($_customsPatterns) . PHP_EOL); + + return WalkAction::ReplaceSkip([]); + } + // Handle @import - especially for 'tailwindcss' module if ($node['name'] === '@import') { $params = $node['params']; @@ -2107,8 +2143,13 @@ function extractCandidates(string $html): array { $candidates = []; + global $_customsPatterns; + $customPatterns = $_customsPatterns ?? []; + + $patterns = array_merge([REGEX_CLASS_ATTR, REGEX_CLASS_ATTR_PARENS, REGEX_CLASSNAME_ATTR], $customPatterns); + // Extract from class and className attributes - foreach ([REGEX_CLASS_ATTR, REGEX_CLASSNAME_ATTR] as $pattern) { + foreach ($patterns as $pattern) { if (preg_match_all($pattern, $html, $matches)) { foreach ($matches[1] as $classAttr) { foreach (preg_split(REGEX_WHITESPACE, $classAttr) as $class) {