-
-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migration to native Focus in Kirby v4 - regex to use #75
Comments
Thank you so much for your kind words! @FynnZW And also thank you for sharing your solution to migrate to Kirby v4. I'm sure it will help others. |
Thank you both for your incredible work! Worked absolutely smooth ❤️ |
Maybe make a CLI command that migrates the content? |
something like this? <?php
declare(strict_types=1);
use Kirby\CLI\CLI;
use Kirby\Filesystem\F;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
return [
// Author: Bnomei
// WARNING: this is a destructive command. Make a backup first!
//
// site/commands/focus-k4.php
// php vendor/bin/kirby focus-k4 --dry-run
//
'description' => 'Migrate from Focus plugin to native K4',
'args' => [
'verbose' => [
'shortPrefix' => 'v',
'longPrefix' => 'verbose',
'description' => 'Verbose output',
'defaultValue' => false,
'noValue' => true,
],
'dryrun' => [
'longPrefix' => 'dry-run',
'description' => 'Dry run',
'defaultValue' => false,
'noValue' => true,
],
],
'command' => static function (CLI $cli): void {
// https://github.com/flokosiol/kirby-focus/issues/75
$files = \Symfony\Component\Finder\Finder::create()
->in(kirby()->roots()->content())
->name('*.txt')
->files();
$processed = 0;
$replacements = 0;
$isDryRun = $cli->arg('dryrun');
$isVerbose = $cli->arg('verbose');
foreach ($files as $file) {
$path = $file->getPathname();
$relpath = $file->getRelativePathname();
$content = F::read($path);
if (!Str::contains($content, 'Focus: ')) {
if ($isVerbose) {
$cli->out("⏩ {$relpath}");
}
continue;
}
$content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
$json = json_decode('{' . $matches[1] . '}', true);
if (!is_array($json)) {
return $matches[0]; // no change
}
$x = A::get($json, 'x', 0);
$y = A::get($json, 'y', 0);
$x = number_format($x * 100, 0);
$y = number_format($y * 100, 0);
return "Focus: {$x}% {$y}%";
}, $content, -1, $count);
if (!$isDryRun) {
F::write($path, $content);
}
if ($count === 0 && $isVerbose) {
$cli->out("⏩ [0] {$relpath}");
}
if ($count > 0) {
$cli->out("✅ [{$count}] {$relpath}");
}
$processed++;
$replacements += $count;
}
$cli->success("files: {$processed}, replacements: {$replacements}");
}
]; |
I just tried it out and it worked perfectly! |
I startet to integrate the command into the plugin structure, but I faced one problem. I got an error, because And of course … THANK YOU @bnomei for taking the time to write the command and for sharing it! Very much appreciated! And secondly, sorry for the late response 🙂 |
In general, Kirby plugins upload the vendor folder to git for people that don't use composer. |
Because I wanted to use it as a global command I replaced the Symfony stuff with a native RecursiveIteratorIterator: <?php
declare(strict_types=1);
use Kirby\CLI\CLI;
use Kirby\Toolkit\A;
use Kirby\Toolkit\Str;
return [
// Author: Bnomei
// WARNING: this is a destructive command. Make a backup first!
//
// site/commands/focus-k4.php
// php vendor/bin/kirby focus-k4 --dry-run
//
'description' => 'Migrate from Focus plugin to native K4',
'args' => [
'verbose' => [
'shortPrefix' => 'v',
'longPrefix' => 'verbose',
'description' => 'Verbose output',
'defaultValue' => false,
'noValue' => true,
],
'dryrun' => [
'longPrefix' => 'dry-run',
'description' => 'Dry run',
'defaultValue' => false,
'noValue' => true,
],
],
'command' => static function (CLI $cli): void {
// https://github.com/flokosiol/kirby-focus/issues/75
$directory = kirby()->roots()->content();
$processed = 0;
$replacements = 0;
$isDryRun = $cli->arg('dryrun');
$isVerbose = $cli->arg('verbose');
$filenames = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::SELF_FIRST);
foreach ($filenames as $filename) {
$filepath = $filename->getPathname();
$relpath = str_replace($directory . '/', '', $filepath);
if ($filename->isDir() || pathinfo($filepath, PATHINFO_EXTENSION) !== 'txt') {
continue;
}
$content = file_get_contents($filepath);
if (!Str::contains($content, 'Focus: ')) {
if ($isVerbose) {
$cli->out("⏩ {$relpath}");
}
continue;
}
$content = preg_replace_callback('/^Focus: {(.*)}$/mi', function ($matches) use ($cli) {
$json = json_decode('{' . $matches[1] . '}', true);
if (!is_array($json)) {
return $matches[0]; // no change
}
$x = A::get($json, 'x', 0);
$y = A::get($json, 'y', 0);
$x = number_format($x * 100, 0);
$y = number_format($y * 100, 0);
return "Focus: {$x}% {$y}%";
}, $content, -1, $count);
if (!$isDryRun) {
file_put_contents($filepath, $content);
}
if ($count === 0 && $isVerbose) {
$cli->out("⏩ [0] {$relpath}");
}
if ($count > 0) {
$cli->out("✅ [{$count}] {$relpath}");
}
$processed++;
$replacements += $count;
}
$cli->success("files processed: {$processed}, replacements: {$replacements}");
}
]; |
I don't know if this is the correct place, but I just tried to migrate existing focus points from this plugin to the new native syntax in Kirby 4.
So going from:
Focus: {"x":0.5, "y": 0.22}
to:
Focus: 50% 22%
.Converting from one decimal place to percentage (0.5 = 50%) made this not so easy.
Here's how I did it - might be useful for others.
Done with VSCode search & replace on the
content
folder (via regex, the.*
-button)Transform X values to two decimal places
Search:
Focus: \{"x":(0.\d(?!\d)),"y":(0.\d+)\}
Replace:
Focus: {"x":$10,"y":$2}
Transform Y values to two decimal places
Search:
Focus: \{"x":(0.\d+),"y":(0.\d(?!\d))\}
Replace:
Focus: {"x":$1,"y":$20}
Turn to percentage syntax
Search:
Focus: \{"x":\d+\.(\d+?),"y":\d+\.(\d+?)\}
Replace:
Focus: $1% $2%
PS: Thanks a lot for this great plugin! While it won't be needed anymore soon, it made working with images in Kirby v3 so much better (I probably wouldn't have used Kirby v3 if this plugin hadn't existed).
The text was updated successfully, but these errors were encountered: