diff --git a/.travis.yml b/.travis.yml index e8df46d..55a10af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 7.0 - 7.1 - 7.2 + - 8.4 env: matrix: diff --git a/README.md b/README.md index 0e6c34a..2cfaba8 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,7 @@ This tool can **convert Your CSS framework (currently Bootstrap) classes** in HT - [Introduction](docs/1_introduction.md) - [Installation](docs/2_installation.md) - [Quick start](docs/3_quick-start.md) + + +chmod +x tailwindo +sudo ln -s $(pwd)/tailwindo /usr/local/bin/tailwindo \ No newline at end of file diff --git a/src/ConsoleHelper.php b/src/ConsoleHelper.php index 8bb5809..34880da 100644 --- a/src/ConsoleHelper.php +++ b/src/ConsoleHelper.php @@ -31,7 +31,7 @@ public function __construct(OutputInterface $output, array $settings) $this->folderConvert = $settings['folderConvert'] ?? false; } - public function folderConvert(string $folderPath) + public function folderConvert(string $folderPath, string $outputDir) { [$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion(); @@ -41,6 +41,10 @@ public function folderConvert(string $folderPath) $frameworkVersion.' to Tailwind '.$TailwindVersion ); + if (!file_exists($outputDir)) { + mkdir($outputDir, 0777, true); + } + if ($this->recursive) { $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator( @@ -62,18 +66,117 @@ public function folderConvert(string $folderPath) $extensions = explode('.', $directory); $extension = end($extensions); if ($directory->isFile() && $this->isConvertibleFile($extension)) { - $this->fileConvert($directory->getRealPath()); + $relativePath = substr($directory->getRealPath(), strlen(realpath($folderPath))); + $outputPath = rtrim($outputDir, '/') . '/' . ltrim($relativePath, '/'); + + if (!file_exists(dirname($outputPath))) { + mkdir(dirname($outputPath), 0777, true); + } + + $this->fileConvert($directory->getRealPath(), $outputPath); } } } - public function fileConvert($filePath) + // public function folderConvert(string $folderPath) + // { + // [$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion(); + + // $this->output->writeln('Converting Folder'.($this->components ? ' (extracted to tailwindo-components.css)' : '').': '.realpath($folderPath)); + // $this->output->writeln( + // 'Converting from '.$this->converter->getFramework()->frameworkName().' '. + // $frameworkVersion.' to Tailwind '.$TailwindVersion + // ); + + // if ($this->recursive) { + // $iterator = new \RecursiveIteratorIterator( + // new \RecursiveDirectoryIterator( + // $folderPath, + // \RecursiveDirectoryIterator::SKIP_DOTS + // ), + // \RecursiveIteratorIterator::SELF_FIRST, + // \RecursiveIteratorIterator::CATCH_GET_CHILD + // ); + // } else { + // $iterator = new \DirectoryIterator($folderPath); + // } + + // if ($this->folderConvert && $this->components) { + // $this->newComponentsFile(realpath($folderPath)); + // } + + // foreach ($iterator as $_ => $directory) { + // $extensions = explode('.', $directory); + // $extension = end($extensions); + // if ($directory->isFile() && $this->isConvertibleFile($extension)) { + // $this->fileConvert($directory->getRealPath()); + // } + // } + // } + + // public function fileConvert($filePath) + // { + // //just in case + // $filePath = realpath($filePath); + + // if (!$this->folderConvert) { + // $this->output->writeln('Converting FIle: '.($this->components ? '(extracted to tailwindo-components.css)' : '').' '.$filePath); + + // [$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion(); + // $this->output->writeln( + // 'Converting from '.$this->converter->getFramework()->frameworkName().' '. + // $frameworkVersion.' to Tailwind '.$TailwindVersion.PHP_EOL + // ); + // } + + // if (!is_file($filePath)) { + // $this->output->writeln('Couldn\'t convert: '.basename($filePath)); + + // return; + // } + + // $content = file_get_contents($filePath); + + // $lastDotPosition = strrpos($filePath, '.'); + + // if ($lastDotPosition !== false && !$this->overwrite) { + // $newFilePath = substr_replace($filePath, '.tw', $lastDotPosition, 0); + // } elseif (!$this->overwrite) { + // $newFilePath = $filePath.'.tw'; + // } else { + // // Set the new path to the old path to make sure we overwrite it + // $newFilePath = $filePath; + // } + + // $newContent = $this->converter + // ->setContent($content) + // ->convert() + // ->get($this->components); + + // if ($content !== $newContent) { + // $this->output->writeln('processed: '.basename($newFilePath)); + + // if ($this->components) { + // if (!$this->folderConvert) { + // $this->newComponentsFile(dirname($filePath)); + // } + + // $this->writeComponentsToFile($newContent, dirname($filePath)); + // } else { + // file_put_contents($newFilePath, $newContent); + // } + // } else { + // $this->output->writeln('Nothing to convert: '.basename($filePath)); + // } + // } + + public function fileConvert($filePath, $outputPath = null) { //just in case $filePath = realpath($filePath); if (!$this->folderConvert) { - $this->output->writeln('Converting FIle: '.($this->components ? '(extracted to tailwindo-components.css)' : '').' '.$filePath); + $this->output->writeln('Converting File: '.($this->components ? '(extracted to tailwindo-components.css)' : '').' '.$filePath); [$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion(); $this->output->writeln( @@ -84,21 +187,21 @@ public function fileConvert($filePath) if (!is_file($filePath)) { $this->output->writeln('Couldn\'t convert: '.basename($filePath)); - return; } $content = file_get_contents($filePath); - $lastDotPosition = strrpos($filePath, '.'); + if ($outputPath === null) { + $lastDotPosition = strrpos($filePath, '.'); - if ($lastDotPosition !== false && !$this->overwrite) { - $newFilePath = substr_replace($filePath, '.tw', $lastDotPosition, 0); - } elseif (!$this->overwrite) { - $newFilePath = $filePath.'.tw'; - } else { - // Set the new path to the old path to make sure we overwrite it - $newFilePath = $filePath; + if ($lastDotPosition !== false && !$this->overwrite) { + $outputPath = substr_replace($filePath, '.tw', $lastDotPosition, 0); + } elseif (!$this->overwrite) { + $outputPath = $filePath.'.tw'; + } else { + $outputPath = $filePath; // overwrite original + } } $newContent = $this->converter @@ -107,16 +210,16 @@ public function fileConvert($filePath) ->get($this->components); if ($content !== $newContent) { - $this->output->writeln('processed: '.basename($newFilePath)); + $this->output->writeln('processed: '.basename($outputPath)); if ($this->components) { if (!$this->folderConvert) { - $this->newComponentsFile(dirname($filePath)); + $this->newComponentsFile(dirname($outputPath)); } - $this->writeComponentsToFile($newContent, dirname($filePath)); + $this->writeComponentsToFile($newContent, dirname($outputPath)); } else { - file_put_contents($newFilePath, $newContent); + file_put_contents($outputPath, $newContent); } } else { $this->output->writeln('Nothing to convert: '.basename($filePath)); diff --git a/src/Converter.php b/src/Converter.php index 9d2c325..464b813 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -35,6 +35,7 @@ public function setContent(string $content): self $this->lastSearches = []; $this->components = []; + $this->convertClassToClassName(); return $this; } @@ -44,6 +45,7 @@ public function setFramework(string $framework): self $this->framework = new $framework(); + $this->convertClassToClassName(); return $this; } @@ -59,6 +61,7 @@ public function classesOnly(bool $value): self { $this->isCssClassesOnly = $value; + $this->convertClassToClassName(); return $this; } @@ -69,6 +72,7 @@ public function setGenerateComponents(bool $value): self { $this->generateComponents = $value; + $this->convertClassToClassName(); return $this; } @@ -86,6 +90,7 @@ public function setPrefix(string $prefix): self $this->prefix = $prefix; } + $this->convertClassToClassName(); return $this; } @@ -97,6 +102,7 @@ public function convert(): self } } + $this->convertClassToClassName(); return $this; } @@ -264,4 +270,20 @@ function ($match) use ($replace) { } } } -} + + /** + * Convert all class="..." to className="..." for JSX/React output. + */ + public function convertClassToClassName(): self + { + $this->givenContent = preg_replace_callback( + '/class=([\\"\'])(.*?)\1/', + function ($matches) { + return 'className=' . $matches[1] . $matches[2] . $matches[1]; + }, + $this->givenContent + ); + + return $this; + } +} \ No newline at end of file diff --git a/tailwindo b/tailwindo index 1d3c118..123a1d4 100755 --- a/tailwindo +++ b/tailwindo @@ -32,6 +32,8 @@ if (file_exists(__DIR__.'/vendor/autoload.php')) { ->addOption('prefix', 'p', InputOption::VALUE_REQUIRED, 'This allows you to add a custom prefix to all of Tailwind\'s generated utility classes', '') + ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Output directory for converted files', 'converted') + ->setCode(function (InputInterface $input, OutputInterface $output) { // output arguments and options $arg = trim($input->getFirstArgument()); @@ -69,11 +71,14 @@ if (file_exists(__DIR__.'/vendor/autoload.php')) { return $consoleHelper->fileConvert($arg); } + //folder ? if (is_dir($arg)) { - return $consoleHelper->folderConvert($arg); + $outputDir = $input->getOption('output'); + $consoleHelper->folderConvert($arg,$outputDir); } + //any html/css classes return $consoleHelper->codeConvert($arg); }) diff --git a/tailwindo.bat b/tailwindo.bat new file mode 100644 index 0000000..87fe34c --- /dev/null +++ b/tailwindo.bat @@ -0,0 +1,2 @@ +@echo off +php "%~dp0tailwindo" %*