Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 42 additions & 1 deletion src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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) {
Expand Down