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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ php:
- 7.0
- 7.1
- 7.2
- 8.4

env:
matrix:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
137 changes: 120 additions & 17 deletions src/ConsoleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -41,6 +41,10 @@ public function folderConvert(string $folderPath)
$frameworkVersion.' <fg=black;bg=green> to </> Tailwind '.$TailwindVersion
);

if (!file_exists($outputDir)) {
mkdir($outputDir, 0777, true);
}

if ($this->recursive) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator(
Expand All @@ -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('<fg=black;bg=blue>Converting Folder'.($this->components ? ' (extracted to tailwindo-components.css)' : '').':</> '.realpath($folderPath));
// $this->output->writeln(
// '<fg=black;bg=green>Converting from</> '.$this->converter->getFramework()->frameworkName().' '.
// $frameworkVersion.' <fg=black;bg=green> 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('<fg=black;bg=blue>Converting FIle: '.($this->components ? '(extracted to tailwindo-components.css)' : '').'</> '.$filePath);

// [$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion();
// $this->output->writeln(
// '<fg=black;bg=green>Converting from</> '.$this->converter->getFramework()->frameworkName().' '.
// $frameworkVersion.' <fg=black;bg=green> to </> Tailwind '.$TailwindVersion.PHP_EOL
// );
// }

// if (!is_file($filePath)) {
// $this->output->writeln('<comment>Couldn\'t convert: </comment>'.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('<info>processed: </info>'.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('<comment>Nothing to convert: </comment>'.basename($filePath));
// }
// }

public function fileConvert($filePath, $outputPath = null)
{
//just in case
$filePath = realpath($filePath);

if (!$this->folderConvert) {
$this->output->writeln('<fg=black;bg=blue>Converting FIle: '.($this->components ? '(extracted to tailwindo-components.css)' : '').'</> '.$filePath);
$this->output->writeln('<fg=black;bg=blue>Converting File: '.($this->components ? '(extracted to tailwindo-components.css)' : '').'</> '.$filePath);

[$frameworkVersion, $TailwindVersion] = $this->converter->getFramework()->supportedVersion();
$this->output->writeln(
Expand All @@ -84,21 +187,21 @@ public function fileConvert($filePath)

if (!is_file($filePath)) {
$this->output->writeln('<comment>Couldn\'t convert: </comment>'.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
Expand All @@ -107,16 +210,16 @@ public function fileConvert($filePath)
->get($this->components);

if ($content !== $newContent) {
$this->output->writeln('<info>processed: </info>'.basename($newFilePath));
$this->output->writeln('<info>processed: </info>'.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('<comment>Nothing to convert: </comment>'.basename($filePath));
Expand Down
24 changes: 23 additions & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function setContent(string $content): self
$this->lastSearches = [];
$this->components = [];

$this->convertClassToClassName();
return $this;
}

Expand All @@ -44,6 +45,7 @@ public function setFramework(string $framework): self

$this->framework = new $framework();

$this->convertClassToClassName();
return $this;
}

Expand All @@ -59,6 +61,7 @@ public function classesOnly(bool $value): self
{
$this->isCssClassesOnly = $value;

$this->convertClassToClassName();
return $this;
}

Expand All @@ -69,6 +72,7 @@ public function setGenerateComponents(bool $value): self
{
$this->generateComponents = $value;

$this->convertClassToClassName();
return $this;
}

Expand All @@ -86,6 +90,7 @@ public function setPrefix(string $prefix): self
$this->prefix = $prefix;
}

$this->convertClassToClassName();
return $this;
}

Expand All @@ -97,6 +102,7 @@ public function convert(): self
}
}

$this->convertClassToClassName();
return $this;
}

Expand Down Expand Up @@ -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;
}
}
7 changes: 6 additions & 1 deletion tailwindo
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
})
Expand Down
2 changes: 2 additions & 0 deletions tailwindo.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
php "%~dp0tailwindo" %*